PHP variable range, PHP global variables and static variables

Source: Internet
Author: User
Tags parse error
  1. !--? php $a = 1;
  2. include ' b.inc ';
  3. ?
copy code

Here the variable $a will take effect in the include file B.inc. 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. A PHP global variable must be declared global when used in a function. 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.

At the global scope, you can also access global variables through $globals, and you do not need to use the Global keyword in the function to access the globals. $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. The

Constant can be defined and accessed anywhere, regardless of the scope of the variable, and another important feature of the scope of the

variable is the static variable (variable). A PHP 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. A simple example of a PHP static variable can cause a parse error if it is assigned a value in the declaration with the result of an expression.

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.

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