Home  >  Article  >  Backend Development  >  function中定义static变量的问题

function中定义static变量的问题

WBOY
WBOYOriginal
2016-06-06 20:41:481386browse

<code>$count = 5;
function get_count() {
        static $count=0;
        return $count++;
}
echo get_count();
echo get_count();
</code>

输出:0 1
static在function中,执行第二次时不初始化吗?

回复内容:

<code>$count = 5;
function get_count() {
        static $count=0;
        return $count++;
}
echo get_count();
echo get_count();
</code>

输出:0 1
static在function中,执行第二次时不初始化吗?

php中的变量作用范围的另一个重要特性就是静态变量(static 变量)。静态变量仅在局部函数域中存在且只被初始化一次,当程序执行离开此作用域时,其值不会消失,会使用上次执行的结果。

静态变量仅在局部函数域中存在,但当程序执行离开此作用域时,其值并不丢失

Statement:
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