Home > Backend Development > PHP Tutorial > PHP function array_product() that calculates and returns the product of an array

PHP function array_product() that calculates and returns the product of an array

黄舟
Release: 2023-03-17 08:12:01
Original
2285 people have browsed it

Example

Calculate and return the product of array:

<?php
$a=array(5,5);
echo(array_product($a));
?>
Copy after login

Definition and usage

array_product() FunctionCalculation and returns the product of arrays.

Syntax

array_product(array)
Copy after login
ParametersDescription
array Required. Specifies an array.

Technical details

Return value: Returns the product of an integer or floating point number.
PHP Version: 5.1.0+
Change Log:Since PHP 5.3.6, the product of empty arrays is 1. Prior to PHP 5.3.6, the product of empty arrays was 0.

More examples

Example 1

Calculate and return the product of arrays:

<?php
$a=array(5,5,2,10);
echo(array_product($a));
?>
Copy after login

Example 1 (Pure Numbers (array without string ):

<?php
$arr=array(2,3,4);
echo array_product($arr);
?>
Copy after login

Output result: 24
Example (array containing string):

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Type" content="text/hmtl; charset=utf-8" ><!--PHP中文乱码处理-->
<title>php array_product()函数笔记_PHP笔记</title>
</head>
<body>
<?php
$arr1=array("2","3","4");//"2"、"3"和"4"会被当中数字2、3和4
$arr2=array("2a","3b","4");//"2a"和"3b"会被当作数字2和3
$arr3=array("a2","3","4"); //"a2"会被当作数字0
echo &#39;"2","3","4":&#39;.array_product($arr1);
echo &#39;</br>"2a","3b","4":&#39;.array_product($arr2);
echo &#39;</br>"a2","3","4":&#39;.array_product($arr3);
?>
</body>
</html>
Copy after login

operation result:
PHP function array_product() that calculates and returns the product of an array

The above is the detailed content of PHP function array_product() that calculates and returns the product of an array. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template