Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

PHP super global variable


May 11, 2021 PHP


Table of contents


PHP super global variable

What is a PHP super global variable? Super global variables are built-in variables that are always available in all scopes.

Super global variables are enabled after PHP 4.1.0 and are variables that come with the PHP system and are available in all scopes of a script.


PHP super global variable

Several superglobals are predefined in PHP, which means they are available in all scopes of a script. You don't need special instructions to use it in functions and classes.

PHP List of Super Global Variables:

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

In this section we'll cover a few commonly used super global variables, the rest of which we'll cover in the next few chapters.


PHP $GLOBALS

$GLOBALS is a super global variable group of PHP that is accessible in all scopes of a PHP script.

$GLOBALS is an array of global combinations that contain all variables. The name of the variable is the key of the array.

The following example shows how to use the hyper$GLOBALS:

<?php
$x = 75;
$y = 25;

function addition()
{
$GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
}

addition();
echo $z;
?>

Run an instance . . .

z in the example above is a super global variable in the $GLOBALS array, which can also be accessed outside the function.


PHP $_SERVER

$_SERVER is an array of information such as header, path, and script location. T he items in this array are created by the Web server. T here is no guarantee that each server will provide all the items;

The following example shows how to use the _SERVER the $2000:

<?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'];
?>

Run an instance . . .

The following table lists the _SERVER elements in all $1 variables:

Element/code Describe
$_SERVER['PHP_SELF'] The file name of the script that is currently executing, related to document root. F or example, using $http://example.com/test.php/foo.bar _SERVER in a script with an address _SERVER 'PHP_SELF' will result in a /test .php/foo.bar. _ _FILE__ constant contains the full path and file name of the current (for example, contained) file. S tarting with PHP 4.3.0, if PHP runs in command-line mode, the variable will contain the script name. The variable was not available in previous versions.
$_SERVER['GATEWAY_INTERFACE'] The version of the CGI specification used by the server;
$_SERVER['SERVER_ADDR'] The IP address of the server on which the script is currently running.
$_SERVER['SERVER_NAME'] The host name of the server on which the script is currently running. I f the script runs in a virtual host, the name is determined by the value set by that virtual host. (e.g www.w3cschool.cn)
$_SERVER['SERVER_SOFTWARE'] The server identification string, given in the header information in response to the request. (e.g. Apache/2.2.24)
$_SERVER['SERVER_PROTOCOL'] The name and version of the communication protocol when the page is requested. For example, HTTP/1.0.
$_SERVER['REQUEST_METHOD'] The request method used to access the page;
$_SERVER['REQUEST_TIME'] The timestamp at the beginning of the request. A vailable from PHP 5.1.0. (e.g. 1377687496)
$_SERVER['QUERY_STRING'] query string, if any, through which to access the page.
$_SERVER['HTTP_ACCEPT'] Accept: The contents of the item in the current request header, if any.
$_SERVER['HTTP_ACCEPT_CHARSET'] Accept-Charset in the current request header: The contents of the item, if any. For example: "iso-8859-1, s, utf-8".
$_SERVER['HTTP_HOST'] Host in the current request header: The contents of the item, if any.
$_SERVER['HTTP_REFERER'] The address, if any, that directs the user agent to the previous page of the current page. I t is determined by the user agent settings. N ot all user agents set up this item, and some provide the ability to modify HTTP_REFERER information. I n short, this value is not credible. )
$_SERVER['HTTPS'] If the script is accessed through the HTTPS protocol, it is set to a non-empty value.
$_SERVER['REMOTE_ADDR'] Browse the IP address of the user on the current page.
$_SERVER['REMOTE_HOST'] The host name of the user browsing the current page. DNS reverse resolution does not depend on the user'REMOTE_ADDR.
$_SERVER['REMOTE_PORT'] The port number used to connect to the Web server on the user's machine.
$_SERVER['SCRIPT_FILENAME'] The absolute path to the script that is currently executed.
$_SERVER['SERVER_ADMIN'] This value indicates the parameter in the Apache SERVER_ADMIN profile. I f the script runs on a virtual host, the value is the value of that virtual host. (e.g [email protected])
$_SERVER['SERVER_PORT'] The port used by the Web server. T he default is "80". If you use an SSL secure connection, this value is the HTTP port set by the user.
$_SERVER['SERVER_SIGNATURE'] A string that contains the server version and the virtual host name.
$_SERVER['PATH_TRANSLATED'] The basic path to the file system (non-document root) where the current script is located. This is the result of the server's virtual-to-real-world path image.
$_SERVER['SCRIPT_NAME'] The path that contains the current script. T his is useful when the page needs to point to itself. __FILE__ constant contains the full path and file name of the current script, such as the file.
$_SERVER['SCRIPT_URI'] The URI is used to specify which pages to visit. For example, "/index .html".


PHP $_REQUEST

PhP $_REQUEST to collect data submitted by HTML forms.

The following example shows a form of input fields and submit buttons. W hen a user submits form data by clicking the "Submit" button, the form data is sent to the script file specified in the action property in the label. I n this example, we specify a file to process the form data. I f you want other PHP files to process the data, you can modify the specified script file name. We can then use the super global variable $_REQUEST to collect input field data in the form:

<html>
<body>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="fname">
<input type="submit">
</form>

<?php
$name = $_REQUEST['fname'];
echo $name;
?>

</body>
</html>

Run an instance . . .


PHP $_POST

PhP $_POST is widely used to collect form data, specifying this property in html form labels: "method""post".

The following example shows a form of input fields and submit buttons. W hen a user submits form data by clicking the "Submit" button, the form data is sent to the script file specified in the action property in the label. I n this example, we specify a file to process the form data. I f you want other PHP files to process the data, you can modify the specified script file name. We can then use the super global variable $_POST to collect input field data in the form:

<html>
<body>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
Name: <input type="text" name="fname">
<input type="submit">
</form>

<?php
$name = $_POST['fname'];
echo $name;
?>

</body>
</html>

Run an instance . . .


PHP $_GET

PhP $_GET is also widely used to collect form data, specifying the property in the HTML form label: "method""get".

$_GET can also collect data sent in the URL.

Suppose we have a hyperlink HTML page with parameters:

<html>
<body>

<a href="test_get.php?subject=PHP&web=w3cschool.cn">Test $GET</a>

</body>
</html>

When the user clicks on the link "Test $GET," the parameters "subject" and "web" are sent to "test_get.php" and you can use the $_GET variable in the "test_get.php" file to get the data.

The following example shows the code test_get.php the "Created" file:

<html>
<body>

<?php
echo "Study " . $_GET['subject'] . " at " . $_GET['web'];
?>

</body>
</html>

Run an instance . . .

Tip: If you want to learn more about $_POST and $_GET, visit our PHP Forms section.

Next, in the next section, we'll learn about phP while looping!