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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 30 23:01:29 2024 UTC