Superglobals in PHP are some pre-defined variables. These are called “Superglobals” because these are available throughout the all scopes within a script. It means you can access them within any function,files or classes.
Superglobals in PHP are mainly used to pass information within pages or different classes.
Below is the list of some superglobal variables used in PHP
– $GLOBALS
– $_SERVER
– $_REQUEST
– $_POST
– $_GET
– $_FILES
– $_ENV
– $_COOKIE
– $_SESSION
Some of the most common and widely used superglobals are explained below, rest of them will be discussed in further chapters –
$GLOBALS – Superglobals in PHP
$GLOBALS superglobal is used to access global variables anywhere within the script. In case you’ve a variable defined outside a function and you want to access, update it inside a function, you need to use $GLOBALS –
Example
In the above example c is present in $GLOBALS array, it can be accessed outside the function as well.
$_SERVER in PHP
$_SERVER superglobal holds various data values related to server such as header information, paths, current location etc.
The following example will print the current URL, HOST, SERVER NAME
For complete reference check – $_SERVER in PHP
$_REQUEST PHP
$_REQUEST superglobal in php is used to collect data when a HTML form is submitted.
The example shown below consists of a form with an input field and a submit button. When the user submits the form, the data will be send to the location specified in the “action” attribute of the form. In this example PHP_SELF is used, it means the data will be sent to this page itself. Then we can use the $_REQUEST superglobal to access the data submitted by user.
Example