Home > Backend Development > PHP Tutorial > PHP static variable testing, beginner PHP static variable error analysis

PHP static variable testing, beginner PHP static variable error analysis

WBOY
Release: 2016-07-25 08:52:35
Original
953 people have browsed it
  1. function myfunc()

  2. {
  3. static $int;

  4. $int=0;

  5. ";
  6. }
  7. echo myfunc();
  8. echo myfunc();
  9. echo myfunc();

  10. ?>< ;/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:

  1. function myfunc()
  2. {
  3. static $int=0; //php static variable definition
  4. $int=$int+1;
  5. echo $int."
    }
  6. echo myfunc();
  7. echo myfunc();
  8. echo myfunc();
  9. ?>
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



source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template