Returning by reference. Parentheses around a retur

2019-02-25 03:07发布

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条回答
贼婆χ
2楼-- · 2019-02-25 03:54

A image can say thousands of words

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.

查看更多
登录 后发表回答