PHP Break , Continue and Goto

PHP Break , Continue and Goto

PHP Break, Continue, and Goto

In PHP, several control flow statements can be used to alter the flow of execution within loops or conditional statements. These include break, continue, and goto. Here's how each one works:

1. PHP break Statement

The break statement is used to terminate the execution of a loop or a switch statement prematurely. When a break is encountered, the loop or switch block is immediately exited, and the program continues executing the code following the loop or switch.

Syntax:

break; // Exits the loop or switch

Example of break in a loop:

for ($i = 1; $i <= 10; $i++) { if ($i == 5) { break; // Exit the loop when $i equals 5 } echo "$i<br>"; }

Output:

1 2 3 4

Explanation:

  • The loop starts from $i = 1 and continues.
  • When $i reaches 5, the break statement is executed, and the loop exits, so it doesn't print numbers greater than 4.

2. PHP continue Statement

The a continue statement is used to skip the current iteration of a loop and move to the next iteration. When the continue statement is executed, the remaining code in the current iteration is skipped, and the loop proceeds with the next iteration.

Syntax:

continue; // Skip the current iteration and continue with the next one

Example of continue in a loop:

for ($i = 1; $i <= 10; $i++) { if ($i == 5) { continue; // Skip when $i equals 5 } echo "$i<br>"; }

Output:

1 2 3 4 6 7 8 9 10

Explanation:

  • The loop starts from $i = 1 and continues.
  • When $i is 5, the continue statement is executed, causing the loop to skip that iteration and continue with $i = 6.
  • As a result, the number 5 is not printed.

3. PHP goto Statement

The a goto statement is used to jump to another part of the code. It's considered controversial because it makes the flow of the program harder to follow, so its usage is generally discouraged. goto allows you to jump to a specific label in the code.

Syntax:

goto label; // Jumps to the label ... label: // Code to be executed after the jump

Example of goto:

$i = 0; start: $i++; echo "Value of i: $i<br>"; if ($i < 5) { goto start; // Jump back to the 'start' label } echo "Done!";

Output:

Value of i: 1 Value of i: 2 Value of i: 3 Value of i: 4 Value of i: 5 Done!

Explanation:

  • The loop starts with $i = 0 and jumps to the start label.
  • The value of $i is incremented and printed.
  • If $i is less than 5, it jumps back to the start label and continues the loop.
  • Once $i reaches 5, the loop stops, and the program prints "Done!".

Flowchart for break, continue, and goto:

Break:

+----------------------------+ | Start of the loop | +----------------------------+ | v +----------------------------+ | Check loop condition | +----------------------------+ | v +----------------------------+ | Execute loop code | +----------------------------+ | v +----------------------------+ | Break condition met? | +----------------------------+ | Yes | No | v +----------------------------+ | Exit the loop | +----------------------------+

Continue:

+----------------------------+ | Start of the loop | +----------------------------+ | v +----------------------------+ | Check loop condition | +----------------------------+ | v +----------------------------+ | Execute loop code | +----------------------------+ | v +----------------------------+ | Continue condition met? | +----------------------------+ | Yes | No | v +----------------------------+ | Skip current iteration | +----------------------------+ | v +----------------------------+ | Go to next iteration | +----------------------------+

Goto:

+----------------------------+ | Start of the code | +----------------------------+ | v +----------------------------+ | Check for goto condition | +----------------------------+ | v +----------------------------+ | Jump to the label | +----------------------------+ | v +----------------------------+ | Execute code after label | +----------------------------+

When to Use break, continue, and goto:

  • break:

    • Used when you need to exit a loop or a switch block prematurely, such as when a condition is met that no longer requires further iterations.
  • continue:

    • Used when you want to skip the current iteration of a loop but continue with the next iteration. It is helpful when certain conditions should exclude specific iterations.
  • goto:

    • Use sparingly as it can lead to code that is difficult to follow. It is usually better to use proper control flow like loops and functions. goto is typically used for error handling in some cases, but its use is often discouraged in modern programming practices.

Important Notes:

  • break and continue work well within loops and switch statements.
  • goto should generally be avoided in favor of cleaner control structures.
Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close