PHP Jumping Statements

PHP Jumping Statements are used to jump out from a loop or jump the controller from one line to another line, inside a program.

PHP Break Statement

PHP break is a keyword, which is used to terminate the execution of a loop.

Example 1: PHP Break Statement

<html>
  <body>
      <?php
          for($i=1; $i<=10; $i++)
          {
              if($i==5)
                  break;
              echo "For Loop $i<br>";
          }
      ?>
  </body>
</html>
For Loop 1
For Loop 2
For Loop 3
For Loop 4

PHP Continue Statement

PHP Continue statement is used to skip the current iteration of a loop but it does not terminates the loop.

Example 2: PHP Continue Statement

<html>
  <body>
    <?php
        for($i=1; $i<=10; $i++)
        {
            if($i==5)
                continue;
            echo "For Loop $i<br>";
        }
    ?>
  </body>
</html>
For Loop 1
For Loop 2
For Loop 3
For Loop 4
For Loop 6
For Loop 7
For Loop 8
For Loop 9
For Loop 10

It will skip 5th statement

PHP goto Statement

PHP goto statement is also a jumping statement, and it is use to jump any section or any line in the program using user defined label.

Example 3: PHP goto Statement

<?php
    echo "<div>First Line</div>";
    goto bottom;
    echo "<div>Second Line</div>";
    bottom:
    echo "<div>Third Line</div>";
?>
First Line
Third Line

Example 4: PHP goto Statement

<?php
    $i=1;
    top:
    echo "<div>".$i++."</div>";
    if($i<6)
        goto top;
?>
1
2
3
4
5

Example 5: PHP goto statement inside loop.

<?php
    for ($i=1; $i<=5; $i++)
    {
        echo $i."<br>";
        goto outloop;
    }
    outloop:
    echo "Program ends";
?>
1
Program ends

Example 6: PHP goto statement inside loop.

<?php
    goto inloop;
    for ($x=1; $x<=5; $x++)
    {
        inloop:
        echo $x."<br>";
    }
?>
Fatal error: 'goto' into loop or switch statement is disallowed in script on line 2