PHP 局部 static 变量详解

lys2017年12月19日 0条评论

今天我们来聊聊 php 局部 static 变量

我们做个例子如下:

<?php

function test(){
    static $i = 0;
    $i++;
    echo $i."<br/>";
}
test();    //输出1
test();    //输出2
test();    //输出3
echo $i;  //报错 Notice: Undefined variable: i

可见 static局部变量只能在函数内部使用,并且只会被初始化一次。