PHP Goto


 

PHP Goto

In this tutorial we will study about goto statement, goto statement helps us to move from one section to another section in a file, in PHP there are few restrictions are present for goto statement which is discussed in the current tutorial. Examples in this tutorial will make it more clear.

In this tutorial we will study about goto statement, goto statement helps us to move from one section to another section in a file, in PHP there are few restrictions are present for goto statement which is discussed in the current tutorial. Examples in this tutorial will make it more clear.

Goto Statement:

The goto operator is used to jump to another line or section in the program. To move to another section we need to specify a name after goto statement and to declare a target we need to give a specific name followed by a colon. In PHP there are few constraints in goto statements, like we can not move from one file/method to another file/method or neither you can move inside a loop/switch structure using goto statement. You may jump out of these constructs using goto statement.

Example:

<?php

echo "First Statement <br/>";

goto rd;

echo "Second statement <br/>";

rd:

echo "Third statement";

?>

Output:

First Statement
Third statement

Example:

<?php

goto mid;

$a=12;

while($a>0){

$a--;

mid:

echo "Inside the goto statement";

}

?>

Output:


Fatal error: 'goto' into loop or switch statement is disallowed in C:\xampp\htdocs\PHP-AJAX\variable.php on line 2

Example:

<?php

0

$a=12;

while($a>0){

$a--;

1

if($a==6)

{

goto mid;

2

}

else

{

3

echo "<br/>$a";

}

4

}

mid:

echo "<br/>Inside the goto statement";

5

?>

Output:

6

11
10
9
8
7
Inside the goto statement

Ads