Doc #74329 [Opn->Nab]: Inconsistency in operators precedence example

From: Date: Wed, 29 Mar 2017 09:03:04 +0000
Subject: Doc #74329 [Opn->Nab]: Inconsistency in operators precedence example
References: 1  Groups: php.doc.bugs 
Request: Send a blank email to doc-bugs+get-14558@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=74329&edit=1 ID: 74329 Updated by: requinix@php.net Reported by: pierstoval at gmail dot com Summary: Inconsistency in operators precedence example -Status: Open +Status: Not a bug Type: Documentation Problem -Package: Documentation problem +Package: Scripting Engine problem PHP Version: Irrelevant Block user comment: N Private report: N New Comment: 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. Previous Comments: ------------------------------------------------------------------------ [2017-03-29 08:25:57] 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 08:15:34] 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); ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=74329&edit=1

« previous php.doc.bugs (#14558) next »