php goto statement

break statement is used to control the program execution for for, foreach, while, do - while loops and switch case.

Break syntax

<?php
for(loop)
{
  if (condition)
  {
  break;
  }
}
?>

Break Example


<?php
$counter = -4;
for ( ; $counter <= 10; $counter++ )
{
   if ( $counter == 0 )
   break;
  $temp = 4000/$counter;
   print "4000 divided by $counter is... $temp<br>";
}
?>

Output:

4000 divided by -4 is... -1000
4000 divided by -3 is... -1333.33333333
4000 divided by -2 is... -2000
4000 divided by -1 is... -4000

In the above example We use an if statement to test the value of $counter. If it is equal to zero, the break statement immediately halts execution of the code block, and program flow continues after the for statement.

You can omit any of the expressions from a for statement, but you must remember to retain the semicolons.





Content