php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #53724 array_diff like methods compares false as identity and true as equal
Submitted: 2011-01-12 14:16 UTC Modified: 2015-05-08 21:12 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: lukas dot starecek at centrum dot cz Assigned: cmb (profile)
Status: Not a bug Package: Arrays related
PHP Version: 5.3.5 OS: Irrelevant
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: lukas dot starecek at centrum dot cz
New email:
PHP Version: OS:

 

 [2011-01-12 14:16 UTC] lukas dot starecek at centrum dot cz
Description:
------------
array_diff functions compares boolean false as identity (operator "===") and boolean true as equal (operator "==") so behaves inconsistently. It should compare all values in same way and in my opinion it should compare as equals ("==") or have argument (or another mechanism) how to set whether compare by equal or identity.

As example I use array_diff_assoc but I think it will probably behave simmilary with another array realted functions (array_diff%, array_intersect%). In expected result I suppose comparing as equal (operator '==').

In example you can see, that array_diff_assoc behaves in such way that 0 or '0' or '' is not same as false, but 1 or '1' is same as true. So false is compared like '===' and true is compared like '=='. Because 0 is same as '0' and 1 is same as '1' I suppose, that this problem is only for boolean false value.


Test script:
---------------
$arr1 = array('a' => 0,     'b' => 1,    'c' => '0',   'd' => '1',  'e' => '',    'f' => false, 'g' => true, 'h' => 0,   'i' => 1);
$arr2 = array('a' => false, 'b' => true, 'c' => false, 'd' => true, 'e' => false, 'f' => false, 'g' => true, 'h' => '0', 'i' => '1');

var_dump(array_diff_assoc($arr1, $arr2));

Expected result:
----------------
array(0) {
}


Actual result:
--------------
array(2) {
  ["a"]=>
  int(0)
  ["c"]=>
  string(1) "0"
}


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-01-12 14:27 UTC] lukas dot starecek at centrum dot cz
A little mistake, '' behaves as same with false, so problem is only in comparing 0 and '0' (and maybee some other variables I did not used).
 [2011-02-20 16:20 UTC] tbrasta at gmail dot com
This is not a bug. The function is operating exactly as stated in the documentation, i.e. casted to string values are compared. Since (string) 0 = "0", (string) false = "" and (string) "0" = "0" but "0" !== "", you get the result as it should be.
 [2011-02-21 23:23 UTC] lukas dot starecek at centrum dot cz
Ah I did not noticed this "grey" part of method documentation in examples. You are right, documentation says it, but imho in not so clear way. I have read only specifications, not examples. Imho documentation could be a little bit more exact, because this is important.

So sadly this functions are designed only for string comparison, not for general variable comparison :-/ .

My fault, bogus, sorry. But think about functions of this kind for general variables comparison, not only for string comparison :-) .
 [2015-05-08 21:12 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2015-05-08 21:12 UTC] cmb@php.net
As tbrasta has already explained, this is not a bug. I agree, though, that the documentation of array_diff() should be improved, by linking to array_diff_uassoc(), array_udiff_assoc() and array_udiff_uassoc() in the "see also" section. I've submitted a respective patch via the online editor.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Wed Jul 02 09:01:38 2025 UTC