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

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

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

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) |
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

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


Comments
Join the conversation! Your thoughts help the community grow.