scriptygoddess

25 Oct, 2004

Continue vs. Break (in while loops)

Posted by: Jennifer In: Lessons learned

(note to self type post)
Using "continue" in the middle of a while loop will skip the current iteration and go back to the beginning of the loop (checking the while value again).

Using "break" in the middle of a while loop will break the loop entirely. (no more looping)

So this code

<?php
$i = 0;
while ($i < 5) {
$i++;
if ($i%2) {
echo "value = " .$i . " – inside if<br>";
continue;
}
echo "value = " .$i . " – outside if<br>";
}
?>

Will produce this:
value = 1 – inside if
value = 2 – outside if
value = 3 – inside if
value = 4 – outside if
value = 5 – inside if

Where as this

<?php
$i = 0;
while ($i < 5) {
$i++;
if ($i%2) {
echo "value = " .$i . " – inside if<br>";
break;
}
echo "value = " .$i . " – outside if<br>";
}
?>

Will produce this:
value = 1 – inside if

See more about while loops here.

Related posts:

  1. Playing with Post Attachments I've been doing a ton of sites recently using WordPress...

Related posts brought to you by Yet Another Related Posts Plugin.

No Responses to "Continue vs. Break (in while loops)"

Comments are closed.

Featured Sponsors


  • Michael: You can use get_header(2) in your case. The filename of your custom header has to be header-2.php. The problem with include(your_file.php) is, all
  • cliff: hi wonder if you can help me, pls i designed www.kouga.mobi using dreamweaver CS3 and now want to make the phonenumbers into links so that if you
  • jerey: how do i rewrite this because it tried RewriteEngine on #Options +FollowSymlinks RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FIL

About


Advertisements