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

WBOY
Release: 2016-05-17 08:58:05
Original
1389 people have browsed it

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

复制代码 代码如下:

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

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

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

像上面的赋值方式会报错,不信你试试
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template