Cannot be restored when the variable value is Superglobals
L2ksy0d opened this issue · 4 comments
sample:
<?php
$a = $_POST['a'];
$b = $_GET['b'];
$c = 'assert';
$a($b);
$c($a);
echo "aaa";
it doesn't seem to get a value, and it appears empty.
What is it that is wrong?
I get this, which looks correct:
<?php
$a = $_POST['a'];
$b = $_GET['b'];
$c = 'assert';
$a($b);
assert($a);
echo "aaa";
Oh, I mean can the value of $a be restored, because it's the special variable "Superglobals".
Just like $c($a)
is restored to assert($_POST['a'])
.
Okay, so in a simple example like:
$a = ...
$b = $a;
echo $b;
Then echo $b
can be replaced by echo $a
?
This won't work in general, as copies are done by-value, which is fine for scalar values but not for arrays.
I don't think superglobals will be any different here.
In an example such as:
$a = [1, 2, 3];
$b = $a;
array_pop($b);
You can't replace array_pop($b)
with array_pop($a)
because they are different arrays. array_pop($b)
only affects array $b
, not array $a
.
I've also just realised that it's unsound to assume $c
is always "assert" when they are global variables. e.g. consider:
function change_globals() {
global $c;
$c = 'foo';
}
$a = $_POST['a'];
$b = $_GET['b'];
$c = 'assert';
$a($b);
$c($a);
echo "aaa";
If $_POST['a']
was the string "change_globals", then suddenly $c
is no longer "assert" but is instead "foo".
tks for your answer.