Skip to main content

PHP return(value); vs return value;



Is there any difference between return($var); and return $var; other then wrapping it in parentheses?





Source: Tips4all

Comments

  1. Unless you are returning by reference, they mean the same thing. It is preferable to exclude the parentheses. From the docs:


    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.





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

    ReplyDelete
  2. From the PHP Manual:


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


    Edit:
    This means that there is a difference between return($var) and return $var. Specifically, the former will return the value of $var and the latter will return $var itself.

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

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


    (Source)

    ReplyDelete
  4. The only time I use parenthesis is when returning two values with an array.

    return array($val,$valb);


    This example is different than your initial question, however this is the only instance where I think parenthesis are required.

    ReplyDelete
  5. Yes. If you use parenthesis it will become an expression that PHP will have to solve first, which is a waste of a few CPU cycles. Additionally, you cannot return by reference using return($var);, only by using return $var;

    This is all clearly stated in the manual.

    ReplyDelete
  6. It's a language construct, not a function, so both are working almost the same. There are two differences tho. Quoting php.net:


    If no parameter is supplied, then the
    parentheses must be omitted and NULL
    will be returned. Calling return()
    with parentheses but with no arguments
    will result in a parse error.


    and


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


    Source: php.net

    ReplyDelete
  7. They're almost the same. In fact, the parentheses don't even apply to the return keyword, but instead to the return value. So instead of looking at it like this:

    return( passing $var as an argument )


    PHP actually looks at it like this:

    $ret = ($var);
    return $ret;


    The parentheses make PHP evaluate $var which does nothing but waste time, really.

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex