global Overwrites¶
<?php
$argv = [1,2,3];
$_POST = [1,2,3];
function foo(?array $argv = null) {
if ($argv === null) {
global $argv;
}
print_r($argv);
}
foo();
foo([4,5,6]);
The global keyword is actually enough to overwrite a local variable.
In this illustration, the default value is used to trigger fetching the global variable $argv. The instruction is alone in the block, but it actually replaces the current variable, aka the parameter, by the global one. No assignation in sight, merely a surprise.
This won’t work with superglobal, such as $_POST, which can’t be overwritten by a parameter.
See Also¶
global Overwrites [Try me]
PHP Error Messages¶
PHP Features¶
Last updated: 14 July 2026