here is how Superglobals are defined in PHP
Superglobals — Built-in variables that are always available in all scopes
And here is how $GLOBALS are defined here
$GLOBALS — References all variables available in global scope
I'm aware of the existence of 'various' scopes like local, static, closure, namespace and maybe more in PHP and I'm not looking for a reason to nitpick wording inaccuracies in the definitions. However I wonder why $GLOBALS was defined differently from Superglobals in terms of availability, not this way:
$GLOBALS — References all variables available in all scopes
here is how Superglobals are defined in PHP
Superglobals — Built-in variables that are always available in all scopes
And here is how $GLOBALS are defined here
$GLOBALS — References all variables available in global scope
I'm aware of the existence of 'various' scopes like local, static, closure, namespace and maybe more in PHP and I'm not looking for a reason to nitpick wording inaccuracies in the definitions. However I wonder why $GLOBALS was defined differently from Superglobals in terms of availability, not this way:
Share Improve this question asked Mar 6 at 12:31 RedgardRedgard 376 bronze badges 11 | Show 6 more comments$GLOBALS — References all variables available in all scopes
1 Answer
Reset to default 1There is no difference in their visibility. $GLOBALS
is a superglobal as well. This means that you can access that $GLOBALS
from variable anywhere in your code, in any scope.
Regarding your confusion, I think you've misunderstood what the documentation is telling you.
The bit from the documentation of $GLOBALS
that you quoted:
$GLOBALS — References all variables available in global scope
isn't talking about the availability of $GLOBALS
itself (that's already described in the superglobals documentation).
Instead it's telling you what the contents of $GLOBALS
is. What $GLOBALS
contains is all variables which have global scope.
And the fact that they're referenced by $GLOBALS
then gives you a nice way to read them from any scope, not just the global scope (this removes the need for you to write things like global $variable;
for every global you want to reference inside a function, for example). Note that as of PHP 8.1, $GLOBALS
, global variables cannot be modified via it - so it provides a read-only view of those values.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744976121a4604153.html
implicitly promting a reader to think that there is such a thinkg like a "global scope" and such a thin like "all scopes"
...yes, those things exist. So it should definitely prompt the reader about them. I'm not clear what you mean by this remark. – ADyson Commented Mar 6 at 13:28