Continue and Break Statement in PHP

Introduction

"Continue is used within looping structures to skip the rest of the current loop iteration" and continue execution at the condition evaluation and proceed to the beginning of the next iteration. The current functionality treats switch structures as looping in regards to continue. It has the same effect as a break.
The following code is an example; in this example you learn about the Continue statement.

Syntax

<?php
Loop (While, do-while, for,)
{
conditions
{
continue;//continue statement
}
code executed;
}
?>

Example of simple continue statement

<?php

echo '<h2><b>Simple Example of continue statement:</b></h2><br />';

for($i=1; $i<=5; $i++)

{

  if($i==7)

 {

   continue;//continue statement

 }

   echo "Value:". "$i."."<br>"."<br>";

}

?>

 

Output

Simple Continue statement in php.jpg

 

Example of Array with continue statement

 

<?php

echo '</h2><b>Example of using Array With Continue statement:</b></h2><br/><br/><br/>';

 $i= array(0=>'Apple',1=>'Pomegranate',' Banana', 'Mango', 'Guava', 'Orange', 'Pineapple');

foreach ($i as $v)

{

   if ($v=='Banana')

   {

     continue;

   }

   echo "The fruit is "."$v";

   echo "<br>"."<br>";

}

?>

 

Output

Continue statement using Array.jpg
 

Example of odd number with continue statement

 

<?php

echo '<h2><b>Example of odd number With continue statement:</b></h2><br />'

$x=1; 

 

while ($x<=10) 

if (($x % 2)==0) 

$x++; 

continue

else 

echo $x.'<br />'

$x++; 

}

Output

Example of odd number with Continue statement.jpg

Break It takes you out from a loop and switch case. The break statement can also be used to jump out of a loop. The break statement is frequently used to terminate the processing of a particular case within a switch statement. Within nested statements, the break statement terminates only the do, for, foreach, switch, or while statement that immediately encloses it. You can use a return or goto statement to transfer control elsewhere out of the nested structure. This example illustrates the break statements.

Syntax

switch (n)
{
case text1:
code executed if n=text1;
break;
case text2:
code executed if n=text2;
break;
default:
code executed if n is different from both text1 and text2;
}

Example of simple Break statement

<?php

$i= array(0=>'Apple',1=>'Pomegranate','Banana', 'Mango', 'Guava', 'Orange', 'Pineapple');

foreach($i as $v)

 {

  if($v=='Mango')

    {

    break;//Break statement

    }

   echo "Value:". "$v."."<br>"."<br>";

  }

?>

 

Output

Simple break statement.jpg

 

Example of Switch case with break statement

 

<?php

 

switch(5) {

    case 1:

        echo Apple . "\n";

               break;

    case 2:

        echo Pomegranate . "\n";

        break;

 

    case 3:

        echo Banana . "\n";

               break;

    case 4:

        echo Mango . "\n";

               break;

    case 5:

        echo Guava . "\n";

               break;

    default:

        echo Orange . "\n";

         break;

    default:

        echo Pineapple . "\n";

    default:

        echo 8 . "\n";

}

?>

Output

Switch-case-using-continue-statement.jpg