Friday, August 29, 2025
HomeLanguagesPHP goto Statement

PHP goto Statement

The goto statement is used to jump to another section of a program. It is sometimes referred to as an unconditional jump statement. The goto statement can be used to jump from anywhere to anywhere within a function.

Flowchart of goto statement:

 

Syntax:

statement_1;

if (expr)
       goto label;
statement_2;
statement_3;

label: statement_4;

Example 1: The following code demonstrates the goto statement.

PHP




<?php
  
    // Function to check even or not
    function checkEvenOrNot($num) {
        if ($num % 2 == 0)
          
            // Jump to even
            goto even;
        else
            // Jump to odd
            goto odd;
  
    even:
        echo $num . " is even";
          
        // Return if even
        return;
    odd:
        echo $num . " is odd";
    }
  
    $num = 26;
    checkEvenOrNot($num);
  
?>


Output

26 is even

Example 2: This is another code to demonstrate the goto statement of PHP.

PHP




<?php
  
    // Function to print numbers
    // from 1 to 10
    function printNumbers() {
        $n = 1;
          
        label:
            echo $n . ' ';
            $n++;
            if ($n <= 10)
                goto label;
    }
  
    printNumbers();
  
?>


Output

1 2 3 4 5 6 7 8 9 10 

Reference: https://www.php.net/manual/en/control-structures.goto.php

RELATED ARTICLES

Most Popular

Dominic
32249 POSTS0 COMMENTS
Milvus
80 POSTS0 COMMENTS
Nango Kala
6617 POSTS0 COMMENTS
Nicole Veronica
11789 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11838 POSTS0 COMMENTS
Shaida Kate Naidoo
6731 POSTS0 COMMENTS
Ted Musemwa
7011 POSTS0 COMMENTS
Thapelo Manthata
6688 POSTS0 COMMENTS
Umr Jansen
6701 POSTS0 COMMENTS