PHP array_product() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, array_product() multiplies all the values of an array together and returns the result.

It works like array_sum() but with multiplication. An empty array gives 1.

Syntax

Call array_product() with the values you need. Start with this line.

PHP
$total = array_product([2, 3, 4]);
CallWhat it does
array_product([2, 3, 4])Returns 24.
array_product([1.5, 2])Returns 3 as a float.
array_product([])Returns 1 for an empty array.

The result is an int when every value is a whole number, and a float if any value is a float or the result gets too big.

Library declarations
PHP
array_product(array $array): int|float

Parameters

ParameterDescription
arrayRequired. The array of numbers to multiply.

Return Value

Return ValueDescription
int|floatThe product of all the values. It is 1 for an empty array.

PHP Examples

Example 1 — Multiply a List of Numbers

Find the product of three small numbers.

PHP
<?php
$numbers = [2, 3, 4];

echo array_product($numbers), PHP_EOL;

How It Works

  1. Create an array. It holds 2, 3 and 4.
  2. Call array_product(). PHP multiplies them one by one.
  3. Calculate. 2 x 3 is 6, and 6 x 4 is 24.
  4. Print the result. The output is 24.

Example 2 — Empty Arrays, Floats and Zero

See the special cases that surprise beginners.

PHP
<?php
echo array_product([]), PHP_EOL;
echo array_product([1.5, 2]), PHP_EOL;
echo gettype(array_product([1.5, 2])), PHP_EOL;
echo array_product([5, 0, 9]), PHP_EOL;

How It Works

  1. Use an empty array. The result is 1, which does not change any multiplication.
  2. Use a float. 1.5 x 2 is 3, which prints as 3.
  3. Check the type. PHP calls a float double in gettype().
  4. Include a zero. Anything multiplied by 0 is 0.

Example 3 — Factorials and Numeric Strings

Combine range() with array_product() to build a factorial.

PHP
<?php
echo array_product(range(1, 5)), PHP_EOL;
echo array_product(["2", "3"]), PHP_EOL;

How It Works

  1. Create 1 to 5 with range(). It gives [1, 2, 3, 4, 5].
  2. Multiply them. 1 x 2 x 3 x 4 x 5 is 120, the factorial of 5.
  3. Use numeric strings. PHP converts "2" and "3" to numbers.
  4. Print both results. The outputs are 120 and 6.

Frequently Asked Questions

It multiplies all the values in an array and returns the product.
It returns 1, because 1 is the value that does not change a multiplication.
array_sum() adds the values together, and array_product() multiplies them.
Yes. array_product(range(1, 5)) returns 120, the factorial of 5.
Numeric strings like "3" work. Other strings may give warnings or be treated as 0, so keep your data numeric.

Next: array_push()

array_push() adds one or more values to the end of an array and returns the new number of .

array_push() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful