Search code examples
phpreturn-valueparentheses

Why wraping return value with parentheses?


I'm reading Pro PHP Programming and in examples the author is using parentheses around return values

What is the difference between this:

function foo($x) {
   return (bar::baz($x));
}

and this:

function foo($x) {
   return bar::baz($x);
}

?


Solution

  • As others have mentioned, they are functionally the same... almost. As stated in the documentation:

    Note: You should never use parentheses around your return variable when returning by reference, as this will not work. You can only return variables by reference, not the result of a statement. If you use return ($a); then you're not returning a variable, but the result of the expression ($a) (which is, of course, the value of $a).

    The PHP manual also notes the (small) performance benefit to avoiding parentheses EDIT... see Benchmarking edits below:

    Note: Note that since return is a language construct and not a function, the parentheses surrounding its arguments are not required. It is common to leave them out, and you actually should do so as PHP has less work to do in this case.

    To be on the safe side, it's probably worth avoiding parentheses.

    BENCHMARKING/EDITS

    I decided to put php.net's note to the test to see how much "less work" PHP has to do without return values in parentheses. The answer: Even by micro-optimization standards it isn't worth worrying about. I set up this test (v5.3) to loop 100,000 times through two otherwise identical functions:

    function test1(){
        //do something
        $a=1;
        return $a;
    }
    
    function test2(){
        //do something
        $a=1;
        return ($a);
    }
    
    $array = array();
    
    for($j=0;$j<100;$j++){
        $array[$j] = array();
        $time = microtime(true);
    
        $val = 0; //set a dummy variable
        for($i=0;$i<100000;$i++){
            $val = test1();
        }
    
        $array[$j][0] = microtime(true)-$time;
    
        unset($i);
        unset($val);
        unset($time);
    
        $time = microtime(true);
    
        $val = 0; //set a dummy variable
        for($i=0;$i<100000;$i++){
            $val = test2();
        }
    
        $array[$j][1] = microtime(true)-$time;
        unset($i);
    unset($val);
    unset($time);
    }
    
    $without_p = 0;
    $with_p = 0;
    foreach($array as $values){
        $without_p +=$values[0];
        $with_p +=$values[1];
    
        echo $values[0].' vs '.$values[1]."\n"; 
    }
    echo "---------------\nAverages: \n".($without_p/count($array))." vs ".($with_p/count($array));
    

    And here are the results:

    0.032001972198486 vs 0.028002023696899
    0.032001972198486 vs 0.028001070022583
    0.032001972198486 vs 0.028002023696899
    0.028001070022583 vs 0.032001972198486
    0.028002023696899 vs 0.03200101852417
    0.02800178527832 vs 0.028002023696899
    0.032001972198486 vs 0.028001070022583
    0.032001972198486 vs 0.028002023696899
    0.028001070022583 vs 0.032001972198486
    0.028002023696899 vs 0.03200101852417
    0.028002023696899 vs 0.028002023696899
    0.03200101852417 vs 0.02800178527832
    0.032002210617065 vs 0.028000831604004
    0.028002023696899 vs 0.032001972198486
    0.028001070022583 vs 0.028002023696899
    0.032001972198486 vs 0.028001070022583
    0.032001972198486 vs 0.028002023696899
    0.028000831604004 vs 0.032002210617065
    0.02800178527832 vs 0.028001070022583
    0.032001972198486 vs 0.028002023696899
    0.032001972198486 vs 0.028001070022583
    0.028002023696899 vs 0.032001972198486
    0.028001070022583 vs 0.032001972198486
    0.028002023696899 vs 0.028000831604004
    0.032002210617065 vs 0.028002023696899
    0.032001972198486 vs 0.028000831604004
    0.028002023696899 vs 0.032001972198486
    0.028001070022583 vs 0.032001972198486
    0.028002023696899 vs 0.028001070022583
    0.032001972198486 vs 0.028002023696899
    0.028000831604004 vs 0.032002210617065
    0.028002023696899 vs 0.032000780105591
    0.028002023696899 vs 0.032001972198486
    0.028001070022583 vs 0.032001972198486
    0.028002023696899 vs 0.028001070022583
    0.032001972198486 vs 0.028002023696899
    0.032001972198486 vs 0.028001070022583
    0.028002023696899 vs 0.032001972198486
    0.032001972198486 vs 0.03200101852417
    0.032001972198486 vs 0.032001972198486
    0.028002023696899 vs 0.03200101852417
    0.032001972198486 vs 0.032001972198486
    0.028002023696899 vs 0.028001070022583
    0.032001972198486 vs 0.028002023696899
    0.032001972198486 vs 0.028001070022583
    0.02800178527832 vs 0.032002210617065
    0.028000831604004 vs 0.028002023696899
    0.032001972198486 vs 0.028001070022583
    0.032001972198486 vs 0.028002023696899
    0.03200101852417 vs 0.028002023696899
    0.032001972198486 vs 0.028001070022583
    0.028002023696899 vs 0.032001972198486
    0.028000831604004 vs 0.028002023696899
    0.032001972198486 vs 0.028001070022583
    0.032001972198486 vs 0.028002023696899
    0.032001972198486 vs 0.028001070022583
    0.028002023696899 vs 0.032001972198486
    0.028001070022583 vs 0.032001972198486
    0.028002023696899 vs 0.028000831604004
    0.032001972198486 vs 0.028002023696899
    0.032001972198486 vs 0.028001070022583
    0.032001972198486 vs 0.028002023696899
    0.03200101852417 vs 0.028002023696899
    0.028002023696899 vs 0.03200101852417
    0.032001972198486 vs 0.028002023696899
    0.032001972198486 vs 0.028001070022583
    0.028002023696899 vs 0.032001972198486
    0.028000831604004 vs 0.028002023696899
    0.032001972198486 vs 0.028001070022583
    0.032001972198486 vs 0.028002023696899
    0.032001972198486 vs 0.028001070022583
    0.032001972198486 vs 0.032001972198486
    0.028001070022583 vs 0.028002023696899
    0.032001972198486 vs 0.032001972198486
    0.028001070022583 vs 0.028002023696899
    0.032001972198486 vs 0.028000831604004
    0.032002210617065 vs 0.028002023696899
    0.032000780105591 vs 0.028002023696899
    0.028002023696899 vs 0.03200101852417
    0.028002023696899 vs 0.028002023696899
    0.03200101852417 vs 0.028002023696899
    0.032001972198486 vs 0.028002023696899
    0.028001070022583 vs 0.032001972198486
    0.028002023696899 vs 0.032000780105591
    0.028002023696899 vs 0.028002023696899
    0.03200101852417 vs 0.028002023696899
    0.028002023696899 vs 0.03200101852417
    0.028002023696899 vs 0.032001972198486
    0.028001070022583 vs 0.028002023696899
    0.032001972198486 vs 0.028000831604004
    0.028002023696899 vs 0.032001972198486
    0.028001070022583 vs 0.032001972198486
    0.028002023696899 vs 0.028001070022583
    0.032001972198486 vs 0.032001972198486
    0.032001972198486 vs 0.032001972198486
    0.032001972198486 vs 0.03200101852417
    0.032001972198486 vs 0.028002023696899
    0.032001972198486 vs 0.028001070022583
    0.028002023696899 vs 0.032001972198486
    0.028001070022583 vs 0.028002023696899
    --------------- Averages: 
    0.030041754245758 vs 0.029521646499634
    

    So (after smoothing out external effects by running this test 100 times) the total impact after 100,000 loops is barely .0005 seconds. The basic conclusion here is that the performance penalty here (at least in terms of processing time) is vanishingly small... to the point where even after 100,000 loops, the version parentheses is sometimes faster than the one using the recommended naked return.

    Of course it's still worth avoiding because of the danger you'll end up trying to return by reference at some point and will spend hours trying to find the source of your error, but performance really isn't a valid argument.