PHP flow control goto syntax
Since PHP 5.3.0, you can also use goto to break out of loops.
In the beginning of this chapter, we told a story. Student Wang travels back and forth every week, but there is a special case:
Except after the project fails or the group is temporarily terminated , he can no longer go back and forth every week.
Basic syntax
<?php goto wan; echo '天王盖地虎'; wan: echo '小鸡炖蘑菇'; ?>
Through the above example, we found that the output is directly displayed: chicken stewed with mushrooms.
Let’s implement the code of the Overbearing President:
<?php for($i=0; $i<100; $i++) { echo '第'. $i .'周往返北京大连<br />'; if($i == 17){ goto end; } } end: echo '集团公司要求停止此项'; ?>
This knowledge point is for the understanding level. If you don’t want to learn it, you don’t need to learn this block.
Note:
The goto operator can be used to jump to another location in the program.
The target position can be marked with the target name plus a colon, and the jump instruction is the mark of the target position followed by goto.
Goto in PHP has certain restrictions. The target location can only be in the same file and scope, which means that it cannot jump out of a function or class method, nor can it jump into another function. It also cannot jump into any loop or switch structure. You can jump out of a loop or switch. The usual usage is to use goto instead of multiple levels of break.