doucheng7234 2015-05-16 16:25
浏览 403
已采纳

if语句中的php return语句

Maybee my question is somehow elementary or stupid, however i need to verify this.

I have a php function "functionA" which is repeatidely called in a for loop:

...
for($j=0; $j<$n_samples;$j++) {
    if($type=='triband') {
        functionA($arg1,$arg2);                 
    }
}
...

and my functionA:

...
functionA($arg1,$arg2) {
    $wide_avg_txon = substr($bit_sequence,0,1);
    if($wide_avg_txon==0)
    {
        echo " ---> is OFF...<br />";
    }
    else
    {
        echo " ---> is ON...<br />";
        // if is ON then skip execution of code inside the function
        return;
    }

    // DO SOME STUFF!
}
...

So simply i do not want to execute the rest of code inside functionA if "$wide_avg_txon==1" and i just want to continue executing the for loop for the next iteration!

Is the above code going to work? What is the difference between: 'return' and 'return false'? Is 'return false' going also to work:

...
if($wide_avg_txon==0)
    {
        echo " ---> is OFF...<br />";
    }
    else
    {
        echo " ---> is ON...<br />";
        // if is ON then skip execution of code inside the function
        return false;
    }

thanks!

  • 写回答

4条回答 默认 最新

  • doupang3062 2015-05-16 16:31
    关注

    Your return will work because you are not interested in the result that is being returned. You just want to continue the for-loop.

    If you had a code construction that tested something and you want to know the result of the testing then you could use return false to let the rest of the code know the test failed.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?