php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #65237 Array Equality Bug
Submitted: 2013-07-10 17:32 UTC Modified: 2013-07-11 15:19 UTC
From: jchan at malwarebytes dot org Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.3.26 OS: OSX & Linux Tested
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: jchan at malwarebytes dot org
New email:
PHP Version: OS:

 

 [2013-07-10 17:32 UTC] jchan at malwarebytes dot org
Description:
------------
When comparing 2 arrays in an if statement, array equality according to the 
documentation (http://www.php.net/manual/en/language.operators.array.php) does not 
work. According to the docs, order should not matter, but when comparing 2 arrays 
directly it does.

Test script:
---------------
<?php

// based off of http://www.php.net/manual/en/language.operators.array.php

$a = array("apple", "banana");
$b = array(1 => "banana", "0" => "apple");


## WORKS ##
var_dump($a == $b); // bool(true)
var_dump($a === $b); // bool(false)
var_dump(array("apple", "banana") == array(1 => "banana", "0" => "apple")); // bool(true)


## FAILS? ##
if (array('apple','banana') == array(1 => 'banana',2=>'apple'))
   echo "they are equal.\n";
else
   echo "They are not equal.\n"; // this is the response I get

## yet if I do:
if ($a == $b)
   echo "they are equal.\n"; // this is the response I get...
else 
   echo "They are not equal.\n";



?>

Expected result:
----------------
bool(true)
bool(false)
bool(true)
they are equal.
they are equal.

Actual result:
--------------
bool(true)
bool(false)
bool(true)
They are not equal.
they are equal.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-07-11 06:53 UTC] ab@php.net
-Status: Open +Status: Not a bug
 [2013-07-11 06:53 UTC] ab@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

array('apple','banana') == array(1 => 'banana',2=>'apple')

is the same as

array(0 => 'apple', 1 => 'banana') == array(1 => 'banana',2=>'apple')

So the doc won :)
 [2013-07-11 15:19 UTC] jchan at malwarebytes dot org
@ab@php.net - ah, you're right. Thank you.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat May 03 15:01:28 2025 UTC