php goto的用法

(PHP 5 >= 5.3.0, PHP 7)

goto 操作符可以用来跳转到程序中的另一位置。该目标位置可以用目标名称加上冒号来标记,而跳转指令是 goto 之后接上目标位置的标记。PHP 中的 goto 有一定限制,目标位置只能位于同一个文件和作用域,也就是说无法跳出一个函数或类方法,也无法跳入到另一个函数。也无法跳入到任何循环或者 switch 结构中。可以跳出循环或者 switch,通常的用法是用 goto 代替多层的 break

 

Example:跳出去,再继续执行startTemplate:

        $where = ['merchant_id' => self::$merchant_id, 'store_id' => $store_id];
        startTemplate:
        $default = Template::find()->where($where)->asArray()->limit(1)->one();
        if (empty($default)) {
            if ($where['store_id'] > 0) {
                $where['store_id'] = 0;
                goto startTemplate;
            } else {
                return false;
            }
        }

Example:调到指定的位置:执行 echo 'a';

<?php
goto a;
echo 'b';
 
a:
echo 'a';
?>
原文地址:https://www.cnblogs.com/fangjiali/p/6616499.html