PHP array_product Function | Product of Array Elements

PHP array_product Function is an inbuilt Function in PHP which calculates the product of all elements in an array. It takes in an array containing integer or float values and returns the product of those elements. Though it is very simple to use, it helps in preventing a loop to calculate the product. In this article, we will discuss the array_product Function in PHP. Also, we will discuss a few examples to demonstrate it’s usage.




The array_product Function in PHP takes in only one mandatory parameter. An array is passed containing integer/float values.

Return Value

The PHP array_product Function returns an integer or float value depending on the type of elements passed to it.

PHP array_product Function Diagram


Let’s discuss a few examples to demonstrate the array_product Function.

Example 1: Basic Usage

	$testArray = array(1, 2, 3, 4, 5);
	$product = array_product($testArray); // Equivalent to 1 * 2 * 3 * 4 * 5
	/* int(120) */

As shown in the above example, the function returns the product of elements passed in the array.

Example 2: Decimal Values

	$testArray = array(1, 2.5, 3.1, 4, 5.9);
	$product = array_product($testArray);
	/* float(182.9) */

Similarly, you can pass decimal values in the input array. The return type of the function changes according to the input elements.

Example 3: Elements with 0

Obviously, if any value in the array is 0, then the function will always return 0.

	$testArray = array(1, 2.5, 3.1, 4, 0);
	$product = array_product($testArray);
	/* float(0) */

Example 4: Array Elements Containing String

If any element in the input array is a string, the function will type-case it to 0. Therefore, it will always return 0 in such a case.

	$testArray = array(1000, 2000, 'String', 'Name');
	$product = array_product($testArray); //Equivalent to 1000 * 2000 * 0 * 0
	/* int(0) */

Example 5: Check If All Values are Boolean True or Not

You can use the function to check whether all values are true or not.

	$check1 = true;
	$check2 = 0;
	$check3 = 1;
	$check4 = false;

	$isTrue = array_product(array($check1, $check2, $check3, $check4));
	/* int(0) */
	//This is similar to
	$isTrue = $check1 && $check2 && $check3 && $check4;
	/* bool(false) */

However, there is one difference between using array_product function and boolean operators. The former returns an integer 0 value and the later returns a bool(false).


In conclusion, you learned about PHP array_product Function. Read about other PHP Array Functions on Concatly! Additionally, you can also learn more about this function on the Official Documentation of PHP.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *