Table of Contents

    Continuing Loops in PHP: Techniques and Best Practices

    Continuing Loops in PHP: Techniques and Best Practices

    While you can break out of a loop completely with the break keyword, there is another keyword used when working with loops - the continue keyword. Using the continue keyword in a loop will stop the loop at some point and continue with the next iteration of the loop from the beginning of it.

    Syntax:

    
    
    <?php
    for($a = 0; $a < 10; $a++){
    
    if($a == 5){
    continue;
    }
    
    print $a . "<br />";
    }
    ?>
    
    

    In the above example, the for loop is set to iterate 9 times and print the current value of the variable a during each iteration. The if statement within the loop states that when the variable a is equal to 5, stop the loop and continue with the next iteration of the loop from the beginning of it. For this reason, all the numbers except the number 5 are printed.

    Output:

    This will produce the following result

    
    
    0
    1
    2
    3
    4
    6
    7
    8
    9