`static` keyword inside function?

后端 未结 7 2267
日久生厌
日久生厌 2020-11-28 04:46

I was looking at the source for Drupal 7, and I found some things I hadn\'t seen before. I did some initial looking in the php manual, but it didn\'t explain these examples.

7条回答
  •  执念已碎
    2020-11-28 05:27

    Inside a function, static means that the variable will retain its value each time the function is called during the life of the page load.

    Therefore in the example you've given, if you call a function twice, if it set $has_run to true, then the function would be able to know that it had previously been called because $has_run would still be equal to true when the function starts the second time.

    The usage of the static keyword in this context is explained in the PHP manual here: http://php.net/manual/en/language.variables.scope.php

提交回复
热议问题