PHP Variable Scope Introduction _php tutorial

Source: Internet
Author: User
For example:
Copy CodeThe code is as follows:
$a = 1;
Include ' B.inc ';
?>

Here the variable $a will take effect in the include file B.inc. However, 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, and this is a local variable.
  
In PHP, global variables must be declared as globals when used in functions.
A variable declared with global in a function is a global variable that can be used outside of a function. Note: When global declares a variable, it cannot directly assign a value to the variable, it needs to be declared and then assigned.

In the global scope, you can also $GLOBALSAccessing global variables, no need to use them within functions GlobalKeyword to access the global variable. $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.
Constants can be defined and accessed anywhere, regardless of the scope of the variable;

Another important feature of the variable range is the static variable ( static variable)。 A static variable exists only in the local function domain, but its value is not lost when the program executes away from the scope. A static variable is initialized only on the first call and can be assigned a value when declared, and cannot be an expression value. If you assign a value to a declaration with the result of an expression, it causes a parse error.

When assigning a reference (a variable or object with &) to a static variable, the reference is not stored statically, and the value of the static variable is not remembered when the function is called the second time. Similarly, when a reference (a variable or object with &) is assigned to the global variable, the change in the variable does not work outside the function, and the scope is only within that function.

http://www.bkjia.com/PHPjc/326212.html www.bkjia.com true http://www.bkjia.com/PHPjc/326212.html techarticle For example: the copy code code is as follows: PHP $a = 1; include ' b.inc '; Here the variable $a will take effect in the include file B.inc. However, in a user-defined function, a local function fan ...

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