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 elaborately and in this article I am going to 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 performed properly.    

PHP break Statement Example

The following program is a tiny example of PHP while break statement. Here, the break keyword is being used to terminate a while loop under a certain within condition.

<?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 the 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 the 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 and desired output cannot be found.

PHP continue Statement Example

The following program is a tiny example of PHP while continue statement where the continue keyword is being used to skip a while loop body under a certain condition. 

<?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 being executed. So, the execution of loop body is ignored and 5 is not printed with the 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 a foreach loop is being ignored with the 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 the admin user, both echo functions will be executed and both echo result will be printed. Try yourself with user equal to admin and verify the 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 discuss in comment or contact me from Contact page. I will try my best to stay with you.

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, Tailwindcss, Bootstrap, JavaScript, jQuery, Vue.js, WordPress and Laravel framework. Follow Me: Facebook, Twitter and Linkedin.

Related Posts

Laravel First Time Installation on Windows 10/11

Laravel is one of the best full stack frameworks used to develop modern web application simply and securely. Day by day PHP developers are absorbing Laravel. So,…

PHP Delete Array Element with Built-in Function

Array is an important and mostly used data type in PHP programming. PHP programmer can manually add element to an array or delete element from an array…

PHP Add Element to Array with Built-in Functions

Array plays an important role in PHP programming. It is one of the most used data structures in PHP programming. So, it is better to have a…

PHP Array Explanation

Array is one of the most important and basic terms in PHP Language. Almost all the PHP program uses array data structure. So, those who want to…

Function Call by Value and Call by Reference in PHP

More than thousands of built-in functions proves the power of PHP Language. Besides built-in function PHP allows to create user defined function. How to create user defined…

PHP Variable Usage inside of Function

Besides more than thousands of built-in functions PHP provides facility to define custom/user defined function. How to define and call a user defined function was discussed in…

This Post Has One Comment

Leave a Reply

Your email address will not be published.

*

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