1. It will not change with the function call and exit. However, although the variable still exists, it cannot be used. If you call the function that defines it again, it can continue to be used, and save the value left after the previous call 2. static local variables are initialized only once. static attributes can only be initialized as one character value or a constant and cannot be used.
1. It will not change with the function call and exit. However, although the variable still exists, it cannot be used. If you call the function that defines it again, it can continue to be used, and save the value left after the previous call 2. static local variables are initialized only once. static attributes can only be initialized as one character value or a constant and cannot be used.
1. It will not change with the function call and exit. However, although the variable still exists, it cannot be used. If you call a function that defines it again, it can continue to be used and save the value left after the previous call.
2. Static local variables will only be initialized once
3. Static attributes can only be initialized as a character value or a constant, and expressions are not supported. Even if no initial value is assigned to a local static variable, the system automatically assigns the initial value 0 (For numeric variables) or null characters (for character variables). The initial value of the static variable is 0.
4. When you call a function multiple times and require that the values of some variables be retained between calls, you can consider using static local variables. Although global variables can also achieve the above purpose, global variables sometimes cause unexpected side effects, so it is best to use local static variables.
The Code is as follows:
Function test ()
{
Static $ var = 5; // static $ var = 1 + 1; an error is returned.
$ Var ++;
Echo $ var .'';
}
Test (); // 2
Test (); // 3
Test (); // 4
Echo $ var; // error: Notice: Undefined variable: var
Static global variables
The Code is as follows:
// Global variables are static storage, and all global variables are static variables.
Function static_global (){
Global $ glo;
$ Glo ++;
Echo $ glo .'
';
}
Static_global (); // 1
Static_global (); // 2
Static_global (); // 3
Echo $ glo .'
'; // 3
$ A will take effect in the included file B. inc.
The Code is as follows:
$ A = 1;
Include "B. inc ";
?>
// Test local variables
$ S1 = "out s1"; // global variable
Function say (){
$ S1 = "in s1"; // local variable
Echo "say (): $ s1 ";
}
Say (); // output local variable: in s1
Echo"
";
Echo "function out: $ s1"; // output global variable: out s1
// Test static variables
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 1 1
}
Echo"
";
For ($ I = 0; $ I <10; $ I ++ ){
Count2 (); // 1 2 3 4 5 6 7 8 9 10
}
Echo"
";
// Use global variables in functions, and add global
$ A = "php ";
$ B = "java ";
Function show (){
Echo $ a; // No output
Global $ B;
Echo $ B; // defines global and outputs java
}
Show ();
?>
Output 3
The Code is as follows:
$ A = 1;
$ B = 2;
Function Sum ()
{
Global $ a, $ B;
$ B = $ a + $ B;
}
Sum ();
Echo $ B; // 3
?>
The second way to access variables globally is to use a special PHP custom $ GLOBALS array.
The Code is as follows:
$ A = 1;
$ B = 2;
Function Sum ()
{
$ GLOBALS ["B"] = $ GLOBALS ["a"] + $ GLOBALS ["B"];
}
Sum ();
Echo $ B;
?>
Static variables also provide a method for processing recursive functions. A recursive function is a function that calls itself.
The Code is as follows:
Function Test ()
{
Static $ count = 0;
$ Count ++;
Echo $ count; // The value 12345678910.
If ($ count <10 ){
Test ();
}
// $ Count --;
}
Test ();
Summary
Local variable -- used in a function, and the scope is the function
Global variables -- outside the function, the scope is in the entire PHP file (including the files introduced by include and require), but cannot be read in the function unless it is declared as global again
Static variables are used in the function. After being called, the memory is not released, and the last value is retained. They are used to accumulate statistics.
For more details, see: http://www.111cn.net/phper/php/57862.htm