note 39124 added to language.references.pass
| From: | blistwon-phpatdesignfridgedotcom at rn2 dot php dot net | Date: | Sat, 17 Jan 2004 23:33:28 +0000 |
| Subject: | note 39124 added to language.references.pass | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-63757@lists.php.net to get a copy of this message | ||
One thing to note about passing by reference. If you plan on assigning the reference to a new
variable in your function, you must use the reference operator in the function declaration as well
as in the assignment. (The same holds true for classes.)
-------------------------------------------------------
CODE
-------------------------------------------------------
function f1(&$num) {
$num++;
}
function f2(&$num) {
$num1 = $num;
$num1++;
}
function f3(&$num) {
$num1 = &$num;
$num1++;
}
$myNum = 0;
print("Declare myNum: " . $myNum . "<br />\n");
f1($myNum);
print("Pass myNum as ref 1: " . $myNum . "<br />\n");
f2($myNum);
print("Pass myNum as ref 2: " . $myNum . "<br />\n");
f3($myNum);
print("Pass myNum as ref 3: " . $myNum . "<br />\n");
-------------------------------------------------------
-------------------------------------------------------
OUTPUT
-------------------------------------------------------
Declare myNum: 0
Pass myNum as ref 1: 1
Pass myNum as ref 2: 1
Pass myNum as ref 3: 2
-------------------------------------------------------
Hope this helps people trying to detangle any problems with pass-by-ref.
----
Manual Page -- http://www.php.net/manual/en/language.references.pass.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+39124
Delete -- http://master.php.net/manage/user-notes.php?action=delete+39124&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+39124&report=yes
Search -- http://master.php.net/manage/user-notes.php