1. Does not change as the function is invoked and exited, however, although the variable continues to exist, it cannot be used. If the function that defines it is invoked again, it can continue to be used, and the value left after the previous call is saved
2. Static local variables are initialized only once
3. Static properties can only be initialized to a character value or a constant, and expressions cannot be used. The system automatically assigns an initial value of 0 (to a numeric variable) or a null character (to a character variable), even if the local static variable is defined without an initial initializer, and a static variable is initially 0.
4. When you call a function multiple times and require the values of certain variables to be preserved between calls, consider the use of static local variables. Although the above goal can be achieved with global variables, global variables sometimes cause unexpected side effects, so it is advisable to adopt local static variables.
The code is as follows |
Copy Code |
function test () { static $var = 5; static $var = 1+1; There will be an error. $var + +; Echo $var. ' '; }
Test (); 2 Test (); 3 Test (); 4 Echo $var; Error: notice:undefined Variable:var |
About static global variables
The code is as follows |
Copy Code |
The global variable itself is a static storage mode, all global variables are static variables function Static_global () { Global $glo; $glo + +; echo $glo. ' <br> '; }
Static_global (); 1 Static_global (); 2 Static_global (); 3 Echo $glo. ' <br> '; 3 |
$a will take effect in the include file B.inc.
The code is as follows |
Copy Code |
<?php $a = 1; Include "B.inc"; ?> <?php Local variable Test $s 1= "out S1"; Global variables
function say () { $s 1 = "in S1"; Local variables echo "Say (): $s 1"; } Say (); Output local variable: in S1 echo "<br/>"; echo "function out: $s 1"; Output global variables: out s1
Static variable test function Count1 () { $num = 0; $num + +; echo $num. " "; }
function Count2 () {//www.111cn.net static $num = 0; $num + +; echo $num. " "; }
For ($i =0 $i <10; $i + +) { Count1 (); 11 1 1 1 1 1 1 1 1 1 } echo "<br/>"; For ($i =0 $i <10; $i + +) { Count2 (); 1 2 3 4 5 6 7 8 9 10 }
echo "<br/>";
Global variables are used in functions, add global
$a = "PHP"; $b = "Java";
Function Show () { echo $a; No output Global $b; Echo $b; Define Global, Output Java } Show (); ?> |
Output 3
The code is as follows |
Copy Code |
<?php $a = 1; $b = 2;
function Sum () { Global $a, $b;
$b = $a + $b; }
Sum (); Echo $b; 3 ?> |
The second way to access a variable globally is to use a special PHP custom $GLOBALS array
The code is as follows |
Copy Code |
<?php $a = 1; $b = 2;
function Sum () { $GLOBALS ["b"] = $GLOBALS ["a"] + $GLOBALS ["B"]; }
Sum (); Echo $b; ?>
|
Static variables also provide a way to handle recursive functions. A recursive function is a function that calls itself
The code is as follows |
Copy Code |
function Test () { static $count = 0;
$count + +; Echo $count; 12345678910 if ($count < 10) { Test (); } $count--; } Test ();
|
Summarize
Local variables--used within a function, where the scope is the function
Global variables--Outside the function, the scope is in the entire PHP file (including the file included with include and require), but cannot be read in the function unless it is declared as global
Static variables--used in functions, after being called, the memory is not released, the last value is kept, and used for statistical accumulation.