php goto variable

£可爱£侵袭症+ 提交于 2021-02-04 19:13:06

问题


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;

Thanks so much!

  • purely theoretically :)

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.

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


回答1:


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




回答2:


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




回答3:


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.




回答4:


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!



来源:https://stackoverflow.com/questions/7766968/php-goto-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!