小弟不才,求教static的有关问题

WBOY
Release: 2016-06-13 13:12:09
Original
868 people have browsed it

小弟不才,求教static的问题
static $ary = array();
$ary[] = 'a';
print_r($ary);die();
static $ary = array('aaa', 'bbb');
$ary[] = 'a';
print_r($ary);

为何这段代码输出的结果是
Array ( [0] => aaa [1] => bbb [2] => a )

难道static的会先执行?不会吧?!

------解决方案--------------------
是的,可以这么认为
static 被 php 解释成了一个内部函数,并先于 php 程序执行
所以静态变量可以有初值,但初值不能包含变量

换一种说法可能容易理解些:
静态变量在程序开始时就已经赋值了

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