[英]php goto variable
I wonder if something like this is possible in PHP我想知道这样的事情在 PHP 中是否可行
$goto = 'end';
goto $goto;
when I use it I get Parse error: syntax error, unexpected T_VARIABLE, expecting T_STRING
.当我使用它时,我得到
Parse error: syntax error, unexpected T_VARIABLE, expecting T_STRING
。
Furthermore, how would I do something like this (considering a()
returns true
or false
)此外,我将如何做这样的事情(考虑
a()
返回true
或false
)
a() or goto end;
as opposed to the longer version与较长的版本相反
if (!a()) goto end;
Thanks so much!非常感谢!
Later edit: This has certainly got a lot of reaction.后期编辑:这肯定引起了很多反应。 I think mentioning two of PHP's most debated areas (goto and eval) helped get some strong reactions.
我认为提及 PHP 最有争议的两个领域(goto 和 eval)有助于引起一些强烈的反应。 Anyway, I want to thank those who put in the effort to get past their "paranoia" (as somebody said in the comments).
无论如何,我要感谢那些努力克服“偏执狂”的人(正如有人在评论中所说)。 I especially want to thank DaveRandom and genesis for their answers.
我特别要感谢 DaveRandom 和 genesis 的回答。
Just to get things clear and put some people's hearts at ease: I know the reasons for not using goto.只是为了把事情弄清楚,让一些人放心:我知道不使用 goto 的原因。 But to every "rule" there are exceptions.
但每一个“规则”都有例外。
As a final note, I'd like to understand the logic the person who actually voted down the question had.最后,我想了解实际投票否决该问题的人的逻辑。 Is it not a valid question, is it not clear enough, could it have been easily answered by using a search engine?
这不是一个有效的问题,是不是不够清楚,是否可以通过使用搜索引擎轻松回答? I guess I'll never have a motive for your action, "down-vote-user" :).
我想我永远不会对你的行为有动机,“拒绝投票的用户”:)。
Goto works only like this Goto 只能这样工作
10:
//something
goto 10;
or要么
end:
//something
goto end;
but yours one is impossible但你的一个是不可能的
Yes, I know using goto
is discouraged, but an answer to the actual question is a lot better than these saying DO NOT USE GOTO GOTO IS EVIL
是的,我知道不鼓励使用
goto
,但实际问题的答案比这些说DO NOT USE GOTO GOTO IS EVIL
的要好得多
Addendum: eval goto附录: eval goto
It's not likely you really want to do that:您不太可能真的想这样做:
$goto = 'end';
eval(<<<EOD
goto $goto;
return;
end:
echo 'end';
EOD
);
The only way I could see this working is if you do this:我能看到这个工作的唯一方法是如果你这样做:
$goto = 'end';
eval("goto $goto;");
HOWEVER然而
eval()
should be avoided at all costs under 99.9% of circumstanceseval()
goto
.goto
。 It is rarely the answer - if you find yourself using goto's, you would probably do better to examine the structure of your code. Most of the time, people use goto to avoid a messy if-else structure, but there is something (slightly) nicer that you can do, which achieves the same thing: wrap the code block in do {} while (FALSE);
大多数时候,人们使用 goto 来避免混乱的 if-else 结构,但是您可以做一些(稍微)更好的事情,它可以实现相同的目的:将代码块包装在
do {} while (FALSE);
. . This means you can call
break
to skip the rest of the code block and jump straight to the end.这意味着您可以调用
break
跳过其余的代码块并直接跳到最后。 These can also be nested, so you can call break 2;
这些也可以嵌套,所以你可以调用
break 2;
etc to skip to the right point.等跳到正确的点。
I know there are many people who will disagree with me on this approach - let the abuse storm begin...我知道有很多人会不同意我的这种做法——让虐待风暴开始吧……
Don't do it with goto, even if it is possible.即使可能,也不要使用 goto 执行此操作。 Do it this way instead:
改为这样做:
$goto = 'end';
$goto();
function end() {
echo "This is run\n";
exit();
}
You can also do this in an object context using $this->$goto()
- very handy sometimes.您也可以使用
$this->$goto()
在对象上下文中执行$this->$goto()
有时非常方便。
There's no way of doing that (with goto
).没有办法做到这一点(使用
goto
)。 Also you should't use goto
for anything, except leaving (nested) loops.此外,除了离开(嵌套)循环外,您不应将
goto
用于任何事情。
Everything else that might be done with goto
can be done with "better" and less confusing methods and code structures, so you should prefer those!可以使用
goto
完成的所有其他操作都可以使用“更好”且更少混淆的方法和代码结构来完成,因此您应该更喜欢那些!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.