Home>Article>Backend Development> What is the role of php static variables?
What are static variables?
Static variable The type specifier is static.
Static variables belong to the static storage method, and their storage space is the static data area in the memory (storage units are allocated in the static storage area). The data in this area occupies these storage spaces throughout the running of the program. (It is not released during the entire running of the program), and it can also be considered that its memory address remains unchanged until the entire program ends.
Although static variables always exist during the entire execution of the program, they cannot be used outside its scope.
As long as the keyword static is added before the variable, the variable becomes a static variable.
The role of php static variables
1. Modify variables inside the function. Static variables maintain their value while the function is called.
";; $val++; } testStatic(); //output 1 testStatic(); //output 2 testStatic(); //output 3 ?>
Program running result:
1 2 3
2. Modify attributes or methods in the class.
Modified attributes or methods can be accessed through the class name. If the modified attribute is a class attribute, the value is retained.
"; $p1=new Person(); $p2=new Person(); $p3=new Person(); echo Person::$id; //output 3 ?>
Program running result:
0 3
3. In the class Modify variables in the method.
Program running results:
The age is: 1 The age is: 2 The age is: 3 The age is: 4
For more PHP-related knowledge, please visitphp中文网!
The above is the detailed content of What is the role of php static variables?. For more information, please follow other related articles on the PHP Chinese website!