note 92765 added to language.operators.increment
| From: | sneskid at hotmail dot com | Date: | Fri, 07 Aug 2009 22:49:23 +0000 |
| Subject: | note 92765 added to language.operators.increment | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-159204@lists.php.net to get a copy of this message | ||
(related to what "Are Pedersen" wrote)
With arrays it can lead to much confusion if your index variable is altered on the right side of the
= sign, either with ++|-- or even when passed to a function by reference..
Consider these (PHP 5):
<?php
$A[$a] = ++$a; // [1]=1
$B[++$b] = ++$b; // [1]=2
$C[$c+=0] = ++$c; // [0]=1
?>
In 'A' you have to be aware that PHP evaluates $A[$a] last.
Yet in 'B' and 'C' PHP evaluates the index and saves it in a temporary variable.
You can always force PHP to evaluate a variable without explicitly storing it as a named variable
first, with a simple "+=0" like in example 'C'.
Compared to 'A', 'C' gives the more logically expected result, when we expect
evaluation occurs left to right.
PHP does evaluate left to right BUT it will attempt to cut down on temporary variables, which can
lead to confusing results.
So just be aware and use either behavior to your advantage for the desired functionality.
----
Server IP: 209.41.74.194
Probable Submitter: 68.146.80.45
----
Manual Page -- http://www.php.net/manual/en/language.operators.increment.php
Edit -- https://master.php.net/note/edit/92765
Del: integrated -- https://master.php.net/note/delete/92765/integrated
Del: useless -- https://master.php.net/note/delete/92765/useless
Del: bad code -- https://master.php.net/note/delete/92765/bad+code
Del: spam -- https://master.php.net/note/delete/92765/spam
Del: non-english -- https://master.php.net/note/delete/92765/non-english
Del: in docs -- https://master.php.net/note/delete/92765/in+docs
Del: other reasons-- https://master.php.net/note/delete/92765
Reject -- https://master.php.net/note/reject/92765
Search -- https://master.php.net/manage/user-notes.php