note 115745 added to language.references.return
| From: | civilization28 at gmail dot com | Date: | Wed, 17 Sep 2014 01:50:02 +0000 |
| Subject: | note 115745 added to language.references.return | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-200460@lists.php.net to get a copy of this message | ||
Zayfod's example above is useful, but I feel that it needs more explanation. The point that
should be made is that a parameter passed in by reference can be changed to reference something
else, resulting in later changes to the local variable not affecting the passed in variable:
<?php
function & func_b ()
{
$some_var = 2;
return $some_var;
}
function func_a (& $param)
{
# $param is 1 here
$param = & func_b(); # Here the reference is changed and
# the "&" in "func_a (&
$param)"
# is no longer in effect at all.
# $param is 2 here
$param++; # Has no effect on $var.
}
$var = 1;
func_a($var);
# $var is still 1 here!!! Because the reference was changed.
?>
----
Server IP: 72.52.91.14
Probable Submitter: 2.122.30.156
----
Manual Page -- http://php.net/manual/en/language.references.return.php
Edit -- https://master.php.net/note/edit/115745
Del: integrated -- https://master.php.net/note/delete/115745/integrated
Del: useless -- https://master.php.net/note/delete/115745/useless
Del: bad code -- https://master.php.net/note/delete/115745/bad+code
Del: spam -- https://master.php.net/note/delete/115745/spam
Del: non-english -- https://master.php.net/note/delete/115745/non-english
Del: in docs -- https://master.php.net/note/delete/115745/in+docs
Del: other reasons-- https://master.php.net/note/delete/115745
Reject -- https://master.php.net/note/reject/115745
Search -- https://master.php.net/manage/user-notes.php