PHP break statement



Suppose we need to terminate the loop then we can use break statement. In PHP break statement is used for the termination of the loop statement and switch case statement. Syntax of the break statement is shown below –

break;

#1. PHP Break Statement Example :

Example of the break statement is used below –

<html>
<body>

<?php
$array = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 0);
foreach( $array as $value )
{
  if( $value == 3 )
	break;

  echo "Value is $value <br />";
}
?>

</body>
</html>

Output :

Value is 1 
Value is 2 

Explanation :

We have specified condition inside the loop to check the current value of the array.

if( $value == 3 )
    break;

If the current value of the array is equal to 3 then we break statement gets executed which will skip all the remaining iterations.

Flow diagram :

php_break_statement