PHP static statically variable
Another important feature of variable scope is static variable. A static variable exists only in a local function field, but its value is not lost when the program executes out of this scope. Take a look at the following example:
Examples demonstrating the need for static variables
- ? Php
- function Test ()
- {
- $w 3sky = 0;
- echo $w 3sky;
- $w 3sky++;
- }
- ?>
This function is not useful because the value of $w 3sky is set to 0 and output "0" each time it is invoked. Adding a variable to the $w 3sky++ does not work, because once the function is exited, the variable $w 3sky does not exist. To write a count function that does not lose the value of this count, define the variable $w 3sky as static:
Examples of using static variables
- ? Php
- function Test ()
- {
- static $w 3sky = 0;
- echo $w 3sky;
- $w 3sky++;
- }
- ?>
Now, each call to the Test () function will output $w 3sky value and add one.