Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 根据IF语句的结果重新启动函数_Php - Fatal编程技术网

Php 根据IF语句的结果重新启动函数

Php 根据IF语句的结果重新启动函数,php,Php,如果我有一个在类内部的函数,并且返回“invalid”,那么如何从顶部函数开始重新启动 function test(){ //curl here //other stuff here if(strpos($data, 'invalid')){ print "invalid"; //discard and remove continue ; } } 但是我得到了以下错误 致命错误:无法在中中断/

如果我有一个在类内部的函数,并且返回“invalid”,那么如何从顶部函数开始重新启动

function test(){

    //curl here

    //other stuff here

    if(strpos($data, 'invalid')){
        print "invalid";
               //discard and remove
        continue ;
    }

}
但是我得到了以下错误

致命错误:无法在中中断/继续1级


如果我被“invalid”击中,我想开始
测试()
返回..

删除
继续并改为添加
test()。它被称为递归函数(调用自身)。

您可能希望在此处使用递归函数:

function test(){

    //curl here

    //other stuff here

    if(strpos($data, 'invalid')){
        print "invalid";
           //discard and remove
        return test() ; // restart process
    }
}

或者,这可能是(非常罕见的)操作员的良好使用:

function test(){
    start:
    //curl here

    //other stuff here

    if(strpos($data, 'invalid')){
        print "invalid";
           //discard and remove
        goto start;
    }
}

请注意,这只适用于PHP>=5.3。

有几种方法可以做到这一点,我的老师通常建议的方法是实际再次调用该函数

function test(){

    //curl here

    //other stuff here

    if(strpos($data, 'invalid')){ 
         print "invalid"; 
         //discard and remove continue ; 
         test();
         return;
    }

}

我的答案可能不是最好的,希望这有帮助。

假设您事先更改了参数,您可以使用新参数递归地再次调用函数。只需确保函数将停止递归的边缘情况。至于这一点,我需要看到真正的代码来告诉你

function test($data){

//curl here

//other stuff here

if(strpos($data, 'invalid')){
    print "invalid";
           //discard and remove
    test($NEW_DATA);
}

}

如果您想中断当前使用的函数,应该使用“return”而不是“continue”

或者,如果您想重新开始处理,您应该将逻辑放入一个循环。

我会利用,并让调用范围控制重复调用
test()
<代码>测试()
执行一项作业;它不应该控制何时被要求执行该任务

(其他示例中给出的递归方法并不真正适合用例[而且让我感到紧张,这要感谢那些递归次数太多最终会耗尽堆栈空间的语言;当然,你是在无缘无故地填充堆栈的语言],尽管
goto
可以很好地工作,但它仍然赋予函数本身太多的功能。)

您仍然可以使用此方法非常干净地应用
goto

function test()
{
    //curl here
    //other stuff here

    if (strpos($data, 'invalid'))
        throw new Exception("Data is invalid");
}

function callingFunction()
{
startTest:
   try {
      test();
   }
   catch(Exception $e) {
      goto startTest;
   }
}

您应该发布您的全部代码,因为当前您似乎使用的是continue语句,而不在循环中(如“for”或“while”)。感谢您的回复,当我尝试时,我遇到了致命错误:调用undefined function test()inThank以获得您的回复。我尝试过这个方法,但在“@henry”中出现了错误“致命错误:调用未定义的函数test()”。您的函数实际上被称为
test
,是吗?是的,该函数被称为test
function test()
{
    //curl here
    //other stuff here

    if (strpos($data, 'invalid'))
        throw new Exception("Data is invalid");
}

function callingFunction()
{
startTest:
   try {
      test();
   }
   catch(Exception $e) {
      goto startTest;
   }
}