In the PHP manual, operator precedence section, there is this example:
// mixing ++ and + produces undefined behavior
$a = 1;
echo ++$a + $a++; // may print 4 or 5
I understand the behavior is undefined because of the following reason:
Since x + y = y + x
the interpreter is free to evaluate x
and y
for addition in any order in order to optimize speed and/or memory. I concluded this after looking at the C code example in this article.
My question is that the output of the above mentioned PHP code should be 4
no matter which way the expression and sub-expressions are evaluated:
Where does the 5 come from? Or should I learn more about how the operators work?
Edit:
I have been staring at Incrementing/Decrementing Operators section but still could not figure out why 5.
++$a: Pre-increment -- Increments $a by one, then returns $a.
$a++: Post-increment -- Returns $a, then increments $a by one.
The echo command writes text to standard output (stdout). Some common usages of the echo command are piping shell variable to other commands, writing text to stdout in a shell script, and redirecting text to a file.
Echo Command Syntax The echo command in Linux is used to display a string provided by the user. For example, use the following command to print Hello, World! as the output: echo Hello, World!
In computing, echo is a command that outputs the strings that are passed to it as arguments. It is a command available in various operating system shells and typically used in shell scripts and batch files to output status text to the screen or a computer file, or as a source part of a pipeline.
a = 1;
++ (preincrement) gives a = 2 (higher precedence than +, and LR higher precedence than postincrement)
++ (postincrement) gives a = 3 (higher precedence than +)
+ (add) gives 2 + 3 = 5
$a is initially set to 1. The ++$a then preincrements $a before using it in the formula, setting it to 2, and pushing that value onto the lexer stack. The $++ is then executed, because incrementor has a higher precedence than +, and that value is also pushed that result onto the lexer stack; and the addition that then takes place adds the lexer stack's 2 result to the lexer stack's 3 result giving a result of 5, which is then echoed. The value of $a once the line has executed is 3.
OR
a = 1;
++ (preincrement) gives a = 2 (higher precedence than +, and LR higher precedence than postincrement)
+ (add) gives 2 + 2 = 4 (the value that is echoed)
++ (postincrement) gives a = 3 (incremented __after__ the variable is echoed)
$a is initially set to 1. When the formula is parses, the ++$a preincrements $a, setting it to 2 before using it in the formula (pushing the result to the lexer stack). The result from the lexer stack and the current value of $a are then added together giving 4; and this value is echoed. Finally, $a is postincremented, leaving a value of 3 in $a.
Yes it will give you 5 because the right side operator works first by its priority/precendence and after that the sum(+) operator will work. So first increment makes it to 2 and second makes it to 3 and after that both will sum and outputs you the result as 5
$result = ++$a + $a++;
++$a
outputs as 2
$a++
outputs as 2 3 only but internally it wll be incremented.
finally sum will happens as 2+3 = 5
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With