$ GLOBALS superglobal is modified when a function is passed

I came across some weird behavior in PHP:

function f($var) { // not using references foreach ($var as $k => $v) { unset($var[$k]); // shouldn't this unset from a copy?! } } print '<pre>'; var_dump($GLOBALS); // array f($GLOBALS); var_dump($GLOBALS); // null?! 

http://3v4l.org/dQmQN

Does anyone know why this is happening?

+4
source share
2 answers

Print that delete it and enable warnings to see what really happens ! =)

$GLOBALS contains GLOBALS . You unset it, which removes the actual global variable. If this were just step-by-step behavior, you would get an empty array, not NULL .

+3
source

This is because its expected behavior :

This is a supergroup or automatic global variable. It just means that it is available in all areas of the script. There is no need to make a global variable $; to access them as part of functions or Methods.

+1
source

Source: https://habr.com/ru/post/1479935/


All Articles