4

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.

10
  • goto $goto is not possible. What are you trying to achieve? Commented Oct 14, 2011 at 11:32
  • 1
    why are you using goto ? couldn't you use functions instead ? Commented Oct 14, 2011 at 11:33
  • 1
    The goto command should only be used to break nested loops. Commented Oct 14, 2011 at 11:33
  • 1
    @Ghommey: purely theoretically :) Commented Oct 14, 2011 at 11:34
  • 1
    @Ghommey In that example, break 3; would do the same job... Commented Oct 14, 2011 at 13:26

4 Answers 4

7

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

Sign up to request clarification or add additional context in comments.

1 Comment

@genesis: It's not really worth of an answer, even I'm not religious about goto ;)
5

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 Comments

I like the do {} while (FALSE); method. I use it occasionally in Java, as well as PHP and JavaScript.
@DaveRandom: eval goto does only work for labels that are part of the eval'ed code. See the demo I added to genesis answer (have not seen your answer earlier the edit).
@NikiC: It does work if you define the goto label. But you really don't want that.
@hakre That's a different question though.
@hakre I thought it might be the case that the above example wouldn't work, because it would be roughly equivalent to doing a goto between included files, which I know does not work (it says so in the manual). I didn't think of wrapping the entire code in eval() though, which obviously would work, but seems like rather extreme lengths to go to to achieve this functionality which you can probably avoid the requirement for in the first place by writing better code. Virtual +1 for the sentiment though...
|
2

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.

3 Comments

I have downvoted, but because your reputation isn't high I was afraid you'll downvote all my posts afterwards. Reason is that he's asking "how to use it like this", he's not asking "how to workaround it, and should I use it?". When you'd ask me "how to set my hair in fire" I'd explain how and also say that you should NOT do that. You did only the second point which is wrong.
Thank you @genesis. (Btw, in your other post on this thread, I think you were looking for the word "addendum", rather than "appendum"! Yours sounds good, but the correct Latin is the first one ;-)
This snippet works quite differently from a goto (which, in the "end" example, makes it incorrect). Namely, it does all the ending stuff, then jumps right back to where you called it from. This is more than likely useless for something called end, unless the function also exits. I will say, though, it's less hideous than trying to use eval.
1

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.