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 // PHP program to demonstrate // the array_product() fucntion $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 // PHP program to demonstrate the array_product() // fucntion when the array contains at least // one non-number data $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 // PHP program to demonstrate the array_product() fucntion // when the array is empty $a1=array(); echo(array_product($a1)); ?> |
Output:
1
Reference:
http://php.net/manual/en/function.array-product.php
Recommended Posts:
- How to call a function that return another function in JavaScript ?
- How to get the function name from within that function using JavaScript ?
- How to get the function name inside a function in PHP ?
- D3.js | d3.map.set() Function
- PHP | max( ) Function
- PHP | exp() Function
- D3.js | d3.min() function
- PHP | Ds\Map get() Function
- CSS | rgb() Function
- PHP | min( ) Function
- p5.js | box() Function
- p5.js | value() Function
- D3.js | d3.rgb() Function
- p5.js | int() function
- D3.js | d3.hcl() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



