This article mainly introduces what is the PHP goto statement and the usage examples of gotooperator. Friends who need it can refer to it
goto operator can be used to jump to the program a specified location in . 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 loop or switch structures. Common usage is to break out of a loop or switch, which can replace multi-layer break.
The 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:
Copy code Code As follows:
<?php goto a;echo 'Foo'; //此句被略过 a:echo 'Bar'; //上面的例子输出结果为: Bar; for($i=0,$j=50; $i<100; $i++) { while($j--) { if($j==17) goto end; } }echo "i = $i"; end:echo 'j hit 17'; //上面的例子输出结果为: j hit 17 ?>
Note:
The goto operator is only valid in PHP 5.3 and above.
The following writing methods are invalid
<?php goto loop; for($i=0,$j=50; $i<100; $i++) { while($j--) { loop: } } echo "$i = $i"; ?>
The above routine will output:
Fatal error: 'goto' into loop or switch statement is disallowed in script on line 2
The above is the detailed content of Detailed explanation of the use of php goto statement. For more information, please follow other related articles on the PHP Chinese website!