php-Variable Range

Source: Internet
Author: User

Range span

Most PHP variables have a single range. This separate scope span also contains the files introduced by include and require

Here the variable $ A will take effect in the include file B.inc

<?php$a = 1;include ' B.inc ';? >

Function range

In a user-defined function, a local function scope is introduced. Any variables used inside the function will be limited to the local function by default

The following script does not have any output, because the Echo statement refers to a local version of the variable $a, and within that range, it is not assigned a value

<?php$a = 1; /* Global scope */function Test () {    echo $a;/* reference to local scope variable */}test (); >

Global keyword

The output of the following script will be "3". After the global variables a and A and B are declared in the function , all references to any of the variables point to their global version. For the maximum number of global variables a function can declare, PHP has no restrictions

<?php$a = 1; $b = 2;function Sum () {    global $a, $b;    $b = $a + $b;} Sum (); Echo $b;//3?>

The second way to access variables globally is to customize the $globals array with special PHP

Globals is an associative array, each variable is an element, the key name corresponds to the variable name, and the value corresponds to the contents of the variable. Globals exists globally because Globals is a hyper-global variable

<?php$a = 1; $b = 2;function Sum () {    $GLOBALS [' b '] = $GLOBALS [' a '] + $GLOBALS [' B '];} Sum (); Echo $b;//3?>

Pre-defined variables

For all scripts, PHP provides a number of predefined variables. These variables represent all external variables as built-in environment variables, and the error information is represented as a return header

The following is a list of predefined variables

Hyper global variable-a hyper-global variable is a built-in variable that is always available in all scopes $globals-references all the variables available in the global scope $_server-Server and execution Environment information $_get-http GET variable $_post-http POST variable $_fil Es-http File upload variable $_request-http REQUEST variable $_session-session variable $_env-environment variable $_cookie-http cookies$php_errormsg-previous error message $http_raw_post_data-native POST Data $http_response_header-http response Header $argc-The number of arguments passed to the script $argv-the parameter array passed to the script

Most of the predefined variables are not super global variables, they need to use the ' global ' keyword to make them valid in the local area of the function

Super Global variables

Hyper-Global variables are valid in any scope, and they do not require a ' global ' declaration

The following is a list of hyper-global variables

$GLOBALS $_server$_get$_post$_files$_cookie$_session$_request$_env
<?phpfunction Test_global () {    //pre-defined variable    global $HTTP _post_vars;    echo $HTTP _post_vars[' name '];    Super global variable    echo $_post[' name ';}? >

php-Variable Range

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.