← All languages
PHP function of the day
Random

array_product

Calculate the product of all values in an array.

Description

The array_product() function returns the product of all values in an array. It multiplies all the values together, starting from 1. For an empty array, it returns 1 (the multiplicative identity).

Like array_sum(), it handles both integer and float values. Non-numeric values are converted to their numeric equivalents following PHP's type juggling rules.

Useful for calculating factorials, compound interest, combined probabilities, and any scenario requiring multiplication of a series of values.

Arguments

NameDescriptionOptional
array The array of values to multiply. No

Example

echo array_product([1, 2, 3, 4]);  // 24
echo array_product([2, 5, 10]);    // 100
echo array_product([]);             // 1

Reference