Are arrays implicitly created in PHP when one of its keys are assigned something?

馋奶兔 提交于 2020-01-03 18:32:07

问题


Just wishing to quickly verify this. It is different from my immediate experience from other languages whereby an array must first be declared before it can be filled with values.


回答1:


Yes, PHP will automatically create an array given any of the following

$foo[] = $bar;
$foo[1] = $bar;
$foo['bar'] = $bar;

// and of course
$foo = array();

// and soon to pass
$foo = [1, 2, 3];



回答2:


PHP will create the array even without being implicitly declared, yes.

$array[] = ...

$array would be a valid array.




回答3:


$array['one'] = "one". Is this what your asking? Because yes, this will create a new array



来源:https://stackoverflow.com/questions/6853067/are-arrays-implicitly-created-in-php-when-one-of-its-keys-are-assigned-something

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!