Returning by reference. Parentheses around a retur

2019-02-25 03:51发布

问题:

http://php.net/manual/en/function.return.php

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

I tried this piece of code and it works:

<?php
function &one($param1) {
    $a = $param1 * 2;
    return $a;
}

function &two($param2) {
    $b = $param2 * 2;
    return ($b); //Parentheses around the return variable
}

$_1 =&one(10);
echo $_1 . "</br>"; //outputs "20"

$_2 =&two(10);
echo $_2 . "</br>"; //outputs "20", the same thing

What code example would explain it better (show what the note is talking about)?

Thanks.

回答1:

A image can explain more than thousands of words. I think both are correct you and documentation also. You should see version first because during coding we have to keep in mind that which version we are using.