Forum Moderators: coopster

Message Too Old, No Replies

array_reduce() not consistent PHP4.3.2/4.3.10

behaviour of array_reduce() differs between PHP versions

         

MTKilpatrick

2:11 pm on Feb 14, 2005 (gmt 0)

10+ Year Member



I have some code along the lines of the following:

function conc($v, $w) {
$v .= "this".$w;
return $v;
}
$dog = array("one", "two", "three");
echo array_reduce($dog, "conc");

In PHP4.3.2 the result is "onethistwothisthree".

In PHP4.3.10 the result is "thisonethistwothisthree".

Furthermore, if I do this:

echo array_reduce($dog, "conc", "FIRST");

In PHP4.3.2 the result is "FIRSTthisonethistwothisthree".

In PHP4.3.10 the result is "0thisonethistwothisthree".

...which is even more confusing. There is a zero, not the string "FIRST" being applied to the front of the concatenation.

Which is "correct", and does anybody know why they are different? Is there a new, extra option to select either behaviour?

thanks,

Michael

hakre

4:04 pm on Feb 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



i don't know exactly any differences, but i remember php 4.3.10 was sometimes updated without the correct zend engine version. many array related functions went mad then and the 0 instead of FIRST points in that direction. but this is only an assumption. check your zend engine version and compare with php.net.

mincklerstraat

4:19 pm on Feb 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's a guess, just a guess:

You might get cross-version consistency if you make your callback function more like:


function conc($v='', $w='') {
$v .= "this".$w;
return $v;
}

When it's called the first time, if there's no first or second argument, and your function *must* have two arguments, it might just not give the return of the function, but the plain string instead -- what 4.3.2 might be doing. Have you tried this with error_reporting(E_ALL)?
If you have a function which is designed for a possibly missing second argument, you won't have registered an error and your function should work.

That said, have a closer look and maybe put conditional statements in your function for the possibility that one of these args comes in as 'null', or 'false', or doesn't come in at all, so it will always return consistently - I'm not sure that the function as stands will. You might want to make this first do some output to check the types of the arguments coming in.