- PHP Array Sorting
- PHP form Processing
The hyper-global variables introduced in PHP 4.1.0 are built-in variables that are always available in all scopes.
PHP Global Variables-Super global variables
Many of the predefined variables in PHP are "hyper-global", which means they are available in all scopes of a script. There is no need to perform a global $variable in a function or method; You can access them.
These hyper-global variables are:
- $GLOBALS
- $_server
- $_request
- $_post
- $_get
- $_files
- $_env
- $_cookie
- $_session
This section describes some of the hyper-global variables and explains the other hyper-global variables in a later section.
$GLOBALS-Referencing all variables available in the global scope
$GLOBALS This global variable is used anywhere in the PHP script to access global variables (from functions or methods).
PHP stores all global variables in an array named $GLOBALS [index]. The name of the variable is the key of the array.
The following example shows how to use the Super global variable $GLOBALS:
Instance
<?php $x = 75; $y = 25; function addition () { $GLOBALS [' z '] = $GLOBALS [' x '] + $GLOBALS [' Y ']; addition (); Echo $z;?>
Running an instance
In the above example, because Z is a variable in the $GLOBALS array, it can also be accessed outside of the function.
PHP $_server
$_server This hyper-global variable holds information about the header, path, and location of the script.
The following example shows how to use some of the elements in $_server:
Instance
<?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 ');? >
Running an instance
The following table lists the most important elements that you can access in $_server:
element/code |
Description |
$_server[' Php_self '] |
Returns the file name of the current execution script. |
$_server[' Gateway_interface '] |
Returns the version of the CGI specification used by the server. |
$_server[' Server_addr '] |
Returns the IP address of the server where the script is currently running. |
$_server[' server_name '] |
Returns the host name (such as www.w3school.com.cn) of the server on which the current run script resides. |
$_server[' Server_software '] |
Returns the server identification string (such as apache/2.2.24). |
$_server[' Server_protocol '] |
Returns the name and version of the communication protocol when the page is requested (for example, "http/1.0"). |
$_server[' Request_method '] |
Returns the request method used by the access page (for example, POST). |
$_server[' Request_time '] |
Returns the timestamp at the start of the request (for example, 1577687494). |
$_server[' query_string '] |
Returns the query string, if this page is accessed through a query string. |
$_server[' Http_accept '] |
Returns the request header from the current request. |
$_server[' Http_accept_charset '] |
Returns the Accept_charset header from the current request (for example, utf-8,iso-8859-1) |
$_server[' Http_host '] |
Returns the Host header from the current request. |
$_server[' Http_referer '] |
Returns the full URL of the current page (unreliable because it is not supported by all user agents). |
$_server[' HTTPS '] |
Whether the script is queried through the secure HTTP protocol. |
$_server[' REMOTE_ADDR '] |
Returns the IP address of the user who browsed the current page. |
$_server[' Remote_host '] |
Returns the host name of the user who browsed the current page. |
$_server[' Remote_port '] |
Returns the port number used to connect to the WEB server on the user's machine. |
$_server[' Script_filename '] |
Returns the absolute path of the current execution script. |
$_server[' Server_admin '] |
This value indicates the Server_admin parameter in the Apache server configuration file. |
$_server[' Server_port '] |
The port used by the WEB server. The default value is "80". |
$_server[' Server_signature '] |
Returns the server version and the virtual host name. |
$_server[' path_translated '] |
The base path of the file system (not the document root) where the current script resides. |
$_server[' Script_name '] |
Returns the path of the current script. |
$_server[' Script_uri '] |
Returns the URI of the current page. |
PHP $_request
PHP $_request is used to collect data submitted by HTML forms.
The following example shows a form that contains an input field and a Submit button. When the user submits the form data by clicking the Submit button, the form data is sent to the script file specified in the <form> tag's Action property. In this example, we specify the file itself to process the form data. If you need to use a different PHP file to work with your form data, please modify the file name you chose. We can then use the Super global variable $_request to collect the value of the input field:
Instance
Running an instance
PHP $_postPHP $_post is widely used to collect form data after submitting an HTML form that method= "post". $_post are also commonly used to pass variables.
The following example shows a form that contains an input field and a Submit button. When the user clicks the Submit button to submit the data, the form data is sent to the file specified in the <form> tag's Action property. In this example, we specify the file itself to process the form data. If you want to work with the form data using another PHP page, change the file name to the one you chose. We can then use the Hyper global variable $_post to collect the values of the input fields:
InstanceRunning an instance
PHP $_getPHP $_get can also be used to collect form data after the submission of an HTML form (method= "GET").
$_get can also collect the data sent in the URL.
Let's say we have a page with a hyperlink with parameters:
When the user clicks on the link "Test $GET", the Parameters "subject" and "web" are sent to "test_get.php", then you can access these values through $_get in "test_get.php".
The following example is the code in "test_get.php":
InstanceRunning an instance
Tip: You'll learn more about $_post and $_get in this section of PHP forms.
PHP Global Variables-Super global variables