PHP Array array_product() Function (With Examples)

In this tutorial, we will learn about the PHP array_product() function with its usage, syntax, parameters, return value, and examples. By IncludeHelp Last updated : December 31, 2023

PHP array_product() function

The 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

The syntax of the array_product() function:

array_product(arr);

Parameters

The parameters of the array_product() function:

  • arr: It accepts an array and returns the product of all values.

Return Value

The return type of this method is int|float, it returns the product as an integer or float.

Sample Input/Output

Input:
$arr = array(10, 20, 30, 40, 50);

Output:
12000000

PHP array_product() Function Example 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

The output of the above example is:

12000000

PHP array_product() Function Example 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

The output of the above example is:

0

To understand the above examples, you should have the basic knowledge of the following PHP topics:

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.