php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46640 A bug then using equality operator ("==") to compare arrays
Submitted: 2008-11-21 17:10 UTC Modified: 2008-11-24 13:26 UTC
From: realbaziak at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.2.6 OS: Gentoo Linux
Private report: No CVE-ID: None
 [2008-11-21 17:10 UTC] realbaziak at gmail dot com
Description:
------------
Equality operator returns true, though arrays are different.
Is it a bug or feature? :)

Thanks.

Reproduce code:
---------------
$a =  array(0 => "something");
$b1 = array(0 => "another");
$b2 = array(0 => "0");
$b3 = array(0 => null);
$b4 = array(0 => 0);

$a2 = array(0 => "two", 1 => "elements");
$b5 = array(0 => 0, 1 => 0);

var_dump($a == $b1); // bool(false)
var_dump($a == $b2); // bool(false)
var_dump($a == $b3); // bool(false)
var_dump($a == $b4); // bool(true) -- why array(0 => "something") 
                     // equals to array(0 => 0)?

var_dump($a2 == $b5); // bool(true) -- it seems there are some regularity

Expected result:
----------------
always bool(false)

Actual result:
--------------
bool(true) on something like 
array(0 => "one", 1 => "two") == array(0 => 0, 1 => 0)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-11-21 17:35 UTC] jani@php.net
Find out about ===
 [2008-11-24 13:26 UTC] realbaziak at gmail dot com
"===" works fine. The problem is with "==".
Sure I can use array_diff() instead, but this bug can produce very strange errors.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sun Dec 08 05:01:23 2019 UTC