PHP break and continue Statement

PHP break and continue statements help to control PHP loop statement. Using break and continue keywords, a PHP developer can dominate over PHP Loop Statement so easily. This article will explain how to use break and continue keywords to control looping in PHP programming.

break and continue statement in PHP

break and continue statement in PHP

break statement in PHP

PHP break keyword helps to terminate looping in PHP programming. When a loop faces break keyword in its body, it terminates its execution immediately. So, be careful to use break keyword inside a PHP Conditional Statement otherwise the loop body will execute no more.

A simple PHP program using break keyword is given below…


<?php
for($i=0;$i<10;$i++){
 if($i==5){
  break; 
 } 
 echo $i.'  '; 
} 
?>

Output: 0 1 2 3 4

From the above program you can see that for loop body is executing only five times although it has been declared to execute ten times.

continue statement in PHP

The continue statement starts a new loop without executing current loop body. When a loop finds continue keyword in its body, it stops the body execution and restarts the loop. So, be careful to use the continue keyword inside a conditional statement otherwise loop body will not be executed.

A simple PHP program using continue keyword is given below…


<?php
for($i=0;$i<10;$i++){
 if($i==5){
  continue;
 }
 echo $i.'  '; 
}
?>

Output: 0 1 2 3 4 6 7 8 9

In this example, when variable $i is equal to 5, the continue statement is executed. So, the execution of loop body is ignored and 5 is not printed with echo function.

In PHP application,  you will often face such a situation where continue statement must use. Say, you want to get all users without admin. Your database query will fetch all users available in our application. So, you need to filter the admin inside the loop body. If admin is appeared, the execution of loop body will be ignored. This algorithm can be applied with continue statement.

An example to find all users without admin is given below…


<?php 
$users=array("David","Bob","admin","Jack","Baily"); 
foreach($users as $user){
  if($user=='admin'){
   continue;
 } 
echo($user.'  ');
}
?>

Output: David Bob Jack Baily

From the output you can see that the admin user is not present because when user will be admin, continue statement will be executed and the loop body will be skipped.

goto statement in PHP

The goto statement helps to jump one execution label to another execution label. So, if you need to jump your program to a labeled statement, we can use goto statement. Say, you need to provide extra information for admin user and less information for other users. You can use goto statement to solve this problem.

A simple example using goto statement is given below…


<?php 
$user='David';
 if($user !="admin"){
 goto general_user;
 }
 echo "Hey, this admin area. General user cannot access here";
 general_user:
 echo "This area is common for both admin and general user";
?>

Output: This area is common for both admin and general user

From the output you can see that only the general user part is executing because user is not admin user. For admin user both echo function will be executed and both echo result will be printed. Try yourself with user equal to admin and verify result.

PHP break and continue statement has been explained in this article elaborately. If you face any problem to understand this article, feel free to contact with from Contact us page. I’ll try my best to stay with you. PHP function, an important topic in PHP programming, will be explained in next article. Spend some times to study the article and improve your idea about PHP function.

One Response
  1. Luis Lavric

Leave a Comment

Your email address will not be published. Required fields are marked *