PHP break, continue and goto Statements

PHP break and continue statements are used to dominate or control PHP loop statements and PHP goto statement is used to jump one execution label to another execution label. There is no alternative to control PHP for loop, while loop and foreach loop without PHP break and PHP continue statements. So, PHP programmer should learn PHP break and continue statements deeply otherwise he/she cannot be expert enough on PHP loop statements. In my previous article I explained PHP loop statements and in this article I will discuss PHP break, continue and goto statements elaborately.

break and continue statements in PHP
break and continue statements in PHP

break Statement in PHP

PHP break statement can terminate PHP loop statements immediately. When for loop, foreach loop or while loop faces break keyword, it terminates its loop execution and program control goes to next statement. So, we should be careful enough to place break keyword within a PHP conditional statement. Otherwise PHP loop statements will not be executed more than once.    

PHP break Statement Example

The following program is a tiny example of PHP while break and the break keyword is being used within conditional statement.

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

Output

0 1 2 3 4

From the above program we can see that while loop is being executed only five times due to face break statement although it has been declared to execute ten times.

continue Statement in PHP

PHP continue statement starts a new loop without executing current loop body. When a loop finds continue keyword, it stops the body execution and restarts the loop. So, we should be careful to use PHP continue inside a conditional statement otherwise loop body will never be executed.

PHP continue Statement Example

The following program is a tiny example of PHP while continue where the continue keyword is being used within PHP conditional statement. 

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

Output

0 1 2 3 4 6 7 8 9

From the above program we can see that 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 real PHP application we may face such a situation where continue statement must use. For example, we want to get all users without admin. Our database query will fetch all users available in our application. So, we need to filter the admin user inside the loop body. If admin is appeared, the execution of loop body will be ignored. This algorithm can be applied with the PHP continue statement.

The following program is a example of PHP foreach continue where the body of foreach loop is being ignored with continue statement when user equal to admin. 

<?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 we 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

PHP goto statement is used to jump one execution label to another execution label. If we need to jump our program to a labeled statement, we can use goto statement. For example, we need to provide extra information for admin user and less information for other users. In this case we can use goto statement to solve this problem.

PHP goto Statement Example

The following program is a simple example of PHP goto statement where admin user will get extra information but general user will get less information.

<?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 we can see that only the general user part is executing because the user is not admin user. For admin user both echo functions will be executed and both echo result will be printed. Try yourself with user equal to admin and verify result.

PHP break, continue and goto statement has been explained in this article. I hope you will now be able to PHP break, continue and goto statements whenever requires. However, if you face any confusion to use break, continue and goto statement properly, feel free to contact me from Contact page. I will try my best to stay with you.

Related post:

php-break-continue-and-goto-statements

ABU SAYEED

I am a system administrator and like to share knowledge that I am learning from my daily experience. I usually work on MikroTik, Redhat/CentOS Linux, Windows Server, physical server and storage, virtual technology and other system related topics. Web development is also my favorite topic and love to work with PHP, HTML, CSS, JavaScript, AJAX, jQuery, WordPress and CodeIgniter framework. Follow Me: Facebook, Twitter and Linkedin.

Your name can also be listed here. Have an IT topic? Submit it here to become a iTechSheet author.

One thought on “PHP break, continue and goto Statements

Leave a Reply

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

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.