php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #45518 NULL matching zero, empty array, false
Submitted: 2008-07-15 12:19 UTC Modified: 2008-07-15 12:23 UTC
From: stephen dot cuppett at sas dot com Assigned:
Status: Not a bug Package: Variables related
PHP Version: 5.2.6 OS: Windows XP/2003
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: stephen dot cuppett at sas dot com
New email:
PHP Version: OS:

 

 [2008-07-15 12:19 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-07-15 12:23 UTC] stephen dot cuppett at sas dot com
NM, I see the loose comparisons table in the appendix.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sun Jan 05 04:01:29 2025 UTC