Does anyone know in which cases may infinite loop be useful in PHP?
Example :
<?php
while(true)
{
#..
}
?>
Does anyone know in which cases may infinite loop be useful in PHP?
Example :
<?php
while(true)
{
#..
}
?>
Few Use cases.
1) If you are writing a program to allow input data for as long as the user wants, 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.
2) If you have some background process monitoring your servers continuously.
3) Possibly a very good usage is when your server script is listening to a socket for connections.
4) Video Game programming use them heavily.
I differ from some of the above answers posted above which say that "infinite loops are useless".
http://devzone.zend.com/209/writing-socket-servers-in-php/ - An example of infinite loops been put to use.
Hey these are very essential in case you want to create a service kind of a thing, may be you want to monitor the server space ,i mean you want to see if there any new files added etc. you can then use such a loop and monitor 24*7.
I use it quite often in c++,in php too it can be used.
This isn't really PHP specific. Infinite loops are useful when you don't know in advance amount of work you need to do.
It is almost useless. True is that it can be used with break as Oleg V. Volkov mentioned but You'll have to add condition into block but while is a loop with condition itself. From my point of view it is wrong usage.
Infinite loop allows you to iterate through unknown set of data, until terminated (brake). or you are performing some actions that need to run until interrupted by user or else. It all depends on what you need to do and how would you tackle the problem.
Two cases :