Hudzilla.org - the homepage of Paul Hudson
Contents > Introducing PHP > How PHP is written Wish List | Report Bug | About Me ]

2.6.11     Infinite loops

This is NOT the latest copy of this book; click here for the latest version.

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.

While you are learning PHP, it is almost certainly best you steer clear of infinite loops, as they can really cause problems in the first few weeks. After that, try playing around with them to see how they can actually help you make your scripts better. Having said that, you will almost certainly make a few loops carry on forever simply by accident!

If you want to try them out, here are the most common examples of infinite loops:

<?php
    
while(1) {
        print
"In loop!\n";
    }
?>

As "1" also evaluates to true, that loop will continue on forever. Many people also like to write their infinite loops like this:

<?php
    
for (;;) {
        print
"In loop!\n";
    }
?>

In that example, the for loop is missing the declaration, condition, and action parts, meaning that it will always loop.





<< 2.6.10 Loops   2.6.12 Special loop keywords >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
A PHP SOOPR NOOB - 07 Sep 2008

TRUE and FALSE are cas INsesitive, u can write them as you see fit.

kewlceo - 07 Sep 2008

Alex said:
"I don't understand why an infinite loop could be useful in a real life situation."

In a real world situation, the infinite loop is usually the main control loop, where the program is waiting for the user's input.

Hemlock - 07 Sep 2008

I remember an old basic program that i used an infinate loop to keep from having to press return after answering a [y]es or [n]o question it woud loop infantly untill the variable was set to either y or n.

Alex - 07 Sep 2008

Sorry, maybe a stupid question, but be patient I'm a newbe.
I don't understand why an infinite loop could be useful in a real life situation.
Anyone can help?

Thanks

Skyblaze - 07 Sep 2008

"TRUE" is always written in uppercase?

jlx - 07 Sep 2008

For better reading i always use

while (TRUE)
{
//running
}



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is four plus eight?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow