Php variable range

Source: Internet
Author: User
The scope of a variable is the context defined by it (that is, its effective range ). Most PHP variables have only one separate range. This independent range span also contains the range of file variables introduced by include and require, that is, the context defined by it (that is, its effective range ). Most PHP variables have only one separate range. This separate range span also contains the files introduced by include and require. For example:

 

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. For example:

 

This script does not have any output, because the echo statement references a local version of variable $ a and is not assigned a value within this range. You may notice that the global variables in PHP are a little different from those in C language. in C language, global variables automatically take effect in functions unless they are overwritten by local variables. This may cause some problems. some may accidentally change a global variable. Global variables in PHP must be declared as global when used in functions.

Global keyword

First, an example of using global:

Example #1 use global

 

The output of the above script is "3 ". After the global variables $ a and $ B are declared in the function, all references to any variable will point to its global version. PHP has no limit on the maximum number of global variables that a function can declare.

The second way to access variables globally is to use a special PHP custom $ GLOBALS array. The preceding example can be written as follows:

Example #2 replace global with $ GLOBALS

 

$ 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. The following example shows how to use a super global variable:

Example #3 examples of hyperglobal variables and scopes

 

Use static variables

Another important feature of variable range is static variable ). Static variables only exist in local function domains, but their values are not lost when the program runs out of this scope. Take a look at the following example:

Example #4 demonstrate examples of static variables

 

This function is useless, because every call will set the value of $ a to 0 and output 0. Adding $ a ++ to the variable does not work, because $ a does not exist once you exit this function. To write a counting function that does not lose the current count value, you must define variable $ a as static:

Example #5 examples of using static variables

 

Now, the variable $ a is initialized only when the test () function is called for the first time. every time you call the test () function, the value of $ a is output and added with one.

Static variables also provide a method for processing recursive functions. A recursive function is a function that calls itself. Be careful when writing recursive functions, because infinite recursion may occur. Make sure there are sufficient methods to abort recursion. The following simple function recursively counts to 10 and uses the static variable $ count to determine when to stop:

Example #6 static variables and recursive functions

 

Static variables can be declared according to the preceding example. If a value is assigned to the expression result in the declaration, the parsing error occurs.

Example #7 declare static variables

 

Static statements are parsed during compilation.

Using the global keyword outside the function is not an error. It can be used when a function contains files.

Reference of global and static variables

In the first generation of Zend engine, it drives PHP4, and the static and global definitions of variables are implemented by reference. For example, a real global variable imported using a global statement within a function domain is actually a reference to a global variable. This may lead to unexpected behaviors, as demonstrated in the following example:

 

The above routine will output:

NULL
Object (stdClass) (0 ){}

Similar behavior applies to static statements. References are not stored statically:

 Property ++; return $ obj;} function & get_instance_noref () {static $ obj; echo 'static object: '; var_dump ($ obj); if (! Isset ($ obj) {// assign an object to the static variable $ obj = new stdclass;} $ obj-> property ++; return $ obj ;} $ obj1 = get_instance_ref (); $ still_obj1 = get_instance_ref (); echo "\ n"; $ obj2 = response (); $ still_obj2 = get_instance_noref ();?>

The above routine will output:

  Static object: NULL    Static object: NULL    Static object: NULL    Static object: object(stdClass)(1) {        ["property"]=>int(1)    }

The preceding example shows that when a reference is assigned to a static variable, the value of the second call to the & get_instance_ref () function is not remembered.

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.