Home  >  Article  >  Backend Development  >  静态变量赋值问题?

静态变量赋值问题?

WBOY
WBOYOriginal
2016-06-23 14:26:551549browse

$a=100;
static $b=$a;
echo $b;

请问静态变量为何不能这样赋值?


回复讨论(解决方案)

$b=100;
static $a;
$a=$b;
echo $a;

这样确可以?

静态属性只能被初始化为一个字符值或一个常量,不能使用表达式。
你可以static $a='a';或者$a=100 但是不能进行赋值运算或者其他表达式。

静态变量创建于 php 预编译期间
所以不能用普通变量赋值,因为普通变量创建于其后的运行期间

说白了就是,你不能向静态变量赋一个不存在的值

静态变量不能拿另外一个变量直接给他赋值。声明之后,才可以。

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