$gg=100;
function a(){
 $gg=1000;   <--它的作用域只限於function內,不會影
echo $gg;    //響到函式以外的原先資料
};
echo $gg;
a();
echo $gg;
輸出結果:
100
1000
100
1000
100
如果想讓function內的$gg變成全域變數可使用global,或是以$GLOBALS[]陣列來做修改,
            $gg=100;
            echo $gg."<br>";
            function a(){
            global $gg;
            $gg=1000;
            echo $gg."<br>";
            };
            a();
            echo $gg."<br>";
輸出結果:
100
1000
100
1000
1000
若希望函式中的區域變數能一直存在,可以使用static來將變數設定成靜態變數,如:
function staticDemo(){
                static $num=0;
                $num++;
                echo "我使用了{$num}次的staticDemo<br>";
            }
            staticDemo();
            staticDemo();
            staticDemo();
輸出結果:
我使用了1次的staticDemo
我使用了2次的staticDemo
我使用了3次的staticDemo
我使用了1次的staticDemo
我使用了2次的staticDemo
我使用了3次的staticDemo
沒有使用static:
            function nostaticDemo(){
                $num=0;
                $num++;
                echo "我使用了{$num}次的staticDemo<br>";
            }
            nostaticDemo();
            nostaticDemo();
            nostaticDemo();
輸出結果:
我使用了1次的staticDemo
我使用了1次的staticDemo
我使用了1次的staticDemo
我使用了1次的staticDemo
我使用了1次的staticDemo
 
