array_push()和$ array []之间的区别= [英] Difference between array_push() and $array[] =
问题描述
在PHP手册中,( array_push )表示..
In the PHP manual, (array_push) says..
如果您使用 array_push()将一个元素添加到数组中,则最好 使用 $ array [] = ,因为这样就不会产生调用 功能.
If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function.
例如:
$arr = array();
array_push($arr, "stackoverflow");
print_r($arr);
vs
$arr[] = "stackoverflow";
print_r($arr);
我不明白为什么会有很大的差异.
I don't understand why there is a big difference.
推荐答案
在PHP中调用函数(例如array_push()
)时,调用会产生开销,因为PHP必须查找函数引用,找到它在内存中的位置,并执行其定义的任何代码.
When you call a function in PHP (such as array_push()
), there are overheads to the call, as PHP has to look up the function reference, find its position in memory and execute whatever code it defines.
使用$arr[] = 'some value';
不需要调用函数,并且可以直接在数据结构中实现加法.因此,在添加大量数据时,使用$arr[]
会更快,更节省资源.
Using $arr[] = 'some value';
does not require a function call, and implements the addition straight into the data structure. Thus, when adding a lot of data it is a lot quicker and resource-efficient to use $arr[]
.
这篇关于array_push()和$ array []之间的区别=的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!