note 21981 deleted from language.variables.scope by danbrown
| From: | danbrown@php.net | Date: | Mon, 18 May 2009 01:42:55 +0000 |
| Subject: | note 21981 deleted from language.variables.scope by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-155584@lists.php.net to get a copy of this message | ||
Note Submitter: cellog at users dot sourceforge dot net
----
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 :).