Forum Moderators: coopster
Therefore I use reference where I need global values and otherwise plain value.
Ergophobe is very good in cases of speed, so you can either wait for him to answer, or write directly to him.
Best regards
michal Cibor
Ergophobe is very good in cases of speed
Not really, but when I'm bored, sometimes I make up short test scripts and run them through xdebug and CacheGrind, but this is one I've never tried.
Anyway, CS, the problem with your test case is that it does not take into account the PHP4 reference counting implementation. Because of that, there is really not much difference between passing by reference and passing by value in terms of performance up until the point that you start to modify elements of the array.
In other words, PHP4 is essentially passing by reference in all cases (except in the case of objects, but they get passed by reference in PHP5+). The difference between explicitly passing by reference and implicitly doing so via reference counting is what happens *after* the function call when you start manipulating data. With an explicit pass by reference, the original array will get modified. Otherwise, the array local to the function gets modified.
In other words, when I do this
$a = array(1,2,3,4,5,6,7,8,9);
$b = $a;
I'm not actually making a copy. I'm actually making a second variable that points to the same location in memory as the first variable, essentially as if it were a reference, and incrementing the reference count for that memory location by one.
See [zend.com...] for more info, but in your particular case, the relevant part is:
Note that PHP 4 is not like C: passing variables by reference is not necessarily faster than passing them by value. Indeed, in PHP 4 it is usually better to pass a variable by value, except if the function changes the passed value or if a reference (or alias, see Aliasing: added language flexibility) is being passed.Objects are usually better passed by reference unless you want copies to be made.
Note - that final line does not hold true for PHP5+
The only reason to explicitly pass by reference is that you want the function to modify the values of the original array rather than return an array. If you are just passing an array that gets parcelled up into INSERT or UPDATE statements, there shouldn't be any performance-based reason to pass by reference, no matter how big the array is. Again, objects prior to PHP5 are another matter.
A.
$array = (1,2,3,4,5,6);
array_mod($array);
db_update($array);function array_mod(&$array)
{
// do stuff to array elements
}
B.
$array = (1,2,3,4,5,6);
db_update($array);function db_update($array)
{
// do stuff to array elements
// insert into DB
}
C.
$array = (1,2,3,4,5,6);
$new_array = array_mod($array);
db_update($new_array);function array_mod($array)
{
// do stuff to array elements
}
A is the only case where you would want to explicitly pass by reference, and not for performance reasons. I also think A has the least readable code and would likely be my last choice of how to approach this.
A
$array = (1,2,3,4,5,6);
array_mod(&$array);
db_update($array);function array_mod($array)
{
// do stuff to array elements
}
I found this works when I enquired about reference. Here you see perfectly clear what is and what is not passed by reference:)
Clear enough for me
See you round
Michal Cibor
The idea is that the function definition should determine the tyep of data expected. This should be part of the interface and not left to the discretion of those who write code that interacts with the interface.