PHP variable range

Source: Internet
Author: User

For example: CopyCodeThe Code is as follows: <? PHP
$ A = 1;
Include 'B. inc ';
?>

The variable $ A will take effect in the included file B. Inc. However, in user-defined functions, a local function range will be introduced. By default, any variable used in a function is limited to a local function.

Global variables in PHP 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$ GlobalsAccessing global variables is not required in the functionGlobalKeyword to access global variables. $ 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 variables (Static variable). Static variables only exist in local function domains.ProgramWhen Execution leaves this scope, its value is not lost. 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. If a value is assigned to the expression result in the declaration, the parsing error occurs.

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.