PHP Function Reference

PHP array_product() Function



The PHP array_product() function returns the product of all values in the array. Please note that, the function converts string elements of the array into numbers. If a element can not be converted into number, it is converted to 0.

Syntax

array_product(array)

Parameters

array Required. Specify an array.

Return Value

Returns the product of all values in the array.

Exceptions

NA.

Example:

In the example below, array_product() function is used to calculate the product of all values in the given indexed array.

<?php
$MyArray = array(1, 2, 3, 4, 5);
$prod = array_product($MyArray);

echo $prod;
?>

The output of the above code will be:

120

Example:

In the example below, the function is used to calculate the product of all values in the given associative array.

<?php
$MyArray = array("Marry"=>10, 
                 "John"=>20, 
                 "Jo"=>30);
                 
$prod = array_product($MyArray);

echo $prod;
?>

The output of the above code will be:

6000

❮ PHP Array Reference