
The break, continue, goto, die, exit statements in PHP
In the previous lesson, we learned about the last loop, which is the foreach loop in PHP. In this lesson, we will explore some program flow control statements that allow us to stop loops and jump to specific positions in a PHP file.
The content of this lesson includes the following parts:
- The Break Statement
- The Continue Statement
- The Goto Statement
- The Die & Exit Statements
Contents
1. The Break Statement
The break
statement is commonly used to exit a loop even if the loop hasn’t finished executing.
Example:
<?php for ($i = 1; $i <= 100; $i++){ echo $i . ' '; if ($i == 20){ break; } } ?>
In this example, the loop iterates from 1 to 100, but it doesn’t complete all 100 iterations. When it reaches the 20th iteration (i.e., when the variable $i equals 20), the if condition evaluates to true, and the break statement inside the if block is executed, causing the loop to terminate.
The break statement can be used not only in the for loop but also in other loop structures such as while, do-while, and foreach loops to end the loop prematurely.
2. The Continue Statement
The continue
statement allows skipping the remaining code below it and jumping to the next iteration of the loop (it doesn’t exit the loop entirely like the break statement).
Example:
<?php for ($i = 1; $i <= 10; $i++){ if ($i == 5){ continue; } echo $i . ' '; } ?>
In this example, the for loop iterates from 1 to 10 and prints the numbers. However, the number 5 is missing in the output because when $i equals 5 (in the 5th iteration), the continue statement is encountered, which causes the program to move to the next iteration without executing the echo $i
statement.
Similarly, we can use the continue statement with other loop structures such as for, while, do-while, and foreach loops.
3. The Goto Statement
The goto statement is used to jump to a specific line of code.
Example:
<?php $a = 12; $b = 13; $c = $a + $b; echo $a; goto label_end; echo $b; label_end; ?>
In this example, under normal circumstances, both $a and $b would be displayed. However, in this case, only $a is displayed because the goto label_end
statement jumps the program to the label_end
line, causing the echo $b
statement to be skipped. label_end
is a label name that can be chosen arbitrarily.
It is generally advised not to use the goto statement because it makes the program harder to read and maintain.
4. The Die and Exit Statements
While the break and continue statements only affect loops, the die and exit statements affect the entire program. If you use either of these statements, the program will immediately stop, and the code below the die or exit statement will not be executed.
Example:
<?php echo '123'; die(); // or exit(); echo '456'; ?>
In this example, the output displayed on the screen is 123 because the echo '456'
statement is not executed.
5. Conclusion
In practice, we often use the break, continue, die, and exit statements, but the goto statement is rarely used because it makes the program more convoluted, harder to upgrade, and maintain. In the next lesson, we will explore functions in PHP.
Related Articales
Comments
-
Download and Install Vertrigo Server
May 25, 2023.84 views -
Declaring variables in PHP, common types of variables encountered
May 25, 2023.84 views -
Data types in PHP and their corresponding variable types
May 25, 2023.84 views -
Operators and expressions in PHP
May 25, 2023.84 views -
The if else statement in PHP
May 25, 2023.84 views -
The switch case statement in PHP
May 25, 2023.84 views -
The for loop in PHP
May 25, 2023.84 views -
While and do-while loops in PHP
May 25, 2023.84 views -
The foreach Loop in PHP
May 25, 2023.84 views -
The break, continue, goto, die, exit statements in PHP
May 25, 2023.84 views -
Building Functions in PHP
May 25, 2023.84 views -
Recursive Algorithm in PHP
May 25, 2023.84 views -
Bubble Sort Algorithm in PHP
May 25, 2023.84 views -
Linear Search Algorithm in PHP
May 25, 2023.84 views -
The technique of sentry placement in PHP
May 25, 2023.84 views -
Flagging technique in PHP
May 25, 2023.84 views -
Selection Sort Algorithm in PHP
May 25, 2023.84 views
Tags
© copyright 2021 Courseplus