php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #31032 Array comparision operators doc inprecise
Submitted: 2004-12-09 04:42 UTC Modified: 2004-12-09 09:09 UTC
From: php dot devel at homelinkcs dot com Assigned:
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS: Probably all
Private report: No CVE-ID: None
 [2004-12-09 04:42 UTC] php dot devel at homelinkcs dot com
Description:
------------
Based on my experimentation, this description of array  
comparison operators, found at  
http://us2.php.net/manual/en/language.operators.array.php,  
seems terribly imprecise:  
  
$a == $b	TRUE if $a and $b have the same elements.  
$a === $b	TRUE if $a and $b have the same elements  
in the same order.  
  
I submit that more precise and helpful definitions would  
be:  
  
$a == $b	TRUE if $a and $b have the same key/value  
pairs.  
$a === $b	TRUE if $a and $b have the same key/value  
pairs, in the same order, and of the same types.  
  
I based my conclusions on the script below, tested on  
Linux using PHP 4.3.4 and PHP 5.0.2.  

Reproduce code:
---------------
<?php

// Equality operator:

// Key/value pairs must be same
var_dump(array(1,2) == array(1=>1,2=>2)); // False

// Order can be different
var_dump(array(1,2) == array(1=>2,0=>1)); // True

// And types can be different
var_dump(array(1,2) == array('1','2e0')); // True


// Identity operator:

// Key/value pairs must be same
var_dump(array(1,2) === array(1=>1,2=>2)); // False

// Order must be same
var_dump(array(1,2) === array(1=>2,0=>1)); // False

// And types must be same
var_dump(array(1,2) === array('1',2)); // False
var_dump(array(1,2) === array(1,'2e0')); // False

?>

Expected result:
----------------
Taking the manual's descriptions at face-value, I would 
have expected the script to return: 
 
bool(true) (elements are same, only keys are different) 
bool(true) 
bool(true) 
bool(true) (elements are in same order, only keys are 
different) 
bool(false) 
bool(true) (elements are in same order, only types, which 
the manual doesn't refer to in discussion of arrays, are 
different) 
bool(true)  
 

Actual result:
--------------
The script actually returns (as explained in the 
comments): 
 
bool(false)  
bool(true)  
bool(true)  
bool(false)  
bool(false)  
bool(false)  
bool(false)  
  

Patches

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-12-09 09:09 UTC] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Aug 20 10:01:27 2024 UTC