I have a situation here with post fix and prefix operator.Take a look at below code snippet. I know some operator precedence exist. but i need a clear explanation regarding this.
<?php
$a=0;
echo $a++ + ++$a; //this will result 2 that is ok
$b=0;
echo $b++ - ++$b; //this should result 0 isn't it? but am getting -2!
?>
Update : The accepted answer describes undefined behavior
That always happens if you make use of undefined behaviour. And what you do is undefined behaviour. So don’t try to get an explanation that follows a deterministic logic as undefined is the opposite of that.
The PHP manual states:
You extended your question in a comment:
This is obviously how it results for your code when you execute it. It is not defined that this will be always calculated that way, hence undefined. For example if the second bracket is processed first, you’ll get a different result.