Php function reference call question function & test (){
Static $ B = 1;
$ B + = 2;
Return $ B;
}
$ A = & test ();
$ A = 8;
$ C = & test (); // call the test () function again here. why not execute static $ B = 1. Because the result shows that $ c = 10.
// If static is removed, the result is 3. how can I explain this? Thank you!
Echo $ c;
Reply to discussion (solution)
Static modifies the variable $ B, so it always exists. if the static variable is removed, it will be initialized once every call.
Because $ a = & test (); (added &, address transfer), $ a is changed, so the value of the static variable $ B is also changed, therefore, when $ c = & test (); is executed, the value of $ B is 8 and 10 is returned.
Static modifies the variable $ B, so it always exists. if the static variable is removed, it will be initialized once every call.
Because $ a = & test (); (added &, address transfer), $ a is changed, so the value of the static variable $ B is also changed, therefore, when $ c = & test (); is executed, the value of $ B is 8 and 10 is returned.
Change the statement
Function & test (){
Static $ B = 1;
Static $ B = 100;
Static $ B = 1000;
// $ B = 1000;
$ B + = 2;
Return $ B;
} Again, the original static method cannot be modified. But no error is reported ~.
Static variables only assign initial values when they are declared. static variables have the nature of global variables but have different scopes.
Static variables only exist in the function scope. that is to say, static variables only exist in the stack. Generally, variables in a function are released after the function ends, such as local variables, but static variables do not. That is to say, when you call this function again, the value of this variable will be retained.
Static variables only assign initial values when they are declared. static variables have the nature of global variables, but with different scopes + 1