php 转到变量
我想知道在 PHP 中是否可能出现这样的情况,
$goto = 'end';
goto $goto;
当我使用 PHP 时,我收到解析错误:语法错误,意外的 T_VARIABLE,期望 T_STRING。
此外,我将如何做这样的事情(考虑 a()
返回 true
或 false
),
a() or goto end;
而不是纯粹
if (!a()) goto end;
- 理论上的较长版本:)
这无疑引起了很大的反响。我认为提及 PHP 两个最有争议的领域(goto 和 eval)有助于引起一些强烈的反应。
只是为了弄清楚事情并让一些人放心:我知道不使用 goto 的原因。但每条“规则”都有例外。
I wonder if something like this is possible in PHP
$goto = 'end';
goto $goto;
when I use it I get 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() or goto end;
as opposed to the longer version
if (!a()) goto end;
- purely theoretically :)
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.
Just to get things clear and put some people's hearts at ease: I know the reasons for not using goto. But to every "rule" there are exceptions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Goto 只能这样工作
,或者
但你的不可能
是的,我知道不鼓励使用
goto
,但对实际问题的答案比这些说不要使用要好得多GOTO GOTO IS EVIL
附录:eval goto
您不太可能真的想要这样做:
演示
Goto works only like this
or
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 sayingDO NOT USE GOTO GOTO IS EVIL
Addendum: eval goto
It's not likely you really want to do that:
Demo
我能看到这个工作的唯一方法是如果你这样做:
但是
eval()< /code> 在 99.9% 的情况下,应该不惜一切代价避免
goto
。这很少是答案 - 如果您发现自己使用 goto,您可能会更好地检查代码的结构。大多数时候,人们使用 goto 来避免混乱的 if-else 结构,但是您可以做一些(稍微)更好的事情,它可以实现相同的效果:将代码块包装在
do {} while (FALSE );
。这意味着您可以调用break
跳过代码块的其余部分并直接跳到末尾。这些也可以嵌套,因此您可以调用break 2;
等来跳到正确的点。我知道有很多人不同意我的这种做法——让虐待风暴开始吧……
The only way I could see this working is if you do this:
HOWEVER
eval()
should be avoided at all costs under 99.9% of circumstancesgoto
. 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);
. This means you can callbreak
to skip the rest of the code block and jump straight to the end. These can also be nested, so you can callbreak 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...
即使可能,也不要使用 goto 来执行此操作。这样做:
您也可以在对象上下文中使用
$this->$goto()
来执行此操作 - 有时非常方便。Don't do it with goto, even if it is possible. Do it this way instead:
You can also do this in an object context using
$this->$goto()
- very handy sometimes.没有办法做到这一点(使用
goto
)。另外,除了留下(嵌套)循环之外,您不应该使用goto
进行任何操作。使用
goto
可以完成的所有其他事情都可以使用“更好”且不太混乱的方法和代码结构来完成,因此您应该更喜欢这些!There's no way of doing that (with
goto
). Also you should't usegoto
for anything, except leaving (nested) loops.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!