Global Variables – Superglobals

Several predefined variables in PHP are “superglobals”, which means that they are always accessible, regardless of scope – and you can access them from any function, class or file without having to do anything special.

The PHP superglobal variables are:

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

$GLOBALS: $GLOBALS is a PHP super global variable which is used to access global variables from anywhere in the PHP script (also from within functions or methods). PHP stores all global variables in an array called $GLOBALS[index].

$_SERVER: $_SERVER is a PHP super global variable which holds information about headers, paths, and script locations.
example: $_SERVER[‘PHP_SELF’], $_SERVER[‘SERVER_NAME’], $_SERVER[‘HTTP_HOST’], $_SERVER[‘HTTP_REFERER’], $_SERVER[‘HTTP_USER_AGENT’];

$_REQUEST: $_REQUEST, by default, contains the contents of $_GET, $_POST and $_COOKIE

$_POST: PHP $_POST is widely used to collect form data after submitting an HTML form with method=”post”.

$_GET: PHP $_GET can also be used to collect form data after submitting an HTML form with method=”get”.

$_FILES is a super global variable which can be used to upload files.

$_ENV is used to return the environment variables form the web server.
Example: $_ENV[“HOSTNAME”], $_ENV[“USER”], $_ENV[“COMPUTERNAME”]

Leave a Reply