Home > Article > Backend Development > What is the difference between static variables and global variables in PHP?
● The scope of global variables is from the point of definition to the end of the source file. The storage period is to allocate memory for it after definition, and the memory is released after the program exits;
● The scope of local variables is local and the storage period is definition. After allocating memory, release the memory after exiting the scope;
● Global variables default to static variables, and local variables default to automatic variables. They can also be declared as static variables. The difference between static variables and automatic variables is The difference in storage period is that the storage period of static variables is that the memory space exists during the running of the program and is released only when the program exits;
● The memory space of automatic variables only exists within the scope and is released after exiting the scope. .
Characteristics of static local variables:
1. It will not change as the function is called and exits. However, although the variable continues to exist, it cannot use it. 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
2. Static local variables will only be initialized once
3. Static properties can only be initialized to a character value or a constant, not expressions. Even if a local static variable is defined without an initial value, the system will automatically assign an initial value of 0 (for numeric variables) or a null character (for character variables); the initial value of a static variable is 0.
4. When a function is called multiple times and the values of certain variables are required to be retained between calls, static local variables can be considered. Although global variables can also be used to achieve the above purpose, global variables sometimes cause unexpected side effects, so it is still better to use local static variables.
The code is as follows:
function test() { static $var = 5; //static $var = 1+1;就会报错 $var++; echo $var . ' '; } test(); //2 test(); //3 test(); //4echo $var; //报错:Notice: Undefined variable: var
About static global variables:
The code is as follows:
//全局变量本身就是静态存储方式,所有的全局变量都是静态变量 function static_global(){ global $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.
The above are the static variables and global variables in PHP that I compiled for you. I hope it will be helpful to you in the future.
Related articles:
PHP variable scope and global variables (picture and text tutorial)
Detailed explanation of PHP class and method keyword tutorial
The above is the detailed content of What is the difference between static variables and global variables in PHP?. For more information, please follow other related articles on the PHP Chinese website!