控制结构
在线手册:中文 英文
PHP手册

goto

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

Example #1 goto 示例

<?php
goto a;
echo 
'Foo';
 
a:
echo 
'Bar';
?>

以上例程会输出:

Bar

Example #2 goto 跳出循环示例

<?php
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

Example #3 以下写法无效

<?php
goto loop;
for(
$i=0,$j=50$i<100$i++) {
  while(
$j--) {
    
loop:
  }
}
echo 
"$i = $i";
?>

以上例程会输出:

Fatal error: 'goto' into loop or switch statement is disallowed in
script on line 2

Note:

The goto 操作符仅在 PHP 5.3及以上版本有效.

xkcd-goto.png
Image courtesy of » xkcd


控制结构
在线手册:中文 英文
PHP手册
PHP手册 - N: goto

用户评论:

roman4work at gmail dot com (09-Apr-2012 11:36)

since label executes all the time even if you don't use goto label;

<?php

if (false)
   goto
label

label
:
   echo
"label triggered";

?>

Will output: label triggered

I use labels like this

<?php

...some code...

if (
false) {
  
label1 :
      echo
"label 1 triggered";
}

if (
false) {

  
label2 :
      echo
"label 2 triggered";
}

if (
false) {

  
label3 :
      echo
"label 3 triggered";
}

?>

It will never output unless you use "goto <label>".

f at francislacroix dot info (30-Nov-2011 09:11)

The goto operator CAN be evaluated with eval, provided the label is in the eval'd code:

<?php
a
: eval("goto a;"); // undefined label 'a'
eval("a: goto a;"); // works
?>

It's because PHP does not consider the eval'd code, containing the label, to be in the same "file" as the goto statement.

Ray dot Paseur at Gmail dot com (27-Oct-2011 02:59)

You cannot implement a Fortran-style "computed GOTO" in PHP because the label cannot be a variable. See: http://en.wikipedia.org/wiki/Considered_harmful

<?php // RAY_goto.php
error_reporting(E_ALL);

// DEMONSTRATE THAT THE GOTO LABEL IS CASE-SENSITIVE

goto a;
echo
'Foo';
a: echo 'Bar';

goto
A;
echo
'Foo';
A: echo 'Baz';

// CAN THE GOTO LABEL BE A VARIABLE?

$a = 'abc';
goto
$a; // NOPE: PARSE ERROR
echo 'Foo';
abc: echo 'Boom';
?>

contact at xpertmailer dot com (07-Jul-2011 02:08)

goto operator can NOT be evaluate with eval()

tweston at coldsteelstudios dot com (06-Oct-2010 04:12)

In a challenge of myself for a college class I decided to use the goto to remove all while loops from my code. It was actually easy, and AS FAST as While loops.

<?PHP
$start
= microtime(true);
$i = 0;
StartOfLoop:
$i++;
if(
$i < 1000000) goto StartOfLoop;

echo
microtime(true) - $start.PHP_EOL;

$start = microtime(true);
$i = 0;
while(
$i < 1000000){
   
$i++;
}

echo
microtime(true) - $start.PHP_EOL;
?>

chrisstocktonaz at gmail dot com (07-Aug-2009 11:03)

Remember if you are not a fan of wild labels hanging around you are free to use braces in this construct creating a slightly cleaner look. Labels also are always executed and do not need to be called to have their associated code block ran. A purposeless example is below.

<?php

$headers
= Array('subject', 'bcc', 'to', 'cc', 'date', 'sender');
$position = 0;

hIterator: {

   
$c = 0;
    echo
$headers[$position] . PHP_EOL;

   
cIterator: {
        echo
' ' . $headers[$position][$c] . PHP_EOL;

        if(!isset(
$headers[$position][++$c])) {
            goto
cIteratorExit;
        }
        goto
cIterator;
    }

   
cIteratorExit: {
        if(isset(
$headers[++$position])) {
            goto
hIterator;
        }
    }
}
?>