break

Used to exit out of a loop code block.

 

Next statement following the loop will then be executed.

 

<?php
	$myVar = 0 ;

	while($myVar < 10) {

	if ($myVar == 7) break;

		echo "\$myVar is currently $myVar<br>" ;

		$myVar++ ;
	}
	echo "The if condition executed the break when \$myVar evaluated to 7" ;
?>

Displays:

$myVar is currently 0
$myVar is currently 1
$myVar is currently 2
$myVar is currently 3
$myVar is currently 4
$myVar is currently 5
$myVar is currently 6
The if condition executed the break when $myVar evaluated to 7

Leave a Reply