Php variable range, php global variables and static variables-php Tutorial

Source: Internet
Author: User
Php variable range, php global variables and static variables

  1. $ A = 1;
  2. Include 'B. Inc ';
  3. ?>

The variable $ a will take effect in the included file B. inc. In a user-defined function, a local function range is introduced. By default, any variable used in a function is limited to a local function. Php global variables must be declared as global when used in functions. The global declared variable in a function can be used outside the function.

Note: When global declares a variable, you cannot assign a value directly to the variable. you must declare the value first and then assign a value.

You can also use $ GLOBALS to access global variables within the global scope. you do not need to use the global keyword to access global variables in the function. $ GLOBALS is an associated array. each variable is an element. The key name corresponds to the variable name and the value corresponds to the variable content. $ GLOBALS exists globally because $ GLOBALS is a super global variable.

Constants can be defined and accessed anywhere regardless of the scope of variables;

Another important feature of variable range is static variable ). Php static variables only exist in local function domains, but their values are not lost when the program runs out of this scope. Static variables are initialized only when they are called for the first time. you can assign values to them when declaring them. they cannot be expression values. A simple example of php static variables will cause parsing errors if you assign values to the results of expressions in the declaration.

When a reference (a variable or object with &) is assigned to a static variable, the reference is not stored statically, the value of the static variable is not remembered when the second function is called. Similarly, when a reference (with & variable or object) is assigned to a global variable, the change of this variable does not work outside the function, and the scope of this variable is only within the 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.