PHP Syntax Statements Jumping break

 

- Info:

- break statement is used to end execution of looping statements or switch blocks.

- This means that you can use it to stop execution of for, foreach, while, do...while or switch blocks.

 

- Syntax:

break;

 

- Example:

- Create PHP Console Application Test.php.

 

Test.php

<?php

#BREAK. Output is: 12 since we break out of for loop when $i=3

for($i=1;$i<=4;$i++){

if($i==3) { break; }

print($i);

}

#BREAK: 1

$array = array("John"=>1, "Jill"=>5);

foreach ($array as $value) {

print($value);

break;

}

#BREAK. Output:12 since we break out of for loop when $i=3.

$i = 0;

while($i<=4){

$i++;

if($i==3) { break; }

print($i);

}

#BREAK. Output:12 since we break out of for loop when $i=3.

$i = 0;

do{

$i++;

if($i==3) { break; }

print $i;

} while($i<=4);

#BREAK. Output:01 since we break out of for loop after1.

$a = 0;

switch ($a) {

case 0:

print(0);

case 1:

print(1);

break;

case 5:

print(1);

default:

print("default");

}

?>