Home > Article > Backend Development > How to use php array_product() function? (code example)
array_product() is a built-in function in PHP that returns the product of all numbers in a given array. This function accepts an array consisting only of numbers. If there is other data in the array besides numbers, the function returns 0.
Syntax:
array_product($array)
Parameters:
array_product() function has a Mandatory parameter $array for which we want to calculate the product of all values.
Return value:
This function returns three different values based on the following conditions:
● If the array contains at least one non-numeric data, then Return 0.
● When an empty array is passed as parameter, it returns 1.
● If neither of the above conditions are met, return the product of all items in the array.
Code Example 1:
<?php $a1=array(1, 2, 3, 4); echo(array_product($a1)); ?>
Output:
24
Code Example 2: When the array contains at least one non-numeric data.
<?php $a1=array(1, 2, 3, 'a'); echo(array_product($a1)); ?>
Output:
0
Code example 3: When the array is empty.
<?php $a1=array(); echo(array_product($a1)); ?>
Output:
1
Related recommendations: "PHP Tutorial"
The above is the detailed content of How to use php array_product() function? (code example). For more information, please follow other related articles on the PHP Chinese website!