How to Use Goto Statement in PHP

Introduction

We are describing how to use a Goto statement in PHP. Goto statements branch unconditionally from one place to another place in the program. The Goto statement is only available in the latest version of PHP 5.3 or higher PHP versions. We can say that a Goto statement jumps to another section of the program. The Goto Statement requires a label to identify the place where the branch is to be made. A label is any valid variable name and must be followed by a colon(:). A label is immediately placed before the statement where the control is to be transferred. A Goto is a very controversial programming statement.

Go-to-statement-in-php5.jpg

Syntax

<?php  

gotolabel;    

  ?>

 

Note

 

The program can be used anywhere in the program before or after the Goto statements. The program runs and when a Goto statement is encountered the flow of program will be transferred to the statement with the specified label. This will occur unconditionally.

 

Example1

 

<?php

  echo "Lets start with Go To"."<br>";

 goto read;

  echo "Skip the code with Goto jump";

 read:

  echo "Execute the code with Goto jump";

?>

 

Output

Go-to-statement-in-php1.jpg 

Example2

 

<?php

 

    goto b;

    echo 'Hello';

    b:

    echo 'This Name is Vinod';

 

?>

 

Output

Go-to-statement-in-php2.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all