#45518 [Opn->Bgs]: NULL matching zero, empty array, false
| From: | stephen dot cuppett at sas dot com | Date: | Tue, 15 Jul 2008 12:23:50 +0000 |
| Subject: | #45518 [Opn->Bgs]: NULL matching zero, empty array, false | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-126759@lists.php.net to get a copy of this message | ||
ID: 45518
User updated by: stephen dot cuppett at sas dot com
Reported By: stephen dot cuppett at sas dot com
-Status: Open
+Status: Bogus
Bug Type: Variables related
Operating System: Windows XP/2003
PHP Version: 5.2.6
New Comment:
NM, I see the loose comparisons table in the appendix.
Previous Comments:
------------------------------------------------------------------------
[2008-07-15 12:19:28] stephen dot cuppett at sas dot com
Description:
------------
I noticed a behavior with PHP 5.2.x that I found disturbing. PHP is
matching variables initialized to zero, an empty array and false with
NULL. From the documentation here:
http://us2.php.net/NULL
The only thing that should match NULL are ones that meet these
criteria:
A variable is considered to be null if:
- it has been assigned the constant NULL.
- it has not been set to any value yet.
- it has been unset().
The function empty() should return true in the above cases (and does),
but a comparison operator of == should not. FYI, is_null() returns
false and the === operator works as expected too.
Reproduce code:
---------------
<?php
$empty_array = array();
if ($empty_array == NULL)
echo "Empty array, same as NULL\n";
else
echo "Empty array, diff from NULL\n";
$zero = 0;
if ($zero == NULL)
echo "Zero, same as NULL\n";
else
echo "Zero, different from NULL\n";
?>
Expected result:
----------------
Empty array, diff from NULL
Zero, different from NULL
Actual result:
--------------
Empty array, same as NULL
Zero, same as NULL
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=45518&edit=1