简体   繁体   English

在函数php中使用goto

[英]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. 有没有办法为PHP goto定义全局标签(类似变量),以便在函数声明中使用它。 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. 我知道不建议使用goto语句。 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 我知道也许总是有替代goto ,只是在我的情况下它会使代码更容易和可理解

You cannot goto outside of a function I believe: http://php.net/manual/en/control-structures.goto.php 你不能转到我认为的函数之外: 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. 这可能与php被解析并跳出函数将导致内存泄漏或其他因为它从未正确关闭的事实有关。 Also as everyone else said above, really you don't need a goto. 正如上面所说的其他人一样,你真的不需要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). Goto对于现代编码来说只是超级糟糕的做法(如果您使用的是基本的,则可以接受)。

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';
}

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;

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. 无论你在someLine:有什么代码someLine:都可以很容易地变成一个函数,如果需要你可以从另一个调用。

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, 是的,只要该函数在同一个php文件中声明或者如果它在另一个脚本上包含,就有一种方法,

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 跳转到某个行的最好方法是返回你的goto的goto代码,这样如果函数被调用,则收到返回值..希望这有助于

 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 */

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM