Super what?
Every variable has scope (where it is visible/accessible). The super in superglobal refers to the fact that a superglobal variable is visible from everywhere… all scripts have access to it!
Compare this to a variable that is only available in the PHP
file it is defined in, or when you include/require a PHP
file with a variable or function defined in it. That data is only available in that file in contrast to the superglobal variable.
There are nine superglobals in PHP
. Don’t feel the need to memorize them, just be aware of their existence. Belive me, doing so will prevent you from reinventing the wheel. I’m speaking from experience here, as I made this mistake when working with data from forms. Since I didn’t know about the $_POST
superglobal I was trying to create my own array of form values! Yikes…
Superglobal variables in PHP share a couple of common characteristics. They are associative arrays (key => value pairs) and are automatically created by the PHP interpreter. This piece of software converts PHP code (a high-level programming language) into low-level machine code (that a computer can understand).
The PHP
superglobals are:
$GLOBALS
: Contains references to all variables that are currently defined in the global scope of the script. It includes superglobals as well as user-defined variables too.$_SERVER
: Holds information about server and execution environment variables, such as headers, paths, and script locations.$_GET
: Contains variables passed to the current script via theHTTP GET
method. This can be from aquery
string orHTML
form.$_POST
: Contains variables passed to the current script via theHTTP POST
method.$_FILES
: Provides access to uploaded file information, including file contents, names, types, and sizes.$_COOKIE
: Holds variables passed to the current script viaHTTP
cookies.$_SESSION
: Contains session variables available to the current script.$_REQUEST
: A combination of$_GET
,$_POST
, and$_COOKIE
. It contains the values from all three superglobals.$_ENV
: It holds variables passed to the script via the environment method (usually the server or command-line script).
Query strings and data
Modern websites involve templates and data. Let’s take my personal website as an example. I have 8 pages:
Home
Projects
About
Writing
Resume
Goals
Experiments
Contact
The HTML
of the navigation of the website looks like this. Don’t worry about the HTML
, look at the value for the href
attribute on the anchor (a
) tags. When clicked, the ?page
query string gets added to the $_GET
superglobal as the key page
, and the value is the value of the query string.
<nav class="site-menu">
<a href='?page=home' >home</a>
<a href='?page=projects' >projects</a>
<a href='?page=about' style = 'border-bottom: 2px solid maroon; padding-bottom: 2px'>about</a>
<a href='?page=experiments' >experiments</a>
<a href="?page=site-map" >site-map</a>
<a href="?page=style-guide">style guide</a>
</nav>
The user is then brought to the projects page (which shares a template with the other pages, such as heading
and paragraph
elements) with the appropriate data that is stored in a user-defined associative array. The full process is the same no matter the query string
The user clicks an anchor tag with a
?page
query string in thehref
attribute. The query string value is the name of the page, i.e. projects.The query string gets added to the
$_GET
superglobal array as a key => value pair. as an aside, the=>
is called an “arrow operator” and is used in the definition of associative arrays.A user-defined function checks the value of the
$_GET
It wasn’t that long ago that I was handwriting the HTML
for 5+ pages from scratch! Thank goodness for PHP
and superglobals like $_GET
and $_POST
.
$_GET and URLs
In PHP
, query strings are added to the superglobal $_GET
as well as URL data. For example, if you clicked projects, the value “projects” gets added to the $_GET
superglobal array. It would look like $_GET[“page”] => “projects”
if you used a function like var_dump($_GET[“page”])
to visualize the data.
In the projects example, the final URL
would look like this: url/?page=about
where URL
is the URL
of the domain, either localhost
(if developing locally) or some domain name. You can see the full URL
in the Code section below.
You may have wondered, is it possible to combine query strings to model complex information? Taking my personal website as an example, I have a projects and experiments page with an associated details page. On the projects page when a user clicks a link to a project detail, the user lands on the project detail page.
The full URL ends up being url/?page=project&slug=project-slug
where project-slug
is the valid name of a project. The query strings end up being connected by the ampersand “&” character to create a unique path for a project detail page.
If the query string isn’t valid, done by checking against a whitelist of sorts (an array of accepted values, in this case, query strings), a 404 page is displayed. After all, a user can edit the query string in the address bar of the browser. This can be a security nightmare, but that’s a topic for another post!- update: I forgot I discussed it here!
Accessing superglobal data
The rules for accessing superglobal data are the same as any other associative array. Use a function like var_dump()
to find the name of the key you want to access and fully visualize the associated value.
Now that you know what you’re working with, then access it by key. Given an array of projects JSON
data in a file named projects.json
[
{
"title": "details: responsive layout",
"project-detail-slug": "responsive-layout-cs",
"id": 0,
"description": "responsive layout"
},
{
"title": "theming challenge",
"project-detail-slug": "theming-challenge-cs",
"id": 1,
"description": "description theming challenge"
}
]
In projects.php
(that you arrived at thanks to the ?page=projects
query string) you can import and decode this data into a PHP
-friendly format, specifically an associative array using a native PHP function called json_decode($variable, bool).
In the function signature, the $variable
is a variable representing a JSON string, and bool
is a boolean
representing whether or not to decode the variable as an object or associative array. Since we want an associative array, we set this to true
.
JSON? Why not just keep it PHP?
You may ask yourself: if I’m just going to turn the JSON data into a PHP associative array anyway, why not just make it one in the first place? This is where the importance of personal projects comes into play.
If I hadn’t made a personal website for myself, I wouldn’t have seen the importance of language-agnostic data structures. JSON
(JavaScript Object Notation) is a standalone standard for data interchange, widely adopted across multiple programming languages, although its origins can be traced back to the JavaScript object syntax.
Query me this
What do you think, are the superglobals a useful feature of PHP
? Which superglobal did you use last? And for your data structures, do you prefer to use JSON or native PHP associative arrays? Why/ why not?