(PHP 5 >= 5.3.0)
goto 操作符可以用来跳转到程序中的另一位置。该目标位置可以用目标名称加上冒号来标记,而跳转指令是 goto 之后接上目标位置的标记。PHP 中的 goto 有一定限制,目标位置只能位于同一个文件和作用域,也就是说无法跳出一个函数或类方法,也无法跳入到另一个函数。也无法跳入到任何循环或者 switch 结构中。可以跳出循环或者 switch,通常的用法是用 goto 代替多层的 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:
goto 操作符仅在 PHP 5.3及以上版本有效。
dpreece at microcal dot ca (2013-06-26 03:43:01)
Now that I have found goto (not been at PHP to long) I can eliminate a bunch of IFs. I have about 12 to do to get to dofinal.
<?php
do stuff
....
set if errorr $err=true //replace with goto dofinal;
if(!$err)
{
do stuff
....
set if errorr $err=true //replace with goto dofinal;
}
if(!$err)
{
do stuff
....
set if errorr $err=true //replace with goto dofinal;
}
dofinal:
do the common end script stuff
?>
sixoclockish at gmail dot com (2012-06-14 07:58:10)
You are also allowed to jump backwards with a goto statement. To run a block of goto as one block is as follows:
example has a prefix of iw_ to keep label groups structured and an extra underscore to do a backwards goto.
Note the `iw_end_gt` to get out of the labels area
<?php
$link = true;
if ( $link ) goto iw_link_begin;
if(false) iw__link_begin:
if ( $link ) goto iw_link_text;
if(false) iw__link_text:
if ( $link ) goto iw_link_end;
if(false) iw__link_end:
goto iw_end_gt;
if (false) iw_link_begin:
echo '<a href="#">';
goto iw__link_begin;
if (false) iw_link_text:
echo 'Sample Text';
goto iw__link_text;
if (false) iw_link_end:
echo '</a>';
goto iw__link_end;
iw_end_gt:
?>
roman4work at gmail dot com (2012-04-09 03:36:14)
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 (2011-11-30 13:11:48)
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 (2011-10-27 06:59:03)
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';
?>
tweston at coldsteelstudios dot com (2010-10-05 20:12:20)
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 (2009-08-07 15:03:50)
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;
}
}
}
?>