1、实现变量的迭代
<?php
function Test(){
$a = 0;
echo $a;
$a++;
}
?>
每次调用Test函数,a都被重新赋值为0
<?php
function test(){
static $a = 0;
echo $a;
$a++;
}
?>
这样$a只在第一次调用时被赋值,之后在每次调用时加1,并且不会被重写。
例如:(获取将某数组弹出5个元素后的结果)
$arr = range(1,10,1);
function test($arr){
static $count=0;
array_pop($arr);
$count++;
if ($count < 5) {
test($arr);
}else{
var_dump($arr);exit;
}
}
test($arr);