note 21981 added to language.variables.scope
| From: | cellog at users dot sourceforge dot net | Date: | Sun, 02 Jun 2002 21:57:10 +0000 |
| Subject: | note 21981 added to language.variables.scope | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-31352@lists.php.net to get a copy of this message | ||
regarding May 27 comment.
Try this file, and you will see that what I said in my comment holds:
<?php
$testvar = 1 ;
$testvar2 = 2 ;
function foo ()
{
global $testvar ;
global $testvar2 ;
$testvar3 = 6;
$testvar = 3 ; //Assigning a constant works as expected.
echo "$testvar,$GLOBALS[testvar],$GLOBALS[testvar2],$testvar3 <br>" ; //
3,3,2,6
$testvar = 4 ; //Assigning a variable also works as expected.
echo "$testvar,$GLOBALS[testvar],$GLOBALS[testvar2],$testvar3 <br>" ; //
4,4,2,6
$testvar = &$testvar2 ; // Assiging a reference points $testvar at what $testvar2
// is pointing at, which is $GLOBALS[testvar2]
echo "$testvar,$GLOBALS[testvar],$GLOBALS[testvar2],$testvar3<br> " ; //
2,4,2,6
$testvar=5 ; // Assigning a value to testvar now modifies $GLOBALS[testvar2]
echo "$testvar,$GLOBALS[testvar],$GLOBALS[testvar2],$testvar3<br>" ; //5,4,5,6
$testvar = &$testvar3;
echo "$testvar,$GLOBALS[testvar],$GLOBALS[testvar2],$testvar3<br>" ; //6,4,5,6
$testvar = 7; // Assigning a value to testvar now modifies local $testvar3
echo "$testvar,$GLOBALS[testvar],$GLOBALS[testvar2],$testvar3<br>" ; //7,4,5,7
}
foo() ;
?>
doing $testvar = &$testvar2 assigns $testvar to point at $testvar2, which is pointing at
$GLOBALS[testvar2]. Later, I assign $testvar = &$testvar3, which points it at the local
variable.
The & operator *only* changes what the left-hand variable points at, and not the contents of
what it used to point at (to borrow from old-fashioned pointer terminology :).
--
http://www.php.net/manual/en/language.variables.scope.php
http://master.php.net/manage/user-notes.php?action=edit+21981
http://master.php.net/manage/user-notes.php?action=delete+21981
http://master.php.net/manage/user-notes.php?action=reject+21981