Have you ever looked into PHP compiled byte code ? If not - I suggest you doing so, because :
When analyzing bytecode - you will become a better developer, knowing how to write more efficient PHP code and it will increase your code reading skills too.
You will understand the need of the JIT compiler for a PHP (it's currently in a road-map)
I will skip tutorial part about How to see PHP script's compiled bytecode - there are a lot of such tutorials in internet, for example this one is good . Instead, let's jump to the rant about How PHP generates un-optimized bytecode.
Here is a test snippet of which bytecode we will check :
$a = 10; $b = 5; if ($b > $a) echo $a;
And PHP generates such bytecode for snippet above :
0 ASSIGN_____________!0, 10 1 ASSIGN_____________!1, 5 2 IS_SMALLER____~4__!0, !1 3 JMPZ_______________~4,->5 4 ECHO______________!0 5 RETURN____________1
What a mess !! Needless to say that PHP - at least in this case - doesn't perform bytecode optimizations. IF it would - it would see that this bytecode semantically is equivalent to :
0 JMP________________->5 1 ASSIGN_____________!1, 5 2 IS_SMALLER____~4__!0, !1 3 JMPZ_______________~4,->5 4 ECHO______________!0 5 RETURN____________1
Or even to :
0 NOP_______________ 1 NOP_______________ 2 NOP_______________ 3 NOP_______________ 4 NOP_______________ 5 RETURN____________1
Which is same as simply :
(An empty PHP script)
We have saved 5 bytecode instructions ! Why to generate redundant code ?
I've missed one side effect - IF globals
$a,$b are used in other scripts then including this exact script somewhere else - will change program behavior, so in that case script bytecode is equivalent to :
0 ASSIGN_____________!0, 10 1 ASSIGN_____________!1, 5 2 RETURN_____________1
Still we saved 3 bytecode instructions ! So JIT'er should evaluate globals usage and according to that - generate most optimal bytecode possible.