在函数 php 中使用 goto
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19185326/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
use goto inside function php
提问by dav
Is there a way to define global label (something like variables) for PHP goto
, in order to use it inside function declaration. I want to do the following:
有没有办法为 PHP 定义全局标签(类似于变量)goto
,以便在函数声明中使用它。我想做以下事情:
function myFunction() {
if (condition) {
goto someLine;
}
}
someLine:
// ....
myFunction();
when I use this code it says
当我使用此代码时,它说
PHP Fatal error: 'goto' to undefined label "someLine"
I know that it is not recommended to use goto
statement. But I need it in my case. I know that perhaps always there are alternatives of goto
, just in my case it would make the code a little easier and understandable
我知道不建议使用goto
语句。但在我的情况下,我需要它。我知道也许总是有 的替代方案goto
,就我而言,它会使代码更容易理解
采纳答案by Lemon Drop
You cannot goto outside of a function I believe: http://php.net/manual/en/control-structures.goto.php
你不能去我相信的函数之外:http: //php.net/manual/en/control-structures.goto.php
Direct Quote: This is not a full unrestricted goto. The target label must be within the same file and context, meaning that you cannot jump out of a function or method, nor can you jump into one.
直接引用:这不是一个完全不受限制的转到。目标标签必须在同一个文件和上下文中,这意味着你不能跳出一个函数或方法,也不能跳入一个函数或方法。
This might have to do with the fact that php is parsed and jumping out of a function will cause a memory leak or something because it was never properly closed. Also as everyone else said above, really you don't need a goto. You can just return different values from the function and have a condition for each. Goto is just super bad practice for modern coding (acceptable if you are using basic).
这可能与解析 php 和跳出函数会导致内存泄漏或其他原因有关,因为它从未正确关闭。也正如上面其他人所说的那样,实际上您不需要转到。您可以只从函数返回不同的值,并为每个值设置一个条件。Goto 对现代编码来说是非常糟糕的做法(如果您使用的是基本的,则可以接受)。
Example:
例子:
function foo(a) {
if (a==1) {
return 1;
} elseif (a==3) {
return 2;
} else {
return 3;
}
}
switch (foo(4)) { //easily replaceable with elseif chain
case 1: echo 'Foo was 1'; break; //These can be functions to other parts of the code
case 2: echo 'Foo was 3'; break;
case 3: echo 'Foo was not 1 or 3';
}
回答by BetaBlaze
There's no way to jump in or out of a function. But since you state that you need it, here's an alternative route.
没有办法跳入或跳出函数。但既然你说你需要它,这里有一条替代路线。
function myFunction() {
if (condition) {
return true;
}
return false;
}
someLine:
// ....
$func = myFunction();
if($func == true) goto someLine;
回答by AbraCadaver
As previously stated you can't. As for "I need it", I highly doubt this. Whatever code you have at someLine:
can easily be made into a function that you can call from the other if needed.
如前所述,你不能。至于“我需要它”,我对此深表怀疑。无论您使用什么代码,都someLine:
可以轻松地将其制作成一个函数,如果需要,您可以从另一个函数中调用该函数。
回答by i am ArbZ
yeah there's a way as long as that function is declared on the same php file or is included if its on another script,
是的,只要该函数在同一个 php 文件中声明或者包含在另一个脚本中,就可以使用
the best way to jump on to someline is to return that goto code of yours dude so that if function is called a return value is received.. hope this helps
跳到某行的最好方法是返回你的哥们的那个 goto 代码,这样如果函数被调用,就会收到一个返回值..希望这会有所帮助
function foo($b="30")
{
$a=30;
if($a==intval($b))
return "someline:goto
someline";
}
try{
eval(foo());
}
catch(IOException $err)
{
exit($err);
}
/*someline is somewhere here
for example */