Php static variable test, php static variable error parsing for beginners
Function myfunc ()
- {
- Static $ int;
$ Int = 0;
Echo $ int + 1 ." ";
- }
- Echo myfunc ();
- Echo myfunc ();
- Echo myfunc ();
?>
The three values in the result are 1, 2, and 3 respectively. However, the actual result cannot be run and the syntax is incorrect. The cause of the subsequent check error is $ int + 1 ." "This statement should be written as ($ int + 1 )." ", After the change, the program will not report an error, but the value is, 1; in fact, this is not difficult to explain, although $ int is continuously adding 1, however, the result is not assigned to $ int again. $ int will increase progressively. It is correct to change the code to the following:
- Function myfunc ()
- {
- Static $ int = 0; // php static variable definition
- $ Int = $ int + 1;
- Echo $ int ."
";
- }
- Echo myfunc ();
- Echo myfunc ();
- Echo myfunc ();
- ?>
Note: the static keyword must be associated with the value assignment (php static variable modifier usage ). Staitc $ int; $ int = 0;Error. the running result is also, 1. |