What is static statically variable? (Below is the understanding in C language)
The static variable type descriptor is static.
static variables belong to static storage mode, Its storage space is a static data area in memory (allocation of storage units within the static storage), The data in this area occupies these storage spaces throughout the program's run (not released during the entire run of the program), or it can be assumed that the memory address remains the same until the entire program runs (instead, the auto auto variable, dynamic local variable, belongs to the dynamic storage category, which accounts for dynamic storage space, Released after the function call is finished). A static variable is always present throughout the execution of a program, but is not available outside its scope.
In addition, variables belonging to static storage are not necessarily static variables. For example, an external variable (a global variable in PHP) that is statically stored, but not necessarily static, must be defined by static to become a static external variable, or a static global variable.
All global variables are static variables, and local variables are local static variables only when they are defined with the type modifier static.
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, the local variable without static declaration), which is the local variable of a particular function, that is, the variable can only be used within the function that defines the variable, and the difference between the two is: Auto Auto variables exist and disappear as the function is called and exited, and the static class local variable does not, it 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,theintrinsic 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.
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.
Third, the application in PHP
<?php//--------------How to interpret static static variables-----------/** Common local Variables */function local () {$loc = 0;//This way, if you do not give the initial value 0 is wrong. + + $loc; Echo $loc. ' <br> ';} Local (); 1local (); 1local (); 1echo ' ===================================<br/> ';/** static local variable */function static_local () {Static $local = 0 ; You can not assign 0 value $local++;echo $local here. ' <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, after each invocation of its values are 0, each call to the functions of the value will be 1, but can not be assumed to write "static" to be decorated, that is wrong. $glo ++;echo $glo. ' <br> ';} Static_global (); 1static_global (); 2static_global (); 3?>
PHP static variable details (a)