Array Functions
Advertisements

PHP array_product() Function

Topic: PHP Array ReferencePrev|Next

Description

The array_product() function calculate the product of values in an array.

The following table summarizes the technical details of this function.

Return Value: Returns the product as an integer or float.
Changelog: Since PHP 5.3.6, the product of an empty array is 1. In earlier versions, this function would return 0 for an empty array.
Version: PHP 5.1.0+

Syntax

The basic syntax of the array_product() function is given with:

array_product(array);

The following example shows the array_product() function in action.

<?php
// Sample array
$numbers = array(2, 5, 8, 10, 15);
    
// Getting the product of array values
echo array_product($numbers); // Prints: 12000
?>

Parameters

The array_product() function accepts the following parameters.

Parameter Description
array Required. Specifies the array to work on.

More Examples

Here're some more examples showing how array_product() function actually works:

If any value in the array cannot be evaluated to a number, this function returns the integer 0.

<?php
// Sample array
$mixed = array(2, 5, "hello", 10, 15);
    
// Getting the product of array values
echo array_product($mixed); // Prints: 0
?>
Advertisements
Bootstrap UI Design Templates