Home »
PHP
PHP array_product() function with example
PHP array_product() function: Here, we are going to learn about the array_product() function with example in PHP.
Submitted by IncludeHelp, on February 22, 2019
PHP array_product() function
array_product() function is used to find the product of all elements (values) of an array, this method accepts an array and returns the product of all values.
Note: If any element is text in the array, it returns 0.
Syntax:
array_product(array);
It accepts an array and returns the product of all values.
Examples:
Input:
$arr = array(10, 20, 30, 40, 50);
Output:
12000000
PHP code 1: Finding product of all numeric values of an array
<?php
$arr = array(10, 20, 30, 40, 50);
//calculating product of all values
$result = array_product($arr);
//printing the result
print_r ($result);
?>
Output
12000000
PHP code 2: Finding product of all values (numeric and text) of an array – it returns 0
<?php
$arr = array(10, 20, "Hello", 30, 40);
//calculating product of all values
$result = array_product($arr);
//printing the result
print_r ($result);
?>
Output
0
TOP Interview Coding Problems/Challenges