简体   繁体   English

if语句中的php return语句

[英]php return statement in if statement

Maybe my question is somehow elementary or stupid, however i need to verify this.也许我的问题有点初级或愚蠢,但是我需要验证这一点。

I have a php function "functionA" which is repeatedely called in a for loop:我有一个 php 函数“functionA”,它在 for 循环中被反复调用:

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

and my functionA:和我的功能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!
}
...

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

Is the above code going to work?上面的代码会起作用吗? What is the difference between: 'return' and 'return false'? 'return' 和 'return false' 之间有什么区别? Is 'return false' going also to work: '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;
    }

thanks!谢谢!

Your return will work because you are not interested in the result that is being returned.您的return将起作用,因为您对返回的结果不感兴趣。 You just want to continue the for-loop.您只想继续 for 循环。

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.如果您有一个测试某些内容的代码构造并且您想知道测试的结果,那么您可以使用return false让其余代码知道测试失败。

Your functionA will work perfectly, but for readability's sake it may be better to format it this way:您的 functionA 将完美运行,但为了可读性,最好以这种方式对其进行格式化:

...
function functionA($arg1, $arg2) {

    $wide_avg_txon = substr($bit_sequence,0,1);

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

    echo " ---> is OFF...<br />";

    // DO SOME STUFF!
}
...

The difference is that you immediately disqualify the "ON" condition that you don't want, and exit the function as quickly as possible.不同之处在于您立即取消了您不想要的“ON”条件,并尽快退出该功能。 Then the rest of the function is working on whatever it is you want to do, rather that sitting inside of an if statement block.然后该函数的其余部分正在处理您想做的任何事情,而不是坐在 if 语句块内。

Function will be stopped after the return statement.函数将在 return 语句后停止。 You can read more about here: http://php.net/manual/tr/function.return.php您可以在这里阅读更多信息: http : //php.net/manual/tr/function.return.php

As as example you can do something like this to test this:例如,您可以执行以下操作来测试:

<?php
$_SESSION['a'] = "Naber";
function a(){
        return;
        unset($_SESSION['a']);
}
a(); // Let see that is session unset?
echo $_SESSION['a'];
?>

Thanks谢谢

return false returns false , a boolean. return false返回false ,一个布尔值。 return will return NULL . return将返回NULL Neither will execute any subsequent code.两者都不会执行任何后续代码。

So to expand upon RST's answer, both returns would not satisfy an if condition:因此,为了扩展 RST 的答案,两个返回都不满足if条件:

if(functionA($arg1,$arg2))
     echo'foo';
else
     echo'bar';

bar would be echoed. bar会得到回应。

Where this might be useful is if you have:如果您有:

$return=functionA($arg1,$arg2);
if($return===NULL)
    echo'Nothing happened';
elseif($return===false)
    echo'Something happened and it was false';
else
    echo'Something happened and it was true';

NULL is pretty useful. NULL非常有用。

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

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