PHP变量引用和内存使用
发布时间:2020-12-13 14:14:49 所属栏目:PHP教程 来源:网络整理
导读:根据 php manual: ?php$a = $b;?// Note:// $a and $b are completely equal here. $a is not pointing to $b or vice versa.// $a and $b are pointing to the same place. 我假设: ?php$x = "something";$y = $x;$z = $x; 应消耗更多的记忆比: ?php$x =
根据
php manual:
<?php $a =& $b; ?> // Note: // $a and $b are completely equal here. $a is not pointing to $b or vice versa. // $a and $b are pointing to the same place. 我假设: <?php $x = "something"; $y = $x; $z = $x; 应消耗更多的记忆比: <?php $x = "something"; $y =& $x; $z =& $x; 因为,如果我理解正确的话,在第一种情况下,我们’重复’一些值并将其分配给$y和$z,最终有3个变量和3个内容,而在第二种情况下,我们有3个变量指向相同内容. 所以,使用如下代码: $value = "put something here,like a long lorem ipsum"; for($i = 0; $i < 100000; $i++) { ${"a$i"} =& $value; } echo memory_get_usage(true); 我期望内存使用量低于: $value = "put something here,like a long lorem ipsum"; for($i = 0; $i < 100000; $i++) { ${"a$i"} = $value; } echo memory_get_usage(true); 但是在这两种情况下,内存使用情况都是一样的. 我失踪了什么
PHP不会在作业上重复,而是写入.参见
Copy-on-Write in the PHP Language (Jan 18 2009; by Akihiko Tozawa,Michiaki Tatsubori,Tamiya Onodera and Yasuhiko Minamide; PDF file)进行科学讨论,Do not use PHP references (Jan 10,2010; by Jan Schlüter)为了一些乐趣和我自己的采取是
References to the Max更多的参考.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |