I was looking through a php package and found an interesting line of code which I would like some light shed on. Rather than passing expressions into the for-loop parameters, all that was written was two semi-colons.
for (;;) { // This is the line in question
$this->output .= $this->a;
$this->a = $this->get();
if ($this->a === $this->b) {
break;
}
if (ord($this->a) <= self::ORD_LF) {
throw new JSMinException('Unterminated string literal.');
}
if ($this->a === '\\') {
$this->output .= $this->a;
$this->a = $this->get();
}
}
I understand that a for-loop normally contains 3 expressions separated by semi-colons. So this is clearly just running a for-loop with no expressions and relying on a break (Line 6) to exit the loop.
What I want to know is, does this method have a performance advantage over the traditional method? The way I see it, this could have been easily rewritten as a while loop like so:
while($this->a !== $this->b){
// Loop Contents
}
Even if it does, and even if it doesn’t, I wouldn’t sacrifice readability for the slight performance boost. Not unless your app is around Google or Facebook’s level of traffic.