note 82885 added to language.references.pass
| From: | madmax at osu1 dot php dot net | Date: | Wed, 30 Apr 2008 10:25:18 +0000 |
| Subject: | note 82885 added to language.references.pass | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-138935@lists.php.net to get a copy of this message | ||
Passing by Reference Issues:
<?php
# sample function:
function foo(&$var)
{
$var++;
}
# this works:
$a = 1;
foo($b = 1);
print_r($a . '<br>'); // 1
print_r($b . '<br>'); // 2
# this produces a error:
$a = 1;
foo($b = $a); // create a copy of a
print_r($a . '<br>'); // should be 1
print_r($b . '<br>'); // should be 2
# this works:
$a = 1;
foo($b = (int)$a);
print_r($a . '<br>'); // 1
print_r($b . '<br>'); // 2
# if you want to pass a copy of a array use this function:
foo($b = DeRef($a)); // create a copy of $a
function &DeRef($var)
{
return($var);
}
?>
can someone explain why foo($b = $a) is not valid ?
----
Server IP: 69.147.83.197
Probable Submitter: 87.188.237.72
----
Manual Page -- http://www.php.net/manual/en/language.references.pass.php
Edit -- https://master.php.net/note/edit/82885
Del: integrated -- https://master.php.net/note/delete/82885/integrated
Del: useless -- https://master.php.net/note/delete/82885/useless
Del: bad code -- https://master.php.net/note/delete/82885/bad+code
Del: spam -- https://master.php.net/note/delete/82885/spam
Del: non-english -- https://master.php.net/note/delete/82885/non-english
Del: in docs -- https://master.php.net/note/delete/82885/in+docs
Del: other reasons-- https://master.php.net/note/delete/82885
Reject -- https://master.php.net/note/reject/82885
Search -- https://master.php.net/manage/user-notes.php