While reading through the great online PHP tutorials of Paul Hudson he said
Perhaps surprisingly, infinite loops can sometimes be helpful in your scripts. As infinite loops never terminate without outside influence, the most popular way to use them is to break out of the loop and/or exit the script entirely from within the loop whenever a condition is matched. You can also rely on user input to terminate the loop - for example, if you are writing a program to accept people typing in data for as long as they want, it just would not work to have the script loop 30,000 times or even 300,000,000 times. Instead, the code should loop forever, constantly accepting user input until the user ends the program by pressing Ctrl-C.
Would you please give me a simple running example of how to use infinite loops in PHP ?
I'm going to disagree with the other answers so far, and suggest that, if you're being careful about things, they never have a place.
There is always some condition in which you want to shut down, so at the very least, it should be while(test if shutdown not requested) or while(still able to meaningfully run)
I think practically there are times when people don't use the condition and rely on things like sigint to php to terminate, but this is not best practice in my opinion, even if it works.
The risk of putting the test inside the loop and breaking if it fails is that it makes it easier for the code to be modified in the future to inadvertently create an endless loop. For example, you might wrap the contents of the while loop inside another loop, and then all of a sudden the break statement doesn't get you out of the while...
for(;;) or while(1) should be avoided whenever possible, and it's almost always possible.
Paul Biggar has posted a make script for LaTeX projects which uses an infinite loop to run in the background and continually tries to rebuild the LaTeX sources.
The only way to terminate the script is to kill it externally (e.g. using Ctrl+C).
(Granted, not PHP (Bash, actually) but the same script could well be implemented in PHP instead.)
There are many ways to use infinite loops, here is an example of an infinite loop to get 100 random numbers between 1 and 200
I think a point is being missed... there aren't really infinite loops (you would be stuck in them forever), rather
while(true){...}
and co are useful when you have non trivial exit conditions (e.g. those coming from a third-party library, or a limit which would take a lot of time to calculate but can be worked out incrementally inside of the loop, or something relying on user input).It shouldn't be surprising that not every loop can be concisely stated as a
for
,while
ordo
loop without usingbreak
.Infinite loops are one of those tools you keep in a separate toolbox that doesn't get opened much as it is a tool of (almost) last resort.
The best use I have found for them is with state machines or loops that are approaching state machines. This is because the exit condition is usually quite complex and cannot be put at the top or the bottom of the loop.
If you implemented a socket server (taken from: http://devzone.zend.com/article/1086 ):