Superglobals vs $GLOBALS visibility in PHP - Stack Overflow

here is how Superglobals are defined in PHPSuperglobals — Built-in variables that are always available

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:

$GLOBALS — References all variables available in all scopes

Share Improve this question asked Mar 6 at 12:31 RedgardRedgard 376 bronze badges 11
  • ...because it only is intended to include global variables, not all variables. Are you asking why the wording is like that, or why that design decision was made? I would say...why would you want a variable that referenced all variables from all scopes? And actually how would that work in practice? If I define a variable in my function, I don't want it also available in another function. – ADyson Commented Mar 6 at 12:37
  • 1 Maybe an easy way to understand this is: $GLOBALS is a superglobal. – KIKO Software Commented Mar 6 at 12:55
  • @ADyson I'm asking what's the reason for saying $GLOBALS are available in global scope, and not saying $GLOBALS are available in all scopes, while a variable that belongs to the $GLOBALS array is available in all scopes, isn't it? – Redgard Commented Mar 6 at 13:12
  • @YourCommonSense You're welcome to change the title. It's not that important. – Redgard Commented Mar 6 at 13:15
  • 1 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
 |  Show 6 more comments

1 Answer 1

Reset to default 1

There 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

相关推荐

  • Superglobals vs $GLOBALS visibility in PHP - Stack Overflow

    here is how Superglobals are defined in PHPSuperglobals — Built-in variables that are always available

    23小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信