|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #48484 array_product(array()) returns 0 instead of 1
Submitted: 2009-06-06 11:22 UTC Modified: 2010-12-12 20:27 UTC
From: zhalassy at loginet dot hu Assigned: iliaa (profile)
Status: Closed Package: Math related
PHP Version: 5.2.9 OS: *
Private report: No CVE-ID: None
 [2009-06-06 11:22 UTC] zhalassy at loginet dot hu
array_product(array()) returns 0 instead of 1. Empty product is 1 (hence "1" is neutral in multiplication) in mathematics.


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.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2009-06-06 11:34 UTC] zhalassy at loginet dot hu

An identity with respect to addition is called an additive identity (often denoted as 0) and an identity with respect to multiplication is called a multiplicative identity (often denoted as 1). The distinction is used most often for sets that support both binary operations, such as rings. The multiplicative identity is often called the unit [...]

In mathematics, an algebra is unital (some authors say unitary) if it contains a multiplicative identity element (or unit), i.e. an element 1 with the property 1x = x1 = x for all elements x of the algebra.
 [2009-06-09 02:21 UTC]
The current behavior was set in bug #35014, pretty sure we should change it as you suggest.

Confirming for now.
 [2009-10-13 05:33 UTC] chrisstocktonaz at gmail dot com
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))) {
 	ZVAL_LONG(return_value, 1);
 [2010-12-12 20:27 UTC]
Automatic comment from SVN on behalf of iliaa
Log: Fixed bug 48484 (array_product() always returns 0 for an empty array).
 [2010-12-12 20:27 UTC]
-Status: Verified +Status: Closed -Assigned To: +Assigned To: iliaa
 [2010-12-12 20:27 UTC]
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
Thank you for the report, and for helping us make PHP better.

PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Apr 20 10:01:28 2024 UTC