The scope of a variable is the context in which it is defined (that is, its effective scope). Most PHP variables have a single range. This separate scope span also contains the files introduced by include and require. For example:
<?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 scope is introduced. Any variables that are used inside the function will be limited to the local function in the default context. For example:
<?php $a = 1;/* Global scope * /function Test () { echo $a;/* reference to local scope variable */
} Test ();? >
This script does not have any output, because the Echo statement refers to a local version of the variable $a, and within that range, it is not assigned a value. You may notice that the global variables in PHP are a little different from the C language, and in C, global variables are automatically applied in functions unless overridden by local variables. This can cause some problems, and some people may accidentally change a global variable. Global variables in PHP must be declared globally when used in functions.
Global keyword
First, an example of using global:
Example #1 Use Global
<?php $a = 1; $b = 2; function Sum () { global $a, $b; $b = $a + $b; } Sum (); echo $b;? >
The output of the above script will be "3". After you declare a global variable $a and $b in a function, all references to either variable point to its global version. PHP has no restrictions on the maximum number of global variables a function can declare.
The second way to access variables globally is to customize $GLOBALS arrays with special PHP. The previous example can be written as:
Example #2 use $GLOBALS instead of global
<?php $a = 1; $b = 2; function Sum () { $GLOBALS [' b '] = $GLOBALS [' a '] + $GLOBALS [' B ']; } Sum (); echo $b;? >
$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 following example shows the usefulness of a hyper-global variable:
Example #3 Examples of hyper-global variables and scopes
<?php function Test_global () { ///Most of the predefined variables are not "super", they need to use the ' global ' keyword to make them valid in the local area of the function. global $HTTP _post_vars; echo $HTTP _post_vars[' name ']; Superglobals are valid in any scope, and they do not require a ' global ' statement. Superglobals was introduced in PHP 4.1.0. echo $_post[' name '; }? >
Using static variables
Another important feature of the variable range is the static variable (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. Take a look at the following example:
Example #4 demonstrating the need for static variables
<?php function Test () { $a = 0; echo $a; $a + +; }? >
This function is of little use because the value of $a is set to 0 and output 0 each time it is called. Adding a variable to the $a + + does not work because the variable $a does not exist once you exit the function. To write a count function that does not lose this count value, define the variable $a as static:
Examples of Example #5 using static variables
<?php function Test () { static $a = 0; echo $a; $a + +; }? >
Now, the variable $a is initialized only the first time the test () function is called, and each time the test () function is called, the value of the $a is output and one is added.
Static variables also provide a way to handle recursive functions. A recursive function is a function that calls itself. Be careful when writing recursive functions, because they can be recursive indefinitely. You must ensure that there is sufficient method to abort the recursion. The following simple function recursively counts to 10, using a static variable $count to determine when to stop:
Example #6 static variables and recursive functions
<?php function Test () { static $count = 0; $count + +; echo $count; if ($count <) { test (); } $count--; }? >
Static variables can be declared according to the example above. If you assign a value to a declaration with the result of an expression, it causes a parse error.
Example #7 declaring static variables
<?php function foo () { static $int = 0;//correct static $int = 1+2;//Wrong (as it is an expression)
static $int = sqrt (121); Wrong (as it is an expression too) $int + +; echo $int; }? >
Static declarations are resolved at compile time.
Using the Global keyword outside of a function is not a mistake. Can be used to include files within a function.
References to global and static variables
In the Zend engine 1, it drives the PHP4, and the static and global definitions of variables are implemented in a reference manner. For example, a real global variable imported with a global statement inside a function field actually establishes a reference to a global variable. This may lead to unexpected behavior, as demonstrated in the following example:
<?php function Test_global_ref () { global $obj; $obj = &new stdclass; } function Test_global_noref () { global $obj; $obj = new Stdclass; } Test_global_ref (); Var_dump ($obj); Test_global_noref (); Var_dump ($obj);? >
The above routines will output:
Null
Object (StdClass) (0) {}
Similar behavior applies to static statements as well. References are not stored statically:
<?php function &get_instance_ref () { static $obj; Echo ' Static object: '; Var_dump ($obj); if (!isset ($obj)) { //assigns a reference to a static variable $obj = &new stdclass; } $obj->property++; return $obj; } function &get_instance_noref () { static $obj; Echo ' Static object: '; Var_dump ($obj); if (!isset ($obj)) { //assigns an object to a static variable $obj = new Stdclass; } $obj->property++; return $obj; } $obj 1 = get_instance_ref (); $still _obj1 = Get_instance_ref (); echo "\ n"; $obj 2 = Get_instance_noref (); $still _obj2 = Get_instance_noref ();? >
The above routines will output:
Static object:null static object:null static object:null static Object:object (StdClass) (1) { [" Property "]=>int (1) }
The example above demonstrates 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.