W3docs

PHP array_product(): Multiply All Array Elements

Learn how PHP array_product() multiplies every value in an array into a single product, how it handles floats, empty arrays, and strings.

The PHP array_product() function multiplies every value in an array together and returns the single resulting number. It is the multiplicative counterpart of array_sum(): where array_sum() adds the elements, array_product() multiplies them. Reach for it whenever you need a cumulative product — for example, the total of price × quantity, the combined factor of several percentages, or a factorial.

What this page covers

  • The function's signature and return type
  • A basic example and how the result is built up
  • How it handles floats, empty arrays, and the keys of an array
  • The string-conversion gotcha (which changed in PHP 8)
  • A practical "cart total" example and links to related functions

Syntax

array_product(array $array): int|float

array_product() takes a single argument — the array to multiply — and returns either an int or a float. The return type depends on the values: if every element is an integer the result is an int; if any element is a float, the result is a float.

Basic example

Pass an array of numbers and array_product() returns their product. Internally it starts from 1 (the multiplicative identity) and multiplies each element into a running total: 1 × 1 × 2 × 3 × 4 × 5 = 120.

php— editable, runs on the server

Output:

120

Working with floats

If any element is a float, the result is a float. This makes array_product() convenient for things like applying several rates at once.

<?php

$factors = [1.5, 2, 4];
echo array_product($factors); // 1.5 * 2 * 4

?>

Output:

12

Note: floating-point arithmetic is not exact. For money, prefer integer cents or a precision library such as BCMath rather than multiplying floats directly.

The empty array returns 1

A common surprise: an empty array returns 1, not 0. This is the mathematically correct empty product — multiplying nothing leaves the identity value 1 untouched, the same way summing nothing gives 0.

<?php

echo array_product([]); // 1

?>

Output:

1

Pitfall: non-numeric strings

array_product() expects numeric values. Numeric strings like "2" are converted to numbers automatically, so ["1", "2", "3"] multiplies to 6. But a non-numeric string like "Q" cannot be converted — and behavior changed across PHP versions:

  • PHP 8.0+ raises a TypeError/warning ("Multiplication is not supported on type string") and treats the value as 0, so the product becomes 0.
  • PHP 7 and earlier silently treated non-numeric strings as 0, also yielding 0.

Either way the final product collapses to 0, so validate or filter your input first if it may contain non-numeric strings.

php— editable, runs on the server

Output:

0

Keys are ignored

array_product() multiplies only the values — the keys, whether numeric or string, play no part. That means it works the same on an associative array as on an indexed one.

<?php

$cart = ['price' => 9.99, 'qty' => 3];
echo array_product($cart); // 9.99 * 3 = 29.97

?>

Output:

29.97

Practical example: cart line total

A typical use is collapsing a few related numbers into one figure. Here array_product() turns a unit price, quantity, and a discount factor into a final line total in a single call.

<?php

$unitPrice = 20;
$quantity  = 3;
$discount  = 0.9; // 10% off

$lineTotal = array_product([$unitPrice, $quantity, $discount]);
echo $lineTotal; // 20 * 3 * 0.9

?>

Output:

54
  • array_sum() — add the elements instead of multiplying them.
  • array_reduce() — fold an array with any custom operation when you need more than a plain product.
  • array_map() — transform values first (e.g. extract one field) before multiplying.
  • array_filter() — drop unwanted entries before passing the array in.

For a refresher on arrays themselves, see PHP Arrays.

Wrapping Up

array_product() is a quick, readable way to multiply every value in an array into one number. Remember the three behaviors that catch people out: the return type is a float as soon as one element is a float, an empty array returns 1, and non-numeric strings drag the whole result down to 0. Keep those in mind and it is a reliable tool for cumulative-product calculations.

Practice

Practice
What does the array_product() function do in PHP?
What does the array_product() function do in PHP?
Was this page helpful?