3
votes

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

4
goto $goto is not possible. What are you trying to achieve?Sjoerd
why are you using goto ? couldn't you use functions instead ?Neel Basu
The goto command should only be used to break nested loops.jantimon
@Ghommey: purely theoretically :)genesis
@Ghommey In that example, break 3; would do the same job...DaveRandom

4 Answers

4
votes

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

7
votes

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

2
votes

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.

1
votes

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!