PHP | array_product() Function
The array_product() is an inbuilt function in PHP and it returns the product of all numbers present in the given array. The function accepts an array which consists of numbers only. If the array has any other data except numbers, the function returns 0.
Syntax:
array_product($array)
Parameters: The function has one 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 below cases:
- It returns 0 if the array consists of atleast one non-number data.
- It returns 1 when an empty array is passed as a parameter.
- If both of the above two cases are not met then it returns the product of all the terms in array.
Examples:
Input : $array = [1, 2, 3, 4]
Output : 24
Input : $array = [1, 'a']
Output : 0
Below programs illustrate the array_product() function:
Program 1: Program to demonstrate the array_product() function.
<?php
$a1 = array (1, 2, 3, 4);
echo ( array_product ( $a1 ));
?>
|
Output:
24
Program 2: Program to demonstrate the array_product() function when the array contains at least one non-number data.
<?php
$a1 = array (1, 2, 3, 'a' );
echo ( array_product ( $a1 ));
?>
|
Output:
0
Program 3: Program to demonstrate the array_product() function when the array is empty.
<?php
$a1 = array ();
echo ( array_product ( $a1 ));
?>
|
Output:
1
Reference:
http://php.net/manual/en/function.array-product.php
Last Updated :
31 Aug, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...