PHP static local static variables and global static variables summary

Source: Internet
Author: User

The reason for using a static local variable is that it cannot be used externally, but its value is still retained after it has not been used. Although using global variables can achieve the same functionality, it often causes unexpected

Characteristics of static local variables: 1. Will 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 use and save the value 2 that was left after the last invocation. Static local variables are initialized only once 3. Static properties can only be initialized to a character value or a constant and cannot use an expression. 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: function test () {Static $var = 5;     static $var = 1+1; it will 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://The global variable itself is a static storage mode, all global variables are static variable function Static_global () {g     Lobal $glo;     $glo + +; echo $glo. ' <br> '; } static_global (); 1 Static_global (); 2 Static_global (); 3 echo $glo. ' <br> '; 3 so static global variables are not used much.

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.