PHP Global Variable

Superglobals in PHP are predefined php global variable. These are always accessible in the PHP program.

The php global variable are:

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

$GLOBALS

$GLOBALS is a php global variable which is used to access global variables from global scope in the PHP script (also from within functions or methods). Global keyword is also used instead of $GLOBALS.

Example

<!DOCTYPE html>
<html>
<body>

<?php 
$a = 10;
$b = 20; 

function addition() {
    $GLOBALS['c'] = $GLOBALS['a'] + $GLOBALS['b'];
}

addition();
echo $c;
?>

</body>
</html>

Output

30

$_SERVER – php global variable

$ _SERVER is a php global variable that stores information such as headers, paths, and script places. Server variables are super global. You do not need a global keyword to access them. You can use any of them in your script. Entries inside this array are created not by your (programmer) but by server.

Example

<?php 
echo $_SERVER['PHP_SELF'];
echo "<br>";
echo $_SERVER['SERVER_NAME'];
echo "<br>";
echo $_SERVER['HTTP_HOST'];
echo "<br>";
echo $_SERVER['HTTP_REFERER'];
echo "<br>";
echo $_SERVER['HTTP_USER_AGENT'];
echo "<br>";
echo $_SERVER['SCRIPT_NAME'];
?>

From the $ _SERVER array, you get any sort of information from the index itself. Therefore, every index of the $ _SERVER array is called a server php global variable.

Available in PHP, all the $_SERVER variables are being described below.

Variable Description
PHP_SELFThis variable stores the file name of the script currently being executed.
argvThis is an array that stores the arguments that have been passed to the script.
argcThis array stores how many command line parameters have been passed to a list.
GATEWAY_INTERFACEIt stores the revision of the CGI specification used by the variable server.
SERVER_ADDRIt stores the current IP address of the variable server.
SERVER_NAMEIt stores the name of the variable server.
SERVER_SOFTWAREThis variable stores the server identification string, which is given in the header when responding to requests.
SERVER_PROTOCOLIt stores the name and version number of the protocol that is used for the variable use script.
REQUEST_METHODThis variable stores the method that has been used to access the page. This is a php global variable.
REQUEST_TIMEThis variable stores the time when the request was started.
REQUEST_TIME_FLOATThis variable also stores the request start time but in time the microsecond precision is stored.
QUERY_STRINGThis variable stores the query string by which the page was accessed.
DOCUMENT_ROOTThis variable stores the name of the root directory whose under current script is executing.
HTTP_ACCEPTIt stores the content of the variable accept parameter.
HTTP_ACCEPT_CHARSETThis store stores the variable accept charset.
HTTP_ACCEPT_ENCODINGThis variable contains the content of accept encoding.
HTTP_ACCEPT_LANGUAGEThis variable contains the contents of the accept language.
HTTP_CONNECTIONThe content of the connection parameter is stored in this variable.
HTTP_HOSTThis variable contains the contents of the host parameter.
HTTP_REFERERIn this variable, the address of the page is stored by which the current page user was mentioned.
HTTP_USER_AGENTThis variable stores the data of the user agent field of HTTP header. php global variable
HTTPSIf the HTTPS protocol is used for the request, then this variable is set by a positive value.
REMOTE_ADDRThis variable stores the IP address from which the user is viewing the current page.
REMOTE_HOSTThis variable stores the host name through which the user is viewing current page.
REMOTE_PORTThis variable stores the port number through which the user communicates with the machine server.
REMOTE_USERThis variable stores the name of the authenticated user.
REDIRECT_REMOTE_USERThis variable stores the name of the authenticated user if the user is internally redirected.
SCRIPT_FILENAMEThis variable stores the absolute path of the currently executable script.
SERVER_ADMINThis variable stores the value given to SERVER_ADMIN directive, which is stored in the server configuration file.
SERVER_PORTThis variable stores the server port number that is being used for communication by a server machine.
SERVER_SIGNATUREThis variable makes a string that tells the name of the server and the name of the virtual host.
PATH_TRANSLATEDIt stores the path of the current script based on variable file system.
SCRIPT_NAMEIt stores the path of variable current script.
REQUEST_URIThis stores the used URI to access the variable page.
PHP_AUTH_DIGESTThe authorization string is assigned to this variable during the Digest HTTP authentication.
PHP_AUTH_USERDuring HTTP authentication, this variable is set by the user name provided by the user.
PHP_AUTH_PWDuring HTTP authentication, this variable will be set with the password provided by the user.
AUTH_TYPEDuring the HTTP authentication this variable is set from the authentication type.
PATH_INFOThis variable stores the path name, variable name and query string.
ORIG_PATH_INFOThis variable stores PATH_INFO’s original name before being processed by PHP. php global variable

Note:-The same information was represented by the PHP $ 5.4.0 before the HTTP_SERVER_VARS array. The only difference was that the $ HTTP_SERVER_VARS array variables were not php global variable.
The use of $ HTTP_SERVER_VARS variables was limited due to not being global. This is the reason that $ HTTP_SERVER_VARS has been removed from PHP 5.4.0.

$_SERVER Variables Example

<?php
// Printing server information
echo $_SERVER[‘SERVER_NAME’];
echo $_SERVER[‘SCRIPT_NAME’];
echo $_SERVER[‘PHP_SELF’];
?>

$_REQUEST

In PHP programming, php global variable $_REQUEST is used to get data from HTML form after submitting the form.

Example

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Example of PHP $_REQUEST variable</title>
</head>
<body>
<?php
if(isset($_REQUEST["name"])){
    echo "<p>Hi, " . $_REQUEST["name"] . "</p>";
}
?>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
    <label for="inputName">Name:</label>
    <input type="text" name="name" id="inputName">
    <input type="submit" value="Submit">
</form>
</body>

$_GET – php global variable

When we use the GET Method, the GET Method form encompasses the data of a URL as follows:

www.example.com/form.php?user=Coding&password=bot

The URL created from the GET Method encoding the data in this way is called Query String. GET Method sends all the data of any form in a similar manner to the Web server by encoding it in the query string format. php global variable

Example

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Example of PHP GET method</title>
</head>
<body>
<?php
if(isset($_GET["name"])){
    echo "<p>Hi, " . $_GET["name"] . "</p>";
}
?>
<form method="get" action="<?php echo $_SERVER["PHP_SELF"];?>">
    <label for="inputName">Name:</label>
    <input type="text" name="name" id="inputName">
    <input type="submit" value="Submit">
</form>
</body>

$_POST

When using the POST method, all Parameters of Form are passed in the body of the HTTP Request. Therefore, the form’s data are not encoded in any way.

Example – php global variable

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Example of PHP POST method</title>
</head>
<body>
<?php
if(isset($_POST["name"])){
    echo "<p>Hi, " . $_POST["name"] . "</p>";
}
?>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
    <label for="inputName">Name:</label>
    <input type="text" name="name" id="inputName">
    <input type="submit" value="Submit">
</form>
</body>