Home >Backend Development >PHP Tutorial >php static变量的refcount为何是2,is_ref为何是1?

php static变量的refcount为何是2,is_ref为何是1?

WBOY
WBOYOriginal
2016-06-06 20:33:231427browse

<code><?php $a = 1;
    xdebug_debug_zval("a");
    static $b = 1;
    xdebug_debug_zval("b");
?>
</code>

为何执行后输出是
a: (refcount=1, is_ref=0)=1
b: (refcount=2, is_ref=1)=1
static变量和普通变量为何会有这样的区别?

回复内容:

<code><?php $a = 1;
    xdebug_debug_zval("a");
    static $b = 1;
    xdebug_debug_zval("b");
?>
</code>

为何执行后输出是
a: (refcount=1, is_ref=0)=1
b: (refcount=2, is_ref=1)=1
static变量和普通变量为何会有这样的区别?

refcount是计数次数,如果为0 就是被销毁
如果用Unset 这个数会减1
is_ref是表示引用
猜想:这表变量里的内容是另一个内存地址(也就是真实的内容),然后unset只会删除这个变量,并不会删除这个内存地址里面的东西
http://php.net/manual/zh/features.gc.refcounting-basics.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