Break in PHP


Prev Tutorial Next Tutorial

Break Statement in PHP

PHP break statement breaks the execution of current for, while, do-while, switch and for-each loop. If you use break inside inner loop, it breaks the execution of inner loop only..

Syntax

jump statement;  
break;  

PHP Break inside loop

Example of break in php

<?php

for($i=1;$i<=10;$i++)
{  
echo "$i <br/>";  
if($i==5)
{  
break;  
}  
}  

?>

Output

1 2 3 4 5

PHP Break inside inner loop

Example break in php

<?php

for($i=1;$i<=3;$i++)
{
 for($j=1;$j<=3;$j++)
 {
  echo "$i   $j<br/>";
  if($i==2 && $j==2)
  {
   break;
  }
 }
}

?>

Output

1 1 1 2 1 3 2 1 2 2 3 1 3 2 3 3

PHP Break: inside switch statement

Example break in php

<?php

$num=5;    
switch($num){    
case 1:    
echo("Monday");    
break;    
case 2:    
echo("Tuesday");    
break;    
case 3:    
echo("Wednasday");    
break;
case 4:    
echo("Thrusday");    
break;
case 5:    
echo("Friday");    
break;
case 6:    
echo("Sateday");    
break;
case 7:    
echo("Sunday");    
break;

?>

Output

Friday

Prev Tutorial Next Tutorial

Google Advertisements
Download Projects

Google Adsense Advertisements

Buy 3 Year Old Domain, Tutorialday.com, SeosmoTools.com @$200

Buy This Ad Space @$50 per Month, Ad Size 300X250 Contact on: hitesh.xc@gmail.com WhatsApp no. 8882538826

Yahoo Advertisements