note 58013 added to language.references.arent

From: Date: Fri, 21 Oct 2005 15:40:34 +0000
Subject: note 58013 added to language.references.arent
Groups: php.notes 
Request: Send a blank email to php-notes+get-97356@lists.php.net to get a copy of this message
Here's a thought to settle the whole (are/aren't) debacle... References are *like* pointers, but are not *identical to* pointers. The best comparison I could give is that references are "scope-limited pointers". You can change the value of one variable by changing the other as long as it is in the same scope. Once you set outside that scope (i.e. calling a function), references cease to act the same as pointers. Example 1: $a = 'eh'; $b = & $a;// $b == 'eh' $c = & $b;// $c == 'eh' $a = 'meh';// $c == 'meh' ... changing a's value will change c's value. This is where references are like pointers in C/C++ Example 2: $GLOBALS["b"] = 'b_val'; $c = 'c_val'; function foo (&$var) { $var =& $GLOBALS["b"]; //affects only 'var' copy } $GLOBALS["b"] = & $a; // $GLOBALS["b"] == 'a_val' foo($c); // the value of c will remain 'c_val' ... It might help to think of it in a different way. A calling the function by reference mimics the following behavior: $var = & $c; ... so when you if you execute this line in foo(): $var =& $GLOBALS["b"]; , you are just re-referencing var from what it was "referenced to" at the function call to a new reference. This is where references are *not* like pointers in C/C++ as the value of c is not changed as it would be if a similiar function that implemented pointers were used. I hope this clears up the confusion. ---- Manual Page -- http://www.php.net/manual/en/language.references.arent.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+58013 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58013&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58013&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58013&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58013&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58013&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58013&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58013&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+58013&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#97356) next »