Variables called superglobal are very important in PHP programming. These so-called superglobal variables are the built-in variables in PHP, each of which having individual scopes. These are predefined variables available in the script and can be accessed within functions or methods from any position inside a code and be used to read from or write to the core PHP structures.
$GLOBALS
The $GLOBALS superglobal refers to all the global variables that can be accessed within a function.
Example of $GLOBALS
<?php
$var1 = 1;
$var2 = 2;
function multiply()
{
$GLOBALS['x'] = $GLOBALS['var1'] * $GLOBALS['var2'];
}
multiply();
echo $x;
?>
$_SERVER
The $_SERVER superglobal is used to hold the information of a specified server such as path, headers and script location.
Example of $_SERVER
<?php
echo $SERVER['server_name'];
?>
$_GET
The $_GET superglobal gets the name of the server or variable.
Example of $_GET
<?php
echo "Your name is: " . $_GET['name'];
?>
$_POST
The $_POST superglobal posts the name of the server or variable.
Example of $_POST
<?php
echo "Your age is: " . $_POST['age'];
?>
$_FILES
The $_FILES superglobal uploads a file to a server.
$_COOKIE
The $_COOKIE superglobal is small piece of information or data sent from a website and store it in the user’s web browser.
Example of $_COOKIE
<?php
echo "Your name is saved as: " . $_COOKIE['saved_name'];
?>
$_SESSION
The $_SESSION superglobal is a way to store information in a variable or array to be used across multiple pages. The information is not stored in the user’s computer.
Example of $_SESSION
<?php
session_start();
$_SESSION['users_name'] = "Robert";
$users_name = "Newman";
echo $_SESSION['users_name'];
?>
$_REQUEST
The $_REQUEST superglobal is used to request a value of a variable.
$_ENV
The $_ENV superglobal is an environment variable. These variables are imported into PHP’s global namespace from the environment under which the PHP parser is running.
Example of $_ENV
<?php
echo "Your username is: " . $_ENV['username'];
?>
Comments
No comments have been made yet.
Please login to leave a comment. Login now