array_push() 和 $array[] 的区别 = [英] Difference between array_push() and $array[] =

查看:61
本文介绍了array_push() 和 $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);

对比

$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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆