Comprehensive understanding of global variable _php instances in PHP

Source: Internet
Author: User

Several super global variables (superglobals) are predefined in PHP, which means they are available in all scopes of a script. You do not need to specify, you can use in functions and classes.

PHP Super Global Variables list:

• $GLOBALS
$_server
$_request
$_post
$_get
$_files
$_env
$_cookie
$_session

In this section we will cover several commonly used super global variables, and the remaining variables will be covered in the next few chapters.

PHP $GLOBAL

$GLOBAL is a super global variable group of PHP that can be accessed in all scopes of a PHP script.

$GLOBAL is a global combined array that contains all the variables. The name of the variable is the key of the array.

The following examples describe how to use a super global variable $GLOBAL:

<?php 
$x =; 
$y =;
 
function addition () 
{ 
$GLOBALS [' z '] = $GLOBALS [' x '] + $GLOBALS [' y ']; 
}
 
addition (); 
echo $z; 
? >

In the above example, Z is a super global variable in a $globals array, which can also be accessed outside of the function.

PHP $_server

$_server is an array that contains information such as header information (header), path (path), and script location (scripts locations), and so on. The items in this array are created by the WEB server. There is no guarantee that every server will provide all the items, the server may ignore some, or provide some items that are not listed here.

The following examples show how to use the elements in $_server:

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

The following table lists the important elements in all $_server variables:

element/code Description
$_server[' Php_self '] The file name of the currently executing script, related to document root. For example, using $_server[' php_self ' in a script with address Http://example.com/test.php/foo.bar will get/test.php/foo.bar. The __FILE__ constant contains the full path and file name of the current (for example, included) file. Starting with PHP version 4.3.0, if PHP runs in command-line mode, this variable will contain the script name. Previous version This variable is not available.
$_server[' Gateway_interface '] The version of the CGI specification used by the server; for example, "cgi/1.1".
$_server[' Server_addr '] The IP address of the server where the script is currently running.
$_server[' server_name '] Hostname of the server on which the script is currently running. If the script is running on a virtual host, the name is determined by the value set by that virtual host. (such as: www.manongjc.com)
$_server[' Server_software '] The server identification string, given in the header information in response to the request. (such as: 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 '] Access the request method used by the page; for example, "get", "Head", "POST", "put".
$_server[' Request_time '] Time stamp at the start of the request. Available from PHP 5.1.0. (For example: 1377687496)
$_server[' query_string '] Query string, if any, for page access through it.
$_server[' Http_accept '] Accept in the current request header: The contents of the item, if present.
$_server[' Http_accept_charset '] Accept-charset in the current request header: The contents of the item, if present. For example: "Iso-8859-1,*,utf-8".
$_server[' Http_host '] The contents of the Host: item in the current request header, if present.
$_server[' Http_referer '] The address, if present, that directs the user to the previous page of the current page. Determined by the user agent setting. Not all user agents set up this item, and some also provide the ability to modify Http_referer. In short, this value is not trustworthy. )
$_server[' HTTPS '] If the script is accessed through the HTTPS protocol, it is set to a Non-empty value.
$_server[' REMOTE_ADDR '] The IP address of the user browsing the current page.
$_server[' Remote_host '] The host name of the user browsing the current page. DNS reverse resolution is not dependent on the user's remote_addr.
$_server[' Remote_port '] The port number used by the user's machine to connect to the WEB server.
$_server[' Script_filename '] The absolute path of the currently executing script.
$_server[' Server_admin '] This value indicates the Server_admin parameter in the Apache server configuration file. If the script is running on a virtual host, the value is the value of that virtual host. (such as: someone@manongjc.com)
$_server[' Server_port '] The port used by the WEB server. The default value is "80". If you are using SSL secure connections, this value is the HTTP port that the user sets.
$_server[' Server_signature '] A string containing the server version and the virtual host name.
$_server[' path_translated '] The basic path of the file system (the non-document root directory) where the current script resides. This is the result of a virtual to real-path image of the server.
$_server[' Script_name '] The path that contains the current script. This is useful when the page needs to point to itself. The __FILE__ constant contains the full path and file name of the current script, such as the containing file.
$_server[' Script_uri '] The URI is used to specify the page to be accessed. For example, "/index.html".


PHP $_request

PHP $_request is used to collect data submitted by HTML forms.

The following example shows the form (form) of an input field and a Submit button (submit). When the user submits the form data by clicking the "Submit" button, the form data is sent to the script file specified in the action attribute in the <form> tag. In this instance, we specify the file to process the form data. If you want other PHP files to process the data, you can modify the specified script filename. We can then use the Super global variable $_request to collect the input field data from the form:

 
 

PHP $_post

PHP $_post is widely used to collect form data, specifying this attribute in the HTML form label: "Method=" POST.

The following example shows the form (form) of an input field and a Submit button (submit). When the user submits the form data by clicking the "Submit" button, the form data is sent to the script file specified in the action attribute in the <form> tag. In this instance, we specify the file to process the form data. If you want other PHP files to process the data, you can modify the specified script filename. We can then use the Super global variable $_post to collect the input field data from the form:

 
 

PHP $_get

PHP $_get is also widely used to collect form data, specifying this attribute in the HTML form label: "method=" "get".

$_get can also collect data sent in URLs.

Suppose we have a hyperlink HTML page that contains parameters:

 
 

When the user clicks on the link "Test $GET", the parameter "subject" and "Web" will be 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 for the "test_get.php" file:

 
 

Hint: If you want to learn more about $_post and $_get, please visit our PHP form chapter.

The above is a small series for you to bring a comprehensive understanding of the global variables in PHP all content, I hope that we support cloud Habitat Community ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.