note 116176 added to language.types.null
| From: | naitsabes dot pawlak at gmail dot com | Date: | Tue, 18 Nov 2014 16:34:08 +0000 |
| Subject: | note 116176 added to language.types.null | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-200949@lists.php.net to get a copy of this message | ||
Why "" == NULL array() == NULL gives TRUE?
'==' is comparison operator which gives true if both sides are equal after type juggling.
Lets look first at "" == NULL
When you are comparing NULL with string, then NULL is converted to empty string "". So
operator '==' is comparing strings in this situation:
"" == NULL
is converted to:
"" == ""
which gives TRUE.
When you are comparing NULL with anything else except strings, then both sides are converted to
bool. So when you are comparing null with empty array, then null is converted to FALSE and empty
array is converted to FALSE.
array() == NULL
is converted to
FALSE == FALSE
which gives of course TRUE.
I'm basing on http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.types
My previous answer was wrong about comparing "" == NULL. I was assuming that here both
sides are also converted to bool, but it isn't true - NULL is converted here to string. You can
just check comparison:
"0" == NULL
if in this comparison both sides are converted to bool then it should give TRUE ((bool)NULL = FALSE,
(bool)"0" = FALSE). But this comparison gives FALSE.
----
Server IP: 72.52.91.14
Probable Submitter: 84.10.190.187
----
Manual Page -- http://php.net/manual/en/language.types.null.php
Edit -- https://master.php.net/note/edit/116176
Del: integrated -- https://master.php.net/note/delete/116176/integrated
Del: useless -- https://master.php.net/note/delete/116176/useless
Del: bad code -- https://master.php.net/note/delete/116176/bad+code
Del: spam -- https://master.php.net/note/delete/116176/spam
Del: non-english -- https://master.php.net/note/delete/116176/non-english
Del: in docs -- https://master.php.net/note/delete/116176/in+docs
Del: other reasons-- https://master.php.net/note/delete/116176
Reject -- https://master.php.net/note/reject/116176
Search -- https://master.php.net/manage/user-notes.php