Return php statement in if statement

Maybee my question is somehow elementary or stupid, however I need to check this.

I have a php function "functionA" that repeats in a for loop:

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

      

and my function 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!
}
...

      

It's so easy I don't want to execute the rest of the code inside function A if "$ wide_avg_txon == 1" and I just want to continue executing the for loop for the next iteration!

Will the above code work? What is the difference between: "return" and "return false"? Does "return false" also 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;
    }

      

thank!

+3


source to share


4 answers


Yours return

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



If you had a code construct that tested something and you want to know the test result, you can use return false

it to let the rest of the code know that the test failed.

+3


source


The function will stop after the return statement. You can read more here: http://php.net/manual/tr/function.return.php

As an 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'];
?>

      

thank

0


source


return false

returns false

, boolean. return

will return NULL

. None of the following codes will execute.

So, to expand on the RST response, both returns did not satisfy the condition if

:

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

      

bar

will echo.

Where can it be useful 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

pretty useful.

0


source


Your A function will work fine, but for readability it is better to format it like this:

...
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 need, and exit the function as quickly as possible. Then the rest of the function works on what you want to do, rather than sitting inside an if block.

0


source







All Articles