开发者

do array() destroy all previous values?

I have an $array with some values stored on it. Now, if I do :

$array=array();

all values/in开发者_如何学运维dex are deleted? Or I need to use unset() before it?


A new array is being created with array() and this new array object is assigned to the variable $array.

The variable ($array) no longer points to the original array object -- and because PHP is a garbage collected language -- the original array object will be eligible for reclamation if (and only if) it is no longer strongly reachable from a root object. (The actual time the previous array object and objects it contained are actually deleted depends on other factors.)

Happy coding.


See PHP Garbage Collection Manual for more details -- PHP uses a hybrid GC (ref-count and cycle-breaking).


Yes the reassignment just wipes out all the data from the array. But to get clear understanding of the garbage collection please check the PHP Reference Counting Basics.


$array = array('apples', 'oranges', 'bananas');
print_r($array);
//Array ( [0] => apples [1] => oranges [2] => bananas ) 

$array = array();
print_r($array);
//Array ( )


Your intent would be clearer if you used something like

$array = null; 

(and even clearer if you used a better name than $array!)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜