Break Statement in PHP : Tutorial


Break statement is used to interrupt the flow of execution in a program. It is used with loops (for or while) and switch structure to end the execution.

Syntax :
break;
Example 1:
<?php

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

?>
Output

1 2 3 4 5
In the above example,

When $i value becomes 6 the program flow enters in else block and break statement is executed and control exits the for loop.

Example 2:
<?php

$number = 1;
while ($number <=10)
{
    if ($number == 6)
    {
        break;
    }
    echo $number . "<br>";
    $number = $number+1;
}
?>
Output

1 2 3 4 5
In the above example,

When $i value becomes 6 the program flow enters in else block and break statement is executed and control exits the while loop.