• 15

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

use goto inside function php

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;

// ....


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

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


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';
  • 9
Reply Report

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;

// ....

$func = myFunction();
if($func == true) goto someLine;
  • 3
Reply Report
      • 2
    • 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
      • 2
    • 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.

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.

  • 3
Reply Report

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")
 return "someline:goto

catch(IOException $err)

/*someline is somewhere here   
for example */
  • 0
Reply Report
      • 2
    • 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

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions