Lessons Learned: Why It’s Better Not To Use Parentheses When They’re Optional

There it is in the PHP manual for return():

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

I knew the parentheses were optional, but I’ve been merrily using them all along. And I probably would have continued doing so until I saw the second note attached to the docs:

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

No Comments Yet

No comments yet.

Comments RSS TrackBack Identifier URI

Leave a comment

 

User contributed tags for this post:

php parentheses (25) - parentheses php (5) - encode parentheses (4) - trademark in parentheses (2) - youtube tags parenthesis (2) - JSON.encode is not a function (2) - json parentheses (2) - when not to use parentheses (2) - php 2 functions in parentheses (1) - use parentheses with a movie (1) - use parentheses mysql (1) - return is not a function, parentheses are not required (1) - php parentheses in comments (1) - Return values should not have parentheses (1) - trademark parentheses (1) - php use of parentheses (1) - php parentheses in return statements (1) - when should i use parentheses in statements (1) - youtube api is not a function (1) - parenthesis around return (1) - php2 jsonencode (1) - parentheses in PHP (1) - should a reference to a photograph be in parentheses (1) - statements in parentheses (1) - display parentheses php (1) - php return paretheses (1) - parentheses around return (1) - when to use a parenthesis on you tube (1) - youtube tags in parentheses (1) - php 5.1.6 json encode (1) - xml encode parentheses (1) - parentheses mysql statements (1) - when to use parentheses (1) - when do you use parentheses for tags youtube video (1) - should return have parentheses? (1) - require with parenthese or not php (1) - when to use parentheses php (1) - php json encode parentheses (1) - php variable parenthesis set (1) - lesson learned for php programming (1) - use parentheses in variable php (1) - php is it better not to use ?> (1) - php json wrong parenthesis (1) - when should you use a parenthesis (1) - php parathenses (1) - why would you use parenthesis (1) - when to use parentheses in php (1) - parentheses how not to use (1) - parentheses around tags for youtube (1) - JSON.encode is not a function json.encode (1) - php parentheses require (1) - (re) when to use parentheses (1) - do youtube tags have to be in parentheses (1) - Do not use parens in return (1) - return with parentheses (1) - It's better not to (1) - when to use the parentheses in its (1) - lessons learned or not (1) -