7 December 2006
The Difference Between PHP’s Globals and Superglobals
PHP only has two variable scopes: local and global. But it has three considerations for scoping variables: local, global and superglobal. What exactly is a superglobal?
The only superglobal variables in PHP are
- $_POST
- $_GET
- $_REQUEST
- $_COOKIE
- $_SERVER
- $_SESSION
- $_ENV
- $_FILES
These are the variables that PHP just gives you which few other languages offer. There is no way to create your own superglobal but with each of those just an ordinary associative array, you could attach your own variables anywhere.
That’s what superglobals are, but what is the real difference between globals and superglobals? It’s pretty simple. Globals are defined in the main program scope, and can be accessed in functions by calling global $variablename; within the function before using it. Superglobals are automatically marked as globals within a function’s local scope. That’s it. They just remove the requirement to declare them as globals before using them.

