The goto operator can be used to jump to a specified position in a program. The target location can be marked with a colon for the target name. The Goto in PHP has a certain limit and can only jump in the same file and scope, which means you can't jump out of a function or class method, and you can't skip to another function. You also can't jump into any loops or switch structures. A common use is to jump out of loops or switch, instead of multiple layers of break.
The usage is simple: goto followed by a flag with the target position, marked with a colon at the target location, as follows:
Copy Code code as follows:
<?php
Goto A;
Echo ' Foo ';//This sentence is skipped.
A:
Echo ' Bar ';
The above example outputs the result: 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 above example outputs the result: J hit 17
?>
Note:
The goto operator is valid only in PHP 5.3 and above.