Disipal Network  
Go Back   Disipal Network > Tutorials > PHP


Reply
 
LinkBack Thread Tools Display Modes
Old 04-09-2009, 05:09 PM   #1 (permalink)
Administrator
 
Disipal's Avatar
 
Join Date: Mar 2009
Location: Athens (Greece)
Posts: 220
Send a message via MSN to Disipal Send a message via Skype™ to Disipal
Default Break and Continue

Continue and break are used within PHP loops (for, while, foreach, do-while and switch) to skip iterations and exit the loops respectively.
break

break is used to exit a loop and is used like this:
PHP Code:
<?php 
$array 
= array('pizza''burger''chips''pasta''kebab'); 

foreach(
$array as $food

    if(
$food == 'chips'
    { 
        break; 
    } 

    echo 
$food '<br />'


echo 
'End of foods'
?>
This will output:
PHP Code:
pizza
burger
End of foods 
So what is happening? When the current $food is equal to chips a break statement is encountered. This causes the foreach loop to stop executing and PHP continues from the end of the foreach loop at the echo statement which outputs "End of foods".
Nested loops

Using break alone will exit the current loop. break accepts an optional argument which tells it how many nested loops to exit from. For example:
PHP Code:
<?php 
$array 
= array('pizza''burger''chips''pasta''kebab'); 

for(
$i 1$i <= 2$i++) 

    foreach(
$array as $food
    { 
           if(
$food == 'chips'
           { 
                    break 
1
            } 

        echo 
$food '<br />'
    } 
     
    echo 
'End of foods ' $i ' <br />'

echo 
'End of foods'
?>
break 1 is the same as just using break alone. So this code will only exit the inner foreach loop giving an output like this:
PHP Code:
pizza
burger
End of foods 1 
pizza
burger
End of foods 2 
End of foods 
In order to exit both the inner foreach loop and outer for loops simply increase the argument of break to 2:
PHP Code:
<?php 
$array 
= array('pizza''burger''chips''pasta''kebab'); 

for(
$i 1$i <= 2$i++) 

    foreach(
$array as $food
    { 
           if(
$food == 'chips'
           { 
                    break 
2
            } 

        echo 
$food '<br />'
    } 
     
    echo 
'End of foods ' $i ' <br />'

echo 
'End of foods'
?>
This will output:
PHP Code:
pizza
burger
End of foods 
This time both the foreach and for loops are exited when the break is found and so the list of foods is only outputted once before continuing from the end of the for loop.
continue

When a continue statement is encountered in a loop the loop skips to the next iteration. Here is a simple example to demonstrate this behaviour:
PHP Code:
<?php 
for($i 1$i <= 5$i++) 

    if(
$i == 3
    { 
        continue; 
    } 
     
    echo 
$i '<br />'

?>
This PHP code outputs:
PHP Code:
1
2
4

When $i is 3 the continue statement causes the loop to skip any further code in the loop and return to the start.
Nested loops

Like break, the continue keyword can take an optional argument to skip nested loops. Take this example:
PHP Code:
<?php 
for($j 10$j <= 30$j += 10

    echo 
$j '<br />'
     
    for(
$i 1$i <= 5$i++) 
    {     
        if(
$i == 3
        { 
            continue 
2
        } 
     
        echo 
$i '<br />'
    } 

?>
This code results in this output:

PHP Code:
10
1
2
20
1
2
30
1

When the inner loop, $i, reaches 3 the continue statement, with an argument of 2, breaks out of both the inner and outer loops. PHP then returns to the start of the outer loop again.
Disipal is offline   Reply With Quote
Old 03-12-2010, 10:26 AM   #2 (permalink)
Junior Member
 
Join Date: Mar 2010
Posts: 1
Default Re: Break and Continue

One of my friends refer me to this forum and I really enjoyed it
__________________
read more about a+ certification and ccna security as well about mcts
Ashlyn is offline   Reply With Quote
Reply

Bookmarks

Tags
break, continue


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump


All times are GMT. The time now is 12:42 PM.

Powered by vBulletin® Version 3.8.4. Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Skin designed by Disipal