|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2009-06-06 11:22 UTC] zhalassy at loginet dot hu
Description:
------------
array_product(array()) returns 0 instead of 1. Empty product is 1 (hence "1" is neutral in multiplication) in mathematics.
Example:
array_product(array(1,2)) * array_product(array(3,4)) === /* should be */ array_product(array()) * array_product(array(1,2,3,4));
example implementation:
function array_product($array){
$product = 1;
foreach($array as $element) $product *= $element;
return $product;
}
Reproduce code:
---------------
echo product_array(array());
Expected result:
----------------
It should return 1.
Actual result:
--------------
It returns 0.
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 09:00:01 2025 UTC |
Patch is of course trivial, posting it only to have no unassigned math bugs! Index: ext/standard/array.c =================================================================== --- ext/standard/array.c (revision 289602) +++ ext/standard/array.c (working copy) @@ -4020,7 +4020,7 @@ } if (!zend_hash_num_elements(Z_ARRVAL_P(input))) { - RETURN_LONG(0); + RETURN_LONG(1); } ZVAL_LONG(return_value, 1);