Disclosure: Some links in this guide are affiliate links. We may earn a commission at no extra cost to you.
Mastering super global variables in php helps you build stronger fundamentals and apply best practices immediately. Below is a clear breakdown covering core concepts, step-by-step examples, and key takeaways.
Complete Guide: Super Global Variables In Php
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.
PHP super global variable is used to access global variables from anywhere in the PHP script. PHP Superglobal variables are accessible on the same page that defines it, as
well as outside the page. While local variableās scope is within the page that defines it.
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]. The index holds the name of the variable.
$_SERVER[āConstantNameā] : $_SERVER holds information about headers, paths, and script locations. eg. $_SERVER[āSERVER_PORTā], $_SERVER[āSERVER_NAMEā], $_SERVER[āREQUEST_URIā]
$_REQUEST[āFormElementNameā]: This can be used to collect data with both post and get method.
$_POST[āFormElementNameā] : It is used to collect value in a form with method=āpostā>. Information sent from a form is invisible to others.(can check on the address bar)
$_GET[āFormElementNameā] : It is used to collect value from a form(HTML script) sent with method=āgetā. Information sent from a form with the method=āgetā is visible to everyone(it display on the browser URL bar).
$_FILES[āFormElementNameā]: It can be used to upload files from a client computer/system to a server.
OR
$_FILES[āFormElementNameā][āArrayIndexā]: Such as File Name, File Type, File Size, File temporary name.
$_ENV: $_ENV is a superglobal that contains environment variables. Environment variables are provided by the shell under which PHP is running, so they may vary according to different shells.
$_COOKIE[āVariableNameā]: A cookie is used to identify a user. a cookie is a small file that the server embedded on a user computer.
$_SESSION[āVariableNameā]: A session variable is used to store information about a single user, and are available to all pages within one application.
Key Takeaways & Resources for Super Global Variables In Php
Review the Official PHP Manual for official specifications, or explore our Basic Syntax of PHP for related tutorials.