Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does for(;;) work?

Tags:

syntax

php

I was looking through some code at work and found something I've not encountered before:

for (; ;)
{
   // Some code here
   break;
}

We call the function that contains this all the time, I only recently got in there to see how it works. Why does this work and is it documented somewhere?

It seems as though a while loop would have been more appropriate in this instance...

like image 801
Jeremy Harris Avatar asked Nov 30 '25 15:11

Jeremy Harris


1 Answers

It's essentially the same as while(true). It doesn't have any initialisation, doesn't change anything between iterations, and in the absence of anything to make it false the condition is assumed to be true.

like image 173
Niet the Dark Absol Avatar answered Dec 03 '25 07:12

Niet the Dark Absol



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!