php goto变量 [英] php goto variable

查看:53
本文介绍了php goto变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道这样的事情在PHP中是否可能

I wonder if something like this is possible in PHP

$goto = 'end';
goto $goto;

当我使用它时,出现 Parse错误:语法错误,意外的T_VARIABLE,期望的T_STRING .

此外,我该怎么做(考虑 a()返回 true false )

Furthermore, how would I do something like this (considering a() returns true or false)

a() or goto end;

与较长版本相反

if (!a()) goto end;

非常感谢!

  • 理论上完全是:)

后来这肯定引起了很多反响.我认为提及PHP争议最大的两个领域(goto和eval)有助于获得一些强烈的反响.无论如何,我要感谢那些为克服自己的偏执狂"而付出努力的人(就像有人在评论中说的那样).我特别要感谢DaveRandom和Genesis的回答.

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. 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.

只是弄清楚事情,让一些人放心:我知道不使用goto的原因.但是每个规则"都有例外.

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.

最后,我想了解实际投票反对该问题的人的逻辑.这不是一个有效的问题,还不够清楚,是否可以使用搜索引擎轻松回答?我想我永远都不会为您的行为动机下投票用户":).

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" :).

推荐答案

我看到此工作的唯一方法是执行以下操作:

The only way I could see this working is if you do this:

$goto = 'end';
eval("goto $goto;");

如何

  • 这可能根本不起作用(我没有5.3安装可供测试)
  • eval() 在99.9%的情况下应不惜一切代价避免
  • 同上 goto .很少有答案-如果您发现自己使用goto的方法,则最好检查一下代码的结构.
  • This may not work at all (I don't have a 5.3 install readily available to test it)
  • eval() should be avoided at all costs under 99.9% of circumstances
  • Ditto 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.

大多数时候,人们使用goto来避免混乱的if-else结构,但是您可以做的(稍微)更好一些,可以达到相同的目的:将代码块包装在 do {}中而(FALSE); .这意味着您可以调用 break 跳过其余的代码块,然后直接跳到最后.这些也可以嵌套,因此您可以调用 break 2; 等跳到正确的位置.

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 call break to skip the rest of the code block and jump straight to the end. These can also be nested, so you can call 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...

这篇关于php goto变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆