php 转到变量

发布于 2024-12-10 00:32:44 字数 480 浏览 2 评论 0原文

我想知道在 PHP 中是否可能出现这样的情况,

$goto = 'end';
goto $goto;

当我使用 PHP 时,我收到解析错误:语法错误,意外的 T_VARIABLE,期望 T_STRING。

此外,我将如何做这样的事情(考虑 a() 返回 truefalse),

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

栀梦 2024-12-17 00:32:44

Goto 只能这样工作

10:
//something

goto 10;

,或者

end:
//something

goto end;

但你的不可能

是的,我知道不鼓励使用goto,但对实际问题的答案比这些说不要使用要好得多GOTO GOTO IS EVIL

附录:eval goto

您不太可能真的想要这样做:

$goto = 'end';

eval(<<<EOD

    goto $goto;
    return; 

    end: 

    echo 'end';
EOD
);

演示

Goto works only like this

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

Addendum: eval goto

It's not likely you really want to do that:

$goto = 'end';

eval(<<<EOD

    goto $goto;
    return; 

    end: 

    echo 'end';
EOD
);

Demo

芯好空 2024-12-17 00:32:44

我能看到这个工作的唯一方法是如果你这样做:

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

但是

  • 这可能根本不起作用(我没有可用于测试它的 5.3 安装)
  • 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:

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

HOWEVER

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

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

不羁少年 2024-12-17 00:32:44

即使可能,也不要使用 goto 来执行此操作。这样做:

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

function end() {
  echo "This is run\n";
  exit();
}

您也可以在对象上下文中使用 $this->$goto() 来执行此操作 - 有时非常方便。

Don't do it with goto, even if it is possible. 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.

述情 2024-12-17 00:32:44

没有办法做到这一点(使用goto)。另外,除了留下(嵌套)循环之外,您不应该使用 goto 进行任何操作。
使用 goto 可以完成的所有其他事情都可以使用“更好”且不太混乱的方法和代码结构来完成,因此您应该更喜欢这些!

There's no way of doing that (with goto). Also you should't use goto 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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文