Infinite loops

We all know what a loop is: a programming language statement that allows us to repeat the execution of a piece of code until a certain ending condition is met. If the ending condition is never met or it does not exist at all, the loop never ends: that’s an infinite loop (also known as an endless loop).

To willingly produce an infinite loop in PHP you can use a while statement with true or 1 instead of the ending condition:

while (true)
	{
	}

or an empty for statement:

for (;;)
	{
	}

When in need of very long loops we sometime use infinite loops in our code but more frequently we use pseudo-infinite loops, which are loops that appear to be infinite but eventually end sometime:

while (true)
	{
	//some code
	if (date('m-d')>'12-25')
		break;
	}
//appears to be infinite but force ends on Christmas :)

Tip: you may find force ending conditions very useful when debugging code.

Leave a Reply

  

  

  

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Categories

Archives

Calendar

September 2014
M T W T F S S
« Jul    
1234567
891011121314
15161718192021
22232425262728
2930