10

Is there a way to define global label (something like variables) for PHP goto, in order to use it inside function declaration. I want to do the following:

function myFunction() {
   if (condition) {
     goto someLine;
  }
}


someLine:
// ....

myFunction();

when I use this code it says

PHP Fatal error:  'goto' to undefined label "someLine"

I know that it is not recommended to use goto statement. But I need it in my case. I know that perhaps always there are alternatives of goto, just in my case it would make the code a little easier and understandable

4
  • So you want to jump outside the function? This isn't possible. What are you trying to achieve? Commented Oct 4, 2013 at 15:41
  • @Jim, I need to check the conditions inside the function and based on them, goto to specific line. Thanks Commented Oct 4, 2013 at 15:45
  • What are you using this for? There is almost always an alternative to using a goto. Commented Oct 4, 2013 at 15:46
  • Obligatory: goto considered harmful: stackoverflow.com/questions/46586/goto-still-considered-harmful Commented Oct 4, 2013 at 15:47

4 Answers 4

9

You cannot goto outside of a function I believe: http://php.net/manual/en/control-structures.goto.php

Direct Quote: This is not a full unrestricted goto. The target label must be within the same file and context, meaning that you cannot jump out of a function or method, nor can you jump into one.

This might have to do with the fact that php is parsed and jumping out of a function will cause a memory leak or something because it was never properly closed. Also as everyone else said above, really you don't need a goto. You can just return different values from the function and have a condition for each. Goto is just super bad practice for modern coding (acceptable if you are using basic).

Example:

function foo(a) {
    if (a==1) {
        return 1;
    } elseif (a==3) {
        return 2;
    } else {
        return 3;
    }
}

switch (foo(4)) { //easily replaceable with elseif chain
    case 1: echo 'Foo was 1'; break; //These can be functions to other parts of the code
    case 2: echo 'Foo was 3'; break;
    case 3: echo 'Foo was not 1 or 3';
}
Sign up to request clarification or add additional context in comments.

Comments

3

There's no way to jump in or out of a function. But since you state that you need it, here's an alternative route.

function myFunction() {
   if (condition) {
     return true;
   }
   return false;
}


someLine:
// ....

$func = myFunction();
if($func == true) goto someLine;

2 Comments

yeah, this way is the alternative that I though as well, but my function's conditions are a little complicated and using goto inside function would make the code easier. Thanks
It isn't really about how "easy" the code is in this case. There's not really another option besides programming things the right way -- which means NOT using goto.
3

As previously stated you can't. As for "I need it", I highly doubt this. Whatever code you have at someLine: can easily be made into a function that you can call from the other if needed.

Comments

0

yeah there's a way as long as that function is declared on the same php file or is included if its on another script,

the best way to jump on to someline is to return that goto code of yours dude so that if function is called a return value is received.. hope this helps

 function foo($b="30")
 {
 $a=30;
 if($a==intval($b))
 return "someline:goto
 someline";
 }

try{
eval(foo());
}
catch(IOException $err)
{
 exit($err);
}

/*someline is somewhere here   
for example */

1 Comment

you may not do a goto from a function to someline outside itself but you can instead return an eval or just return a simple string return "goto someline" but make sure to evaluate your function call like eval(foo()): where foo returns goto, i think thats working dude better try

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.