The goto operator can be used to jump to a specified position in the program. You can add a colon to the target location. PHP has certain restrictions on goto. You can only jump to the same file and scope. That is to say, you cannot jump out of one function or class method, or jump to another function. You cannot jump into any loop or switch structure. A common usage is to jump out of a loop or switch, which can replace multiple levels of break.
The usage is simple: The goto is followed by the target position mark, and the target name is marked with a colon on the target location, as shown below:
Copy codeThe Code is as follows: <? Php
Goto;
Echo 'foo'; // This sentence is skipped
A:
Echo 'bar ';
// The output result of the preceding 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 preceding example is j hit 17.
?>
Note:
The goto operator is only valid in PHP 5.3 and later versions.