note 14307 deleted from language.variables.scope.php by zak
| From: | zak@php.net | Date: | Thu, 26 Jul 2001 07:23:53 +0000 |
| Subject: | note 14307 deleted from language.variables.scope.php by zak | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-15858@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 => $an";
echo "b => $bn";
echo "c => $cn";
echo "test => $testn";
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.