PHP 中的静态变量的简单使用

  静态变量的初始化只能在第一次static 声明的时候进行,这些静态变量只能在声明他的函数中访问到。

例如:

<?php
    
    function do_something(){
    static $first_flag=true;
    if($first_flag){
        $first_flag=false;
        echo 'do something<br/>';    
    }    

    echo 'haha...<br/>';
    }

    do_something();
    do_something();
    do_something();
?>

运行结果:

do something
haha...
haha...
haha...

原文地址:https://www.cnblogs.com/yshyee/p/3371623.html