Php function reference call problems

Source: Internet
Author: User
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

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.