Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

...
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!

like image 851
thanili Avatar asked Sep 28 '22 22:09

thanili


1 Answers

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.

like image 178
RST Avatar answered Oct 03 '22 00:10

RST