php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #74329 Inconsistency in operators precedence example
Submitted: 2017-03-29 08:15 UTC Modified: 2017-03-29 09:03 UTC
From: pierstoval at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: Irrelevant OS:
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: pierstoval at gmail dot com
New email:
PHP Version: OS:

 

 [2017-03-29 08:15 UTC] pierstoval at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/language.operators.precedence
---

There's an inconsistency in Example #2 on the "Language Operators Precedence" page.

The example states that the script "may print either 2 or 3", but actually, it will *always* print "3", and for the second script, the array will *always* be equal "[2 => 1]".

This might confuse people about the real role of precedence operators.

You can check on 3v4l here: https://3v4l.org/EUknn

Test script:
---------------
// First script
$a = 1;
echo $a + $a++;
echo "\n";

// Second script
$i = 1;
$array[$i] = $i++;
var_export($array);


Patches

fix-precedence-doc-inconsistency (last revision 2017-03-29 08:20 UTC by pierstoval at gmail dot com)

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-03-29 08:25 UTC] pierstoval at gmail dot com
The "may print either 2 or 3" issue only happens with 2 PHP versions:
* PHP 4.3.0 (released in 2002 and fixed 2 months later in 4.3.1)
* and 5.0.5 (released in 2005 and fixed 3 months later in 5.1.0).

That's why I consider this "may print either 2 or 3" sentence as an inconsistency
 [2017-03-29 09:03 UTC] requinix@php.net
-Status: Open +Status: Not a bug -Package: Documentation problem +Package: Scripting Engine problem
 [2017-03-29 09:03 UTC] requinix@php.net
You're already confused about what precedence means. Precedence (and associativity) is about grouping operands and says that (eg) $a+$b*$c will be interpreted as $a+($b*$c). It does not say that $a+$b*$c will be *executed* using $b first, $c second, and $a third - or any other way.

> echo $a + $a++;
PHP does not guarantee when the $a and $a++ are evaluated. It could be that the $a comes first (=1) and the $a++ second (=1 and $a=2) resulting in 1+1=2, or it could be that $a++ comes first (=1 and $a=2) and $a second (=2) resulting in 1+2=3.

> $array[$i] = $i++;
Same situation. PHP does not guarantee when the $i and $i++ are evaluated. It could be that the $i comes first (=1) and the $i++ second (=1 and $i=2) resulting in 1=>1, or it could be that $i++ comes first (=1 and $i=2) and $i second (=2) resulting in 2=>1.

Consider:
  function f() { echo "f"; return 1; }
  function g() { echo "g"; return 2; }
  function h() { echo "h"; return 3; }
  echo f() + g() * h();
Precedence states it will be grouped as f+(g*h) and thus the result will be 1+(2*3)=7. But do you know how it will be executed? Are you sure?
Take a look: https://3v4l.org/LVr4o

Your homework is to learn about operator precedence vs. order of evaluation, and optionally what sequence points are. It's mostly about C but a lot applies to PHP too.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Wed Oct 07 20:00:02 2026 UTC