Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php array reference passing to function

Tags:

php

I have a question that I cannot find an answer.

I'm constructing a very big array that contain hex values of a file (like $array[0]=B5 $array[1]=82 and so on until $array[1230009])

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

How is that possible ?

PS: Is there any faster way not to use array at all ? Just to use a $string = "B5 82 00 1E ..etc", but I need to track the Offset as i advance in reading hex values as some of this values contains lengths"

like image 876
pufos Avatar asked Feb 14 '12 21:02

pufos


People also ask

Can I pass an array to a function in PHP?

This tutorial explains how to pass an array as an argument in a function with PHP. The process of passing an array into a function is very much the same as passing a variable into a function.

Is PHP function pass by reference?

Introduction. In PHP, arguments to a function can be passed by value or passed by reference. By default, values of actual arguments are passed by value to formal arguments which become local variables inside the function. Hence, modification to these variables doesn't change value of actual argument variable.

What is the use of $$ in PHP?

It can store any value like integer, float, char, string etc. On the other hand, the $$var_name is known as reference variable where $var_name is a normal variable. The $$var_name used to refer to the variable with the name as value of the variable $var_name.

How will you passing an argument to a function in PHP?

PHP Function Arguments Information can be passed to functions through arguments. An argument is just like a variable. Arguments are specified after the function name, inside the parentheses. You can add as many arguments as you want, just separate them with a comma.


1 Answers

There are some informations that might help in the following article : Do not use PHP references

Close to the end of that post, Johannes posts the following portion of code (quoting) :

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

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

And a 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 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.

You might well be in a situation like this one, considering the question you are asking...

like image 60
Pascal MARTIN Avatar answered Oct 25 '22 09:10

Pascal MARTIN