Is there any difference in PHP between while(true)
and for(;;)
besides syntax and readability?
Loops in PHP are used to execute the same block of code a specified number of times. PHP supports following four loop types. for − loops through a block of code a specified number of times. while − loops through a block of code if and as long as a specified condition is true.
The while loop in python runs until the "while" condition is satisfied. The "while true" loop in python runs without any conditions until the break statement executes inside the loop.
The 'for' loop used only when we already knew the number of iterations. The 'while' loop used only when the number of iteration are not exactly known. If the condition is not put up in 'for' loop, then loop iterates infinite times. If the condition is not put up in 'while' loop, it provides compilation error.
The while loop executes a block of code as long as the specified condition is true.
Ok, so first off, let me say this: Use while(true)
, as it gives the most semantic meaning. You need to parse for (;;)
as it's not something you see often.
With that said, let's analyze:
The code
while(true) { break; } echo "hi!";
Compiles down to the opcodes:
0: JMPZ(true, 3) 1: BRK(1, 3) 2: JMP(0) 3: ECHO("hi!")
So basically, it does a check if "true", and if not, jumps to the 4th opcode which is the echo opcode). Then it breaks (which is really just a static jump to the 4th opcode). Then the end of the loop would be an unconditional jump back to the original check
Compare that to:
for (;;) { break; } echo "hi!";
Compiles down to:
0: JMPZNZ(true, 2, 4) 1: JMP(0) 2: BRK(1, 4) 3: JMP(1) 4: ECHO("hi!")
So we can immediately see that there's an extra opcode in the for(;;)
version.
This opcode jumps if the condition is false
. If it is true
, it does nothing but advance one opcode.
This opcode jumps to pos1
if the condition is true, and pos2
if the condition is false.
This opcode always jumps to the opcode at the specified position.
This breaks level
levels to the opcode at position
Outputs the string
Well, looking at the opcodes, it's clear that they are not identical. They are ==
, but not ===
. The while(true)
loop does a conditional jump followed by code followed by an unconditional jump. The for(;;)
loop does a conditional jump, followed by code, followed by an unconditional jump, followed by another unconditional jump. So it does an extra jump.
In 5.5, the Optimizer portion of opcache will optimize static conditional jumps.
So that means the while(true)
code will optimize down to:
0: BRK(1, 2) 1: JMP(0) 2: ECHO("hi!")
And for(;;)
loop becomes:
0: BRK(1, 2) 1: JMP(0) 2: ECHO("hi!")
This is because the optimizer will find and optimize out jump-chains. So if you're using 5.5's built-in opcache, they will be identical...
This is a complete and utter micro-optimization to base a decision on. Use the readable one. Don't use one based on performance. The difference is there, but it's trivial.
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