-
-
function myfunc() - {
- static $int;
$int=0;
- ";
- }
- echo myfunc();
- echo myfunc();
- echo myfunc();
?>< ;/p>
-
Copy the code
The three values in the book are 1, 2, and 3 respectively
However, the actual result is that it cannot be run, and the syntax is wrong. The reason for the post-check error is that the sentence $int+1." " should be written as ($int+1)." ". After changing it, the program no longer reports an error. But the values are 1, 1, 1; in fact, this is not difficult to explain. Although $int keeps adding 1, the result is not assigned to $int again. Why does $int increase?
Modify the code to the following to make it correct:
-
- function myfunc()
- {
- static $int=0; //php static variable definition
- $int=$int+1;
- echo $int."
} - echo myfunc();
- echo myfunc();
- echo myfunc();
- ?>
Copy code
Note that the static keyword must be together with the assignment (php static static variable modifier usage), if it is written in the book
staitc $int;
$int=0;
Error, the result after running is also 1, 1, 1
|