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);
}
?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
As others have mentioned, they are functionally the same… almost. As stated in the documentation:
The PHP manual also notes the (small) performance benefit to avoiding parentheses EDIT… see Benchmarking edits below:
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:
And here are the results:
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.