Use of goto syntax in PHP

Time:2022-1-17

In C + +, Java and many languages, there is a magical syntax, goto. As the name suggests, its use is to go directly to a certain place. From the perspective of code, that is, jump directly to the specified place. We also have this function in PHP. Let’s see how it is used first:

goto a;
echo "1"; //  No output

a:
echo '2'; // 2

When the code runs to the goto location, it jumps to the code line where a: is located and continues to execute. It’s fun. This function is very useful for complex nested if or jumping out of some loops, especially for the handling of some exceptions or errors, such as:

for ($i = 0, $j = 50; $i < 100; $i++) {
    while ($j--) {
        If ($J = = 17) {// suppose $J = = 17 is an exception
            goto end; //  If you skip directly, the result of the end of the loop will not be output
        }

    }
}
echo "i = $i";
end:
echo 'j hit 17'; //  You can output or handle exceptions directly here

It feels good, doesn’t it, but goto grammar also has some limitations:

  • The target location can only be in the same file and scope, that is, you can’t jump out of a function or class method, or jump into another function
  • Cannot jump into any loop or switch structure
  • Jump out of the loop or switch. The common usage is to use goto instead of multi-layer break

For example, the following codes are invalid:

$a = 1;
goto switchgo;
switch ($a){
    case 1:
        echo 'bb';
    break;
    case 2:
        echo 'cc';
        switchgo:
            echo "bb";
    break;
}

goto whilego;
while($a < 10){
    $a++;
    whilego:
        echo $a;
}


// Fatal error: 'goto' to undefined label 'ifgo'

They all report the same error because the scope relationship cannot find the defined goto tag. In addition, it should be noted that using goto may cause dead circulation, as shown below:

b:
    echo 'b';

goto b;

When the code is executed to goto, it jumps back to the previous B label line, and then continues to execute downward. It goes to goto again and becomes an endless loop. It feels like while (true). However, there is no break in this goto cycle, only when goto goes out to other places.

Therefore, goto syntax is rarely used because it will disturb the logical flow of your code, but people who like it will feel that it can make the code very flexible. This requires different opinions. At present, the use of this syntax is not very advocated in most language documents, including PHP. My suggestion is to try not to use goto syntax if it is not a very special case or to show off skills. When the project code is complex, it is easy to confuse others or yourself.

Test code:https://github.com/zhangyue0503/dev-blog/blob/master/php/201911/goto%E8%AF%AD%E6%B3%95%E5%9C%A8PHP%E4%B8%AD%E7%9A%84%E4%BD%BF%E7%94%A8.md

Reference documents:https://www.php.net/manual/zh/control-structures.goto.php

Official account: hard core project manager

Add wechat / QQ friends: [xiaoyuezigonggong / 149844827] get free PHP and project management learning materials

Tiktok, official account, voice, headline search, hard core project manager.

Station B ID: 482780532

Recommended Today

Big data Hadoop — spark SQL + spark streaming

catalogue 1、 Spark SQL overview 2、 Sparksql version 1) Evolution of sparksql 2) Comparison between shark and sparksql 3)SparkSession 3、 RDD, dataframes and dataset 1) Relationship between the three 1)RDD 1. Core concept 2. RDD simple operation 3、RDD API 1)Transformation 2)Action 4. Actual operation 2)DataFrames 1. DSL style syntax operation 1) Dataframe creation 2. SQL […]