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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to demonstrate 
// the array_product() fucntion
$a1=array(1, 2, 3, 4);
  
echo(array_product($a1));
?>

chevron_right


Output:

24

Program 2: Program to demonstrate the array_product() function when the array contains at least one non-number data.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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));
?>

chevron_right


Output:

0

Program 3: Program to demonstrate the array_product() function when the array is empty.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to demonstrate the array_product() fucntion
// when the array is empty
  
$a1=array();
  
echo(array_product($a1));
?>

chevron_right


Output:

1

Reference:
http://php.net/manual/en/function.array-product.php



My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.