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
$a=12; while($a>0){ $a--; if($a==6){
goto mid;
}
else { echo "<br/>$a";}
}
mid:
echo "<br/>Inside the goto statement";?>
Output:
11
10
9
8
7
Inside the goto statement