Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8313763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:36:09+00:00 2026-06-08T20:36:09+00:00

I’m reading Pro PHP Programming and in examples the author is using parentheses around

  • 0

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);
}

?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T20:36:11+00:00Added an answer on June 8, 2026 at 8:36 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.