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

2.6.12     Special loop keywords

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

There are two special keywords you can use with loops, and they are "break" and "continue". We already used break previously when we looked at case switching - it is used there to exit a switch/case block, and it has the same effect with loops. When used inside loops in order to manipulate the loop behaviour: break causes PHP to exit the loop and carry on immediately after it, and continue causes PHP to skip the rest of the current loop iteration and go onto the next.

For example:

<?php
    
for ($i = 1; $i < 10; $i = $i + 1) {
        if (
$i == 3) continue;
        if (
$i == 7) break;
        print
"Number $i\n";
    }
?>

That is a modified version of our original for loop script. This time the output is this:

Number 1
Number 2
Number 4
Number 5
Number 6

Note that number 3 is missed out entirely, and the script exits after number 6. The reason for this is because of the two if statements - if the current number is 3, "continue" is used to skip the rest of that iteration and go on to number 4. Also, if the number is 7, "break" is used to exit the loop altogether.





<< 2.6.11 Infinite loops   2.6.13 Loops within loops >>
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
Paja - Czech Republic - 06 Sep 2008

To Newbie:
holding to the author´s example you can do much better when you change the condition of the loop:
$i<7
You will get the same result, you will have saved one line of code and your script will be simpler.
It is a "pig style" to say "do until 10" and later in the code you add "do until 7". Understand?

A newbie - 06 Sep 2008

Posters below, can you elaborate on why it's good practice to avoid break?

A PHP User - 06 Sep 2008

print "Number $i\n";
is not working

print "Number $i<br>";

is working

A PHP User - 06 Sep 2008

I've seen times when using a break can really save yourself a lot confusing code, but never the less I must agree with the above reader. It's good practice to avoid break.

A PHP User - 06 Sep 2008

Each one has your own code style. But i think it is a good programming practice do not break "for loops" neither "while loops" using the break keyword. So, this is the kind of stuff you know it exists but usually don`t use. Pay attention to that guys :).

Excellent book!



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 zero plus four?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow