The break, continue, goto, die, exit statements in PHP

The break, continue, goto, die, exit statements in PHP

16/05/2023
28 Lượt xem

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

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.

5/5 - (3 votes)
Download and Install Vertrigo Server
16/05/2023
28 Lượt xem
Operators and expressions in PHP
16/05/2023
28 Lượt xem
The if else statement in PHP
16/05/2023
28 Lượt xem
The switch case statement in PHP
16/05/2023
28 Lượt xem
The for loop in PHP
16/05/2023
28 Lượt xem
While and do-while loops in PHP
16/05/2023
28 Lượt xem
The foreach Loop in PHP
16/05/2023
28 Lượt xem
Building Functions in PHP
16/05/2023
28 Lượt xem
Recursive Algorithm in PHP
16/05/2023
28 Lượt xem
Bubble Sort Algorithm in PHP
16/05/2023
28 Lượt xem
Linear Search Algorithm in PHP
16/05/2023
28 Lượt xem
The technique of sentry placement in PHP
16/05/2023
28 Lượt xem
Flagging technique in PHP
16/05/2023
28 Lượt xem
Selection Sort Algorithm in PHP
16/05/2023
28 Lượt xem

Related Articales

Comments

© copyright 2021 Courseplus