Info
Open the page on your phone

What is the difference between BREAK and CONTINUE statements?

In the PHP programming language, the `break` and `continue` statements are used to control the execution of loops. The main difference between them lies in their functionality:

`break`:

  • When the break statement is encountered inside a loop (e.g.,` for`, `while`, `do-while`), the execution of that loop is immediately terminated, and the program control goes beyond the loop.
  • In other contexts (e.g., within a switch statement), `break` can also be used to exit constructs.
  •                         
    for ($i = 0; $i < 10; $i++) {
        if ($i == 5) {
            break; // Terminate the loop if $i equals 5
        }
        echo $i . '<br>';
    }
                            
                        

    `continue`:

  • When the continue statement is encountered inside a loop, the remaining code in that loop after continue is skipped, and the execution of the loop proceeds to the next iteration.
  • If `continue` is within a nested loop, it only affects the innermost loop where it is placed.
  •                         
    for ($i = 0; $i < 10; $i++) {
        if ($i % 2 == 0) {
            continue; // Skip even values of $i
        }
        echo $i . '<br>';
    }
                            
                        

    In the provided examples, `break` is used to exit the loop when a certain condition is met, while `continue` is used to skip specific iterations of the loop.