Full understanding of global variables in PHP, comprehensive understanding of global variables _php tutorials

Source: Internet
Author: User
Tags php form

Comprehensive understanding of global variables in PHP and global variables


Several super global variables (superglobals) are predefined in PHP, which means they are available in all scopes of a script. You don't need to specify it, you can use it 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 common super global variables, the rest of which we will introduce 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 globally combined array that contains all the variables. The name of the variable is the key of the array.

The following example shows how to use the Super global variable $GLOBAL:

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

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

PHP $_server

$_server is an array of information, such as header information (header), 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 each 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 "
"Echo $_server[' server_name '];echo"
"Echo $_server[' Http_host '];echo"
"Echo $_server[' Http_referer '];echo"
"Echo $_server[' http_user_agent '];echo"
"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 current execution script, which is related to document root. For example, using $_server[' php_self ' in a script with an address of 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, the containing) file. Starting with PHP 4.3.0, if PHP is running in command-line mode, this variable will contain the script name. Previous versions of this variable are 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 '] The host name of the server where 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. (eg: www.manongjc.com)
$_server[' Server_software '] The server identification string, given in the header information in response to the request. (eg: 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 '] Timestamp at the start of the request. Available from PHP 5.1.0. (eg: 1377687496)
$_server[' query_string '] Query string, if any, for page access.
$_server[' Http_accept '] Accept: The contents of the item, if any, in the current request header.
$_server[' Http_accept_charset '] Accept-charset in the current request header: The contents of the item, if one exists. For example: "Iso-8859-1,*,utf-8".
$_server[' Http_host '] Host in current request header: The contents of the item, if one exists.
$_server[' Http_referer '] The address, if any, that directs the user's agent to the previous page of the current page. Determined by the user agent settings. Not all user agents will set the item, and some also provide the ability to modify the Http_referer. In short, the value is not trustworthy. )
$_server[' HTTPS '] If the script is accessed through the HTTPS protocol, it is set to a non-null value.
$_server[' REMOTE_ADDR '] The IP address of the user who browsed the current page.
$_server[' Remote_host '] The host name of the user who browsed 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 current execution 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. (eg: someone@manongjc.com)
$_server[' Server_port '] The port used by the WEB server. The default value is "80". If SSL secured connection is used, 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 base path of the file system (not the document root) where the current script resides. This is the result of the server making a virtual-to-real-path image.
$_server[' Script_name '] Contains the path to 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 a 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 will be sent to <?php $name = $_request[' fname '); echo $name;?>

PHP $_post

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

The following example shows a 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 will be sent to <?php $name = $_post[' fname '); echo $name;?>

PHP $_get

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

$_get can also collect the data sent in the URL.

Suppose we have a hyperlink HTML page that contains parameters:

Test $GET

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

<?php echo "Study". $_get[' Subject '. "At". $_get[' web '];? >

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

The above is a small series for everyone to get a comprehensive understanding of the global variables in PHP all the content, I hope that we have a lot of support to help guests home ~

http://www.bkjia.com/PHPjc/1136630.html www.bkjia.com true http://www.bkjia.com/PHPjc/1136630.html techarticle Comprehensive understanding of global variables in PHP, comprehensive understanding of global variables several super global variables (superglobals) are predefined in PHP, which means they are all in the scope of a script ...

  • 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.