Open In App

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:



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() function
$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() 
// function 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() function
// when the array is empty
  
$a1=array();
  
echo(array_product($a1));
?>

Output:

1

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


Article Tags :