gpt4 book ai didi

传递给函数的 php 数组引用

转载 作者:可可西里 更新时间:2023-11-01 13:26:54 26 4
gpt4 key购买 nike

我有一个找不到答案的问题。

我正在构造一个非常大的数组,其中包含一个文件的十六进制值(如 $array[0]=B5 $array[1]=82 等等直到 $array[1230009])

当我创建一个函数来操作该数组中的一些偏移量并将 $array 作为引用传递时(function parse(&$array) { ... })它比我传递数组 normaly ( function parse($array) { ... } ) ..

花费的时间更长

这怎么可能?

PS:有没有更快的方法完全不使用数组?只是为了使用 $string = "B5 82 00 1E ..etc",但我需要在读取十六进制值时跟踪偏移量,因为其中一些值包含长度"

最佳答案

以下文章中的一些信息可能会有所帮助:Do not use PHP references

接近该帖子的末尾,Johannes 发布了以下代码部分(引用):

function foo(&$data) {
for ($i = 0; $i < strlen($data); $i++) {
do_something($data{$i});
}
}

$string = "... looooong string with lots of data .....";
foo(string);

随之而来的部分评论是(仍在引用):

But now in this case the developer tried to be smart and save time by passing a reference.
But well, strlen() expects a copy.
copy-on-write can't be done on references so $data will be copied for calling strlen(), strlen() will do an absolutely simple operation - in fact strlen() is one of the most trivial functions in PHP - and the copy will be destroyed immediately.

考虑到您要问的问题,您很可能处于这种情况......

关于传递给函数的 php 数组引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9284248/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com