Passing a link to a php array

I have a question that I can not find the answer.

I create a very large array containing the hexadecimal values ​​of the file (for example, $array[0]=B5 $array[1]=82 , etc. up to $array[1230009] )

When I create a function to manipulate some offsets in this array and pass $array as a reference ( function parse(&$array) { ... } ), it takes WAY longer than if I pass an array normaly ( function parse($array) { ... } ) ..

How is this possible?

PS: Is there a faster way not to use an array? Just to use $ string = "B5 82 00 1E..etc", but I need to track the offset as I progress when reading hexadecimal values, as some of these values ​​contain lengths "

+6
source share
1 answer

The following information may be used in the following article: Do not use PHP links

Toward the end of this post, Johannes publishes the following piece of code (citation):

 function foo(&$data) { for ($i = 0; $i < strlen($data); $i++) { do_something($data{$i}); } } $string = "... looooong string with lots of data ....."; foo(string); 

And part of the comment that goes with it is (still quoting):

But now in this case, the developer tried to be smart and save time on passing the link.
But ok, strlen() expects a copy. Copy-to-write cannot be done by reference, so $data will be copied to call 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.

You may well be in a situation like this, given the question you ask ...

+3
source

Source: https://habr.com/ru/post/908464/


All Articles