php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #60688 Array comparison documentation inconsistency
Submitted: 2012-01-08 23:48 UTC Modified: 2014-02-01 11:18 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: wolfgang dot stengel at gmx dot de Assigned:
Status: Not a bug Package: Documentation problem
PHP Version: 5.3.8 OS: Debian
Private report: No CVE-ID: None
 [2012-01-08 23:48 UTC] wolfgang dot stengel at gmx dot de
Description:
------------
Comparing arrays is in the PHP manual described as "Array with fewer members is 
smaller, if key from operand 1 is not found in operand 2 then arrays are 
uncomparable, otherwise - compare value by value (see following example)" 
(http://www.php.net/manual/en/language.operators.comparison.php).

This does not mention that the order in which the operands are compared is 
reversed with the > and >= operators (I think). I found this in 
zend_language_parser.y:

T_IS_GREATER_OR_EQUAL expr { zend_do_binary_op(ZEND_IS_SMALLER_OR_EQUAL, &$$, &$3, 
&$1 TSRMLS_CC); }

This will lead to the foreach in the transcription of the comparison code 
iterating over $b instead of $a in the example below.

Test script:
---------------
$a=array(0 => 2, 'test' => 2);
$b=array('test' => 3, 0 => 1);
var_dump($a>$b);

Expected result:
----------------
The documentation should mention that the > and >= operators flip the arguments.

Actual result:
--------------
The documentation does not mention that the > and >= operators flip the arguments.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-01-24 23:04 UTC] frozenfire@php.net
I'm not seeing how this minor engine quirk produces a distinguishable effect upon 
the result of the operation. Could you provide a test script which demonstrates 
that different results will be produced based on a different invocation?
 [2012-01-24 23:04 UTC] frozenfire@php.net
-Status: Open +Status: Feedback
 [2012-01-25 11:08 UTC] wolfgang dot stengel at gmx dot de
I agree that this is minor and does not need to be changed in the engine. I just thought it would be nice to have a half sentence anywhere in the documentation mentioning this. Here's the script:

$a=array(0 => 2, 'test' => 2);
$b=array('test' => 3, 0 => 1);
var_dump($a>$b, $b>$a);

Both operations result in false, which makes no sense.
 [2012-01-25 11:08 UTC] wolfgang dot stengel at gmx dot de
-Status: Feedback +Status: Open
 [2014-02-01 11:18 UTC] krakjoe@php.net
-Status: Open +Status: Not a bug
 [2014-02-01 11:18 UTC] krakjoe@php.net
It does make sense ...

<?php
$a = [1=>"one", 2=>"two"];
$b = [2=>"two", 1=>"one"];

var_dump($a>$b, $a<$b, $a==$b);
?>

Produces exactly what is expected from the description:

bool(false)
bool(false)
bool(true)

So marking as not a bug, if somehow I'm wrong, feel free to open it again.

Noteworthy, this bug was reported for a version of PHP that is unsupported today, if there is still a discrepancy which I do not see, then it would be wise to report the bug for a stable, supported version of PHP.

Thanks for taking the time to make PHP better :)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 07:01:29 2024 UTC