note 14307 added to language.variables.scope.php

From: Date: Thu, 26 Jul 2001 04:09:43 +0000
Subject: note 14307 added to language.variables.scope.php
Groups: php.notes 
Request: Send a blank email to php-notes+get-15853@lists.php.net to get a copy of this message
I've noticed that global doesn't seem to behave as expected when inside of a loop inside a function. For example, the code written as: function myextract(&$valarray) { foreach($valarray as $name => $value) { global $$name; $$name = $value; } $name = 'test'; global $$name; $$name = 'abcxyz'; } $myarray = array( 'a' => 'z', 'b' => 'y', 'c' => 'x', ); myextract($myarray); echo "a => $a\n"; echo "b => $b\n"; echo "c => $c\n"; echo "test => $test\n"; will output: a => b => c => test => abcxyz But if I replace: global $$name; $$name = value; with: $GLOBALS[$name] = $value; the output becomes: a => z b => y c => x test => abcxyz I don't know why this is, but it is--at least on my 4.0.6 install. http://www.php.net/manual/en/language.variables.scope.php

« previous php.notes (#15853) next »