The goto operator can be used to jump to a specified location in the program. The target location can be marked with the target name followed by a colon. Goto in PHP has certain restrictions and can only jump within the same file and scope. This means that you cannot jump out of a function or class method, nor can you jump into another function. You also cannot jump into any loops or switch structures. Common usage is to break out of a loop or switch, which can replace multiple levels of break.
Usage is very simple: put the mark of the target position after goto, and mark the target position with the target name plus a colon, as follows:
//The output result of the above example is: Bar;
for($i=0,$j=50; $i<100; $i++) {
while($j-- ) {
if($j==17) goto end;
}
}
echo "i = $i";
end:
echo 'j hit 17';
//The output result of the above example is: j hit 17
?>
Note:
The goto operator is only available in PHP 5.3 and above.