You are on page 1of 11

Superglobals in PHP with Examples

[2 Steps]

What are Superglobals in PHP?


There are so many predefined global variable in PHP. Superglobal variable in
PHP are predefined global variables. Global variables are variable with global
scope, which means that nor do they need to be marked with global in functions.
All soperglobal variable are written in all-caps like, $_SERVER.

List of Superglobal variables in PHP:


• $GLOBALS
• $_SERVER
• $_REQUEST
• $_POST
• $_GET
• $_FILES
• $_ENV
• $_COOKIE
• $_SESSION

$GLOBAL Variable in PHP:

$GLOBAL is a superglobal variable in PHP which contains all global variables in


PHP script, including other superglobals. PHP stores all global variable in an
array. The global variable names act as key to their values.

Example of $GLOBAL variable in PHP:


Output:

2. $_SERVER Variable in PHP:


In this contains data about headers, scripts location and paths. Some of these
elements are used to get the information from the superglobal variable
$_SERVER.

Example of $_SERVER Variable in PHP:


Output:
3. $_REQUEST Variable in PHP:
This variable used to access the data after submission of HTML Form. Form
method can be either ‘GET’ or ‘POST’.

Here is simple example where I have submit form which is point to itself for
process with data with POST method. Now in PHP script I am using the super
global variable $_REQUEST to get the value of the input field.

Example of $_ REQUEST Variable in PHP:

Output:
4. $_POST Variable in PHP:

In this PHP $_POST is a PHP super global variable which is used to collect form
data after submitting an HTML form with method=”POST”.$_POST is also widely
used to pass variables.

Example of $_ POST Variable in PHP:


Output:

In the above code we have created a form that takes name and age of the user
and accesses the data using $_POST super global variable when they submit
the data. Since each superglobal variable is an array it can store more than one
values. Hence we retrieved name and age from the $_POST variable and stored
them in $nm and $age variables.

5. $_GET Variable in PHP:


In this $_GET super global variable is used to access form data if form is
submitted with GET method. When form uses method get to transfer data, the
data is visible in the query string, therefore the values are not hidden. $_GET
super global array variable stores the values that come in the URL.

Example of $_ GET Variable in PHP:


$_GET can also collect data sent in the URL.
NOTE: You will learn more about $_GET in the PHP Forms chapter.

Conclusion:

In this article we are going to cover Superglobals in PHP with Examples, List of
Superglobal variables in PHP.

6. $_FILES

$_FILES — HTTP File Upload variables

Description ¶

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.

The global $_FILES will contain all the uploaded file information. Its contents from
the example form is as follows. Note that this assumes the use of the file upload
name userfile, as used in the example script above. This can be any name.

$_FILES['userfile']['name']

The original name of the file on the client machine.

$_FILES['userfile']['type']

The mime type of the file, if the browser provided this information. An example
would be "image/gif". This mime type is however not checked on the PHP side
and therefore don't take its value for granted.

$_FILES['userfile']['size']

The size, in bytes, of the uploaded file.

$_FILES['userfile']['tmp_name']

The temporary filename of the file in which the uploaded file was stored on
the server.
$_FILES['userfile']['error']

The error code associated with this file upload.

$_FILES['userfile']['full_path']

The full path as submitted by the browser. This value does not always contain
a real directory structure, and cannot be trusted.

7. $_ENV
$_ENV — Environment variables

Description ¶

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.
8. $_COOKIE
$_COOKIE — HTTP Cookies

Description ¶

An associative array of variables passed to the current script via HTTP Cookies.

Examples ¶

Example #1 $_COOKIE example

<?php
echo 'Hello ' . htmlspecialchars($_COOKIE["name"]) . '!';
?>

Assuming the "name" cookie has been set earlier

The above example will output something similar to:


Hello Hannes!

9. $_SESSION
$_SESSION — Session variables

Description ¶

An associative array containing session variables available to the current script. See
the Session functions documentation for more information on how this is used.

Session Functions ¶
Table of Contents ¶

• session_abort — Discard session array changes and finish session


• session_cache_expire — Get and/or set current cache expire
• session_cache_limiter — Get and/or set the current cache limiter
• session_commit — Alias of session_write_close
• session_create_id — Create new session id
• session_decode — Decodes session data from a session encoded string
• session_destroy — Destroys all data registered to a session
• session_encode — Encodes the current session data as a session encoded string
• session_gc — Perform session data garbage collection
• session_get_cookie_params — Get the session cookie parameters
• session_id — Get and/or set the current session id
• session_module_name — Get and/or set the current session module
• session_name — Get and/or set the current session name
• session_regenerate_id — Update the current session id with a newly generated one
• session_register_shutdown — Session shutdown function
• session_reset — Re-initialize session array with original values
• session_save_path — Get and/or set the current session save path
• session_set_cookie_params — Set the session cookie parameters
• session_set_save_handler — Sets user-level session storage functions
• session_start — Start new or resume existing session
• session_status — Returns the current session status
• session_unset — Free all session variables
• session_write_close — Write session data and end session

You might also like