if语句中的php return语句

tha*_*ili 5 php if-statement return

也许我的问题有点初级或愚蠢,但是我需要验证这一点。

我有一个 php 函数“functionA”,它在 for 循环中被反复调用:

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

和我的功能A:

...
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!
}
...
Run Code Online (Sandbox Code Playgroud)

因此,如果“$wide_avg_txon==1”,我不想执行 functionA 中的其余代码,我只想继续执行 for 循环以进行下一次迭代!

上面的代码会起作用吗?'return' 和 'return false' 之间有什么区别?'return false' 是否也会起作用:

...
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;
    }
Run Code Online (Sandbox Code Playgroud)

谢谢!

RST*_*RST 5

您的return意愿会起作用,因为您对返回的结果不感兴趣。您只想继续 for 循环。

如果您有一个测试某些内容的代码结构,并且您想知道测试的结果,那么您可以使用return false让其余代码知道测试失败。