PHP Superglobal Variables
Superglobals are built-in variables that are always available in all scopes. Several predefined variables in PHP are "superglobals", which means they are available in all scopes throughout a script. There is no need to do global $variable; to access them within functions or methods.
What's a superglobal??
If you're thinking that these are like superheroes - they're not. As of PHP version 7.1.3 there are 9 superglobal variables. They are as follows:
An associative array containing references to all variables which are currently defined in the global scope of the script. The variable names are the keys of the array.
string 'global' (length=6)
string 'global' (length=6)
In the above example $myLocal is not displayed the second time because it is declared inside the test() function and then destroyed after the function is closed.
$_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these; servers may omit some, or provide others not listed here. That said, a large number of these
An associative array of variables passed to the current script via the URL parameters.
The above example makes use of the ternary operator. This shows how you can access the value from the URL using the $_GET superglobal. Now another example! gasp
It is possible to send multiple variables through the URL by separating them with an ampersand (&) character
An associative array of variables passed to the current script via the HTTP POST method when using
application/x-www-form-urlencoded or multipart/form-data as the HTTP Content-Type in the request.
Very similar to $_GET in that data is sent from one place to another. I'll start by going straight into an example. (I have omitted the action attribute as this will send the information to the page that the form is in).
Above is a basic form for which data can be sent. In an real environment the value attribute would not be set
meaning the form would be blank. This would then send whatever information is entered by the user.
An associative array of items uploaded to the current script via the HTTP POST method. The structure of this array is outlined in the POST method uploads section.
Note that I omitted the action attribute (again!). Also, I added enctype="multipart/form-data", this is important to any form that will be dealing with file uploads.
This is used to upload one file. Sometimes you may wish to upload more than one file. An attribute exists for that, it's called multiple.
There's an attribute for just about anything. I'm sorry
Below is an example of a form submitting multiple files.
This is a very simple example and doesn't handle problems such as file extensions that aren't allowed or files named with PHP code (like a PHP equivalent of an SQL injection). See the documentation.
The first process is checking if there are any files, and if so, set the total number of them to $total.
Using the for loop allows an iteration of the $_FILES array and accessing each item one at a time. If that file doesn't encounter a problem then the if statement is true and the code from the single file upload is run.
If an problem is encountered the switch block is executed and an error is presented in accordance with the error for that particular upload.
An associative array of variables passed to the current script via HTTP Cookies. Cookies are variables that contain data and are stored on the client's computer. Unlike the aforementioned superglobals, cookies must be created with a function (and not be assigning a value). The convention is below.
In this example a name is specified for the cookie (in this example it is "myVar"), a value is given (in this example it is "myVal", but a variable can be passed to assign its value to the cookie), and then an expiration time is given (in this example it is one hour since 3600 seconds is a minute). Despite the convention for creating a cookie being different, it is accessed in the same way as the others.
To destroy a cookie, setcookie must be called again, but the expiration time is set to any time in the past. See below
This will unset the cookies and remove it from the clients computer
An associative array containing session variables available to the current script. See the Session functions documentation for more information on how this is used.
Sessions are much like cookies except they are server side. To use sessions you must include session_start() at the top of your scripts to allow sessions to be utilised. Setting a session variable is the same as setting any other variable. See example below.
When starting a session a random ID is set as a cookie and called "PHPSESSID" nd will contain the session ID for that current session. This can be accessed by calling the session_id() function. It is possible to destroy session variables using the unset function (such that unset($_SESSION["myVar"]) would destroy that variable). The alternative is to call session_destory(). This will destroy the entire session meaning that all session variables will no longer exist.
An associative array that by default contains the contents of $_GET, $_POST and $_COOKIE
As the PHP documentation states, this is just a collation of $_GET, $_POST, and $_COOKIE all in one variable. Since it is possible for all three of those arrays to have an index with the same name, there is a setting in the php.ini file called request_order which can specify which of the three has precedence.
For instance, if it was set to "GPC", then the value of $_COOKIE will be used, as it is read from left to right meaning the $_REQUEST will set its value to $_GET, then $_POST, and then $_COOKIE and since $_COOKIE is last that is the value that is in $_REQUEST.
An associative array of variables passed to the current script via the environment method.
These variables are imported into PHP's global namespace from the environment under which the PHP parser is running. Many are provided by the shell under which PHP is running and different systems are likely running different kinds of shells, a definitive list is impossible. Please see your shell's documentation for a list of defined environment variables.
Other environment variables include the CGI variables, placed there regardless of whether PHP is running as a server module or CGI processor.
Anything stored within $_ENV is from the environment from which PHP is running in.
$_ENV is only populated if php.ini allows it