6

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!

2
  • Both will work. You can return something, if you want to rely on it somehow, e.g. if (functionA($arg1,$arg2 == <somethng>)) {} Commented May 16, 2015 at 16:29
  • If anything you would want it to return true for on since true == 1. Commented May 16, 2015 at 16:36

4 Answers 4

5

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.

Sign up to request clarification or add additional context in comments.

Comments

1

Your functionA will work perfectly, but for readability's sake it may be better to format it this way:

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

Comments

0

Function will be stopped after the return statement. You can read more about here: 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

Comments

0

return false returns false, a boolean. return will return NULL. Neither will execute any subsequent code.

So to expand upon RST's answer, both returns would not satisfy an if condition:

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

bar would be echoed.

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.