Home  >  Article  >  php教程  >  浅析php变量修饰符static的使用

浅析php变量修饰符static的使用

WBOY
WBOYOriginal
2016-06-13 11:44:22793browse

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

复制代码 代码如下:


function test(){
static $a=0;
$a++;
echo $a;
}


test();//1
test();//2
test();//3
Note: 静态变量可以按照上面的例子声明。如果在声明中用表达式的结果对其赋值会导致解析错误。

复制代码 代码如下:


static $a=0+1;
static $a=sqrt(121);


像上面的赋值方式会报错,不信你试试
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