【问题标题】:Start function over, depending on results of IF statement重新启动函数,取决于 IF 语句的结果
【发布时间】:2011-03-24 00:12:52
【问题描述】:

如果我有一个类内部的函数,并且返回“无效”,我该如何从顶部函数开始备份?

function test(){

    //curl here

    //other stuff here

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

}

但我收到以下错误

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

如果我遇到 "invalid" 我想重新开始 test()..

【问题讨论】:

  • 您应该发布您的整个代码,因为目前您似乎在使用 continue 语句,而没有处于循环中(如“for”或“while”)。

标签: php


【解决方案1】:

您可能想在这里使用递归函数:

function test(){

    //curl here

    //other stuff here

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

或者,这可能是(非常罕见的)goto 运算符的好用法:

function test(){
    start:
    //curl here

    //other stuff here

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

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

【讨论】:

  • 感谢您的回复。我已经尝试过了,并收到错误“致命错误:调用未定义函数 test() in”
  • @henry 你的函数实际上叫做test,是吗?
  • 是的,这个函数叫做test
【解决方案2】:

我会使用exceptions,并让调用范围控制重复调用test()test() 执行一项工作;它不应该控制何时它被要求执行该工作。

(在其他示例中给出的递归方法并不真正适合用例 [这让我感到紧张,这要归功于递归太多次的语言最终会使您耗尽堆栈空间;当然,您正在为堆栈填满没有理由],虽然 goto 可以正常工作,但它仍然赋予函数本身太多的权力。)

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

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

function callingFunction()
{
   while (true) {
      try {
         test();
         break;  // only reached if test() didn't throw
      }
      catch(Exception $e) {} // if we fall into this, the loop repeats
   }
}

您仍然可以使用这种方法非常干净地应用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;
   }
}

【讨论】:

    【解决方案3】:

    删除continue;,改为添加test();。它被称为递归函数(调用自身)。

    【讨论】:

    • 感谢您的回复,当我尝试出现致命错误:调用未定义函数 test() in
    【解决方案4】:

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

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

    我的回答可能不是最好的,希望对你有所帮助。

    【讨论】:

      【解决方案5】:

      假设您事先更改了参数,您可以使用新参数再次递归调用该函数。只需确保存在函数将停止递归的边缘情况。至于那个,我得看真正的代码才能告诉你。

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

      【讨论】:

        【解决方案6】:

        如果你想跳出你当前所在的函数,你应该使用'return'而不​​是'continue'。

        或者如果你想重新开始处理,你应该把你的逻辑放到一个循环中。

        【讨论】:

          猜你喜欢
          • 2015-10-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-09-16
          • 2015-04-25
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多