PHP将$a的类型从integer更改为object.为什么?
发布时间:2020-12-13 21:40:35 所属栏目:PHP教程 来源:网络整理
导读:?php$a = 3;echo 'typeof $a is : ' . gettype($a) . "n"; // integer$b = $a;echo 'typeof $b es : ' . gettype($b) . "n"; // integer$c = new stdClass;$c-name = "charles";$b = $c;$b-name = "bill";echo '$c-name : ' . $c-name . "n";echo 'typeof
<?php $a = 3; echo 'typeof $a is : ' . gettype($a) . "n"; // integer $b = &$a; echo 'typeof $b es : ' . gettype($b) . "n"; // integer $c = new stdClass; $c->name = "charles"; $b = $c; $b->name = "bill"; echo '$c->name : ' . $c->name . "n"; echo 'typeof $b es : ' . gettype($b) . "n"; echo 'typeof $a is : ' . gettype($a) . "n"; // object echo 'The value of $a is : ' . $a->name; // bill ?> 输出: typeof $a is : integer typeof $b is : integer $c->name : bill typeof $b is : object typeof $a is : object The value of $a is : bill 解决方法
这是因为你设置$b来共享与$a相同的内存地址.因此,当您更改$b时,$a也会发生变化.
如果结果不合需要,请设置$b = $a而不是$b =& $a. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |