Content · Programming · PHP

PHP array_product() Function: Usage and Examples

Learn how to use PHP array_product() to multiply array values, calculate totals, handle empty arrays, and avoid common numeric conversion mistakes.

3 views Published Oct 16, 2026 Updated Oct 16, 2026 Admin

When working with PHP arrays, you may need to multiply several values together. For example, you might want to calculate a product's volume, find the total number of possible combinations, or perform mathematical calculations on a list of numbers.

The array_product() function performs this calculation without requiring a manual loop.

What Is array_product() in PHP?

The array_product() function multiplies all numeric values in an array and returns their product.

Its syntax is:

array_product(array $array): int|float

The function accepts one parameter, $array, containing the values to multiply.

It returns an integer or floating-point number, depending on the calculation, and does not modify the original array.

The function has been available since PHP 5.1 and is supported in PHP 8.

Example 1: Multiplying Array Values

Let's multiply four numbers together.

<?php

$numbers = [2, 3, 4, 5];

$result = array_product($numbers);

echo $result;

Output:

120

PHP calculates 2 × 3 × 4 × 5 = 120.

You can also use decimal numbers:

<?php

$numbers = [2.5, 4, 2];

var_dump(array_product($numbers));

Output:

float(20)

When floating-point values are involved, the result may be a float.

Example 2: Working with Associative Arrays

The function also works with associative arrays. It multiplies their values and ignores their keys.

<?php

$dimensions = [
    "length" => 5,
    "width" => 4,
    "height" => 3
];

$volume = array_product($dimensions);

echo $volume;

Output:

60

The result represents the volume of a rectangular box: 5 × 4 × 3 = 60 cubic units.

The names length, width, and height do not affect the calculation.

What Happens with Zero, Negative Numbers, and Empty Arrays?

Three behaviors are important to understand:

Input Result Explanation
[2, 0, 5] 0 Multiplying by zero gives zero.
[-2, 3, 4] -24 One negative factor produces a negative result.
[] 1 The product of an empty array is defined as one.

The empty-array behavior can be surprising:

<?php

var_dump(array_product([]));

Output:

int(1)

Mathematically, one is the identity value for multiplication, just as zero is the identity value for addition.

If an empty collection should represent an unavailable or invalid calculation in your application, check for emptiness before calling array_product().

Practical Example: Calculating Product Variations

Imagine you're building an online store. A T-shirt is available in several sizes, colors, and materials.

You want to calculate the total number of possible combinations without generating every variation individually.

Create a file named product-variations.php:

<?php

declare(strict_types=1);

// Available product options.

$options = [
    "sizes" => ["S", "M", "L"],
    "colors" => ["Black", "White"],
    "materials" => ["Cotton", "Linen"]
];

// Count choices in each category.

$counts = array_map("count", $options);

// Calculate possible combinations.

$total = $options !== []
    && !in_array(0, $counts, true)
    ? array_product($counts)
    : 0;

// Display the result.

echo "<h2>Product Variations</h2>";

foreach ($counts as $category => $count) {

    $message = ucfirst($category)
        . ": "
        . $count;

    echo "<p>" . htmlspecialchars(
        $message,
        ENT_QUOTES,
        "UTF-8"
    ) . "</p>";
}

echo "<p>Total combinations: "
    . $total
    . "</p>";

Browser output:

Product Variations

Sizes: 3
Colors: 2
Materials: 2

Total combinations: 12

The calculation is 3 × 2 × 2 = 12.

First, array_map() counts the available options in each category. Then array_product() multiplies those counts.

The example also handles empty option categories. If no choices are available in a required category, the total becomes zero.

This technique is useful for product configurations, scheduling combinations, and other situations involving independent choices.

Handling Numeric Strings and Invalid Values

PHP can convert numeric strings into numbers during multiplication.

<?php

$values = ["2", "3", "4"];

echo array_product($values);

Output:

24

However, you should not assume that every string is a valid number.

Starting with PHP 8.3, array_product() emits an E_WARNING when an element cannot be converted into a number.

For external data, validate values before multiplication:

<?php

$values = ["2", "3.5", "4"];

if (array_all(
    $values,
    fn($value): bool => is_numeric($value)
)) {
    echo array_product($values);
} else {
    echo "Invalid numeric data.";
}

Output:

28

This validation example uses array_all(), which requires PHP 8.4 or later. On older versions, use a foreach loop to check every value.

Very large products may overflow integer limits and become floating-point values, which can introduce precision limitations.

array_product() vs array_sum()

Function Operation Empty Array
array_product() Multiplies all values. Returns 1.
array_sum() Adds all values. Returns 0.

For example, given [2, 3, 4], array_product() returns 24, while array_sum() returns 9.

Key Takeaways

  • array_product() multiplies numeric array values.
  • It works with indexed and associative arrays.
  • Array keys do not affect the calculation.
  • Multiplication by zero produces zero.
  • An empty array returns 1.
  • Validate external values to avoid unexpected numeric conversions and warnings.
  • The function returns a new result without modifying the original array.

The array_product() function offers a simple way to perform multiplication across PHP array values. It is particularly useful for mathematical calculations, dimensions, and counting possible combinations.

For additional details, see the official PHP array_product() documentation.

Share this resourceSend this page to someone who may find it useful.