Explanation of the difference between static and static local variables

Source: Internet
Author: User
Local VariablesIt can be divided into dynamic storage type and static storage type from storage mode. Local variables in a function, such as not specifically declared as static storage classes, are dynamically allocated storage space by default. The internal dynamic variables are automatically freed after the function call ends. If you want the internal variables to remain in memory after the function executes, you should use a static variable. After the function has been executed, the static variable does not disappear, but it is shared among all calls to the function, that is, when the function executes again, the static variable will continue the previous result, and only be initialized the first time the function is called when the script executes. To declare a function variable to be static, you need to use the keyword Static

In the previous section, we introduced the variables of the scope of the PHP variable and the scope of the PHP variable global variables. Static variables can be applied wherever they can be applied, and once the application is successful, it will no longer accept the same application.

A static variable does not mean that it cannot change the value, and the amount of the value cannot be changed is called a constant. The value it has is mutable, and it maintains the most recent value. It is static because it does not change as the function calls and exits. That is, when the function was last called, if we assign a value to a static variable, the value remains unchanged the next time the function is called.

First, static local variables:

1. The internal variable of the static type is the same as the auto variable (that is, a local variable without static declaration), which is a local variable of a particular function, that is, the variable can only be used within the function that defines the variable, and the scope is the same The difference between the two is that the auto variable will exist and disappear with the function being called and exited, and the static class local variable will persist regardless of whether its function is called or not, although the variable continues to exist but cannot be used. If the function that defines it is called again, it can continue to be used, and the value left after the previous call is saved. In other words, the intrinsic variable of the static type is a variable that can only be used in a particular function but occupies the storage space.

2, the function body if the static variable is initialized at the same time, then the program will no longer initialize the operation (the static variable initialization statement of the basic type appearing inside the function is executed only on the first call). The initial value of an automatic variable is performed at the time of the function call, and each call to the function is given an initial value, which is equivalent to executing an assignment statement.

3. The initialization expression of a static local variable must be a constant or constant expression. Even if the local static variable is defined without an initial value, the system automatically assigns the initial values of 0 (to numeric variables) or null characters (to character variables), and the static variable is initially 0. For auto variable auto, if the initial value is not assigned, it will be an indeterminate value.

4. Consider using static local variables when you call a function multiple times and require the values of certain variables to be preserved between calls. Although this can be achieved with global variables, global variables sometimes cause unintended side effects (mainly in the scope of the variables), so it is advisable to use local static variables.

Note: Local static variables take longer to take up memory and are less readable, so try to avoid using local static variables unless necessary.

Second, static global variables

The declaration of a global variable (an external variable) is then prefixed with static, which constitutes a global variable. global variables themselves are static storage, and static global variables are, of course, static storage methods. The two are not different in how they are stored.

Static local variable and static global variable difference

The difference between the two is:

1, the scope of the non-static global variable is the whole source program, when a source program consists of multiple source files, non-static global variables are valid in each source file.

2. A static global variable restricts its scope, which is valid only within the source file that defines the variable, and cannot be used in other source files of the same source program.

From the above analysis can be seen ————

Changing the local variable to a static variable changes the way it is stored, that is, it changes its lifetime.

Changing a global variable to a static variable changes its scope and limits its scope of use. Static statically variable is placed in the program's Global store (that is, in the program's global data area, not on the stack, so it does not cause a stack overflow), so that the original assignment can be maintained at the next call. This is the difference between a stack variable and a heap variable .

The sample code looks like this:

<?php//--------------How to understand static variables-----------/** Common local variables */function local    {$loc = 0;//In this way, it is wrong to give the initial value 0 directly.    + + $loc; Echo $loc. ' <br> ';} Local (); 1local (); 1local (); 1echo ' ===================================<br/> ';    /** static local variable */function static_local () {static $local = 0;//This can not be assigned 0 value $local + +; Echo $local. ' <br> ';} Static_local (); 1static_local (); 2static_local (); 3//echo $local; Note that although the static variable is still local, it is not directly accessible outside. Echo ' =======================================<br> '; /** static global variable (in fact: the global variable itself is a static storage mode, all global variables are static variables) */function Static_global () {global $glo;//Here, can not be assigned a value of 0, of course, the value of 0,    Each call has a value of 0, and each call to the function will have a value of 1, but it is not possible to write "static" to modify it, which is wrong.    $glo + +; Echo $glo. ' <br> ';} Static_global (); 1static_global (); 2static_global (); 3?> 
Related Article

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.