Home > Backend Development > PHP Tutorial > PHP methods for calculating the sum and product of values ​​in an array array_sum and array_product functions array product company numpy array product arra

PHP methods for calculating the sum and product of values ​​in an array array_sum and array_product functions array product company numpy array product arra

WBOY
Release: 2016-07-29 08:54:45
Original
1950 people have browsed it

The example in this article describes how PHP calculates the sum and product of values ​​in an array. Share it with everyone for your reference, the details are as follows:

1. Overview:

array_sum() function is used to calculate the sum of all values ​​in the array.
array_product() function is used to calculate the product of all values ​​in an array.

2. Usage example:

array_sum()

PHP array_sum() function is used to calculate the sum of all values ​​in the array, and returns the calculation result as an integer or floating point number. Non-numeric units will be treated as 0.

Syntax:

number array_sum( array array )

Copy after login

Example:

<&#63;php
$arr_a = array(1, 2, 3, "a");
echo array_sum($arr_a);  //输出:6
&#63;>

Copy after login

array_product()

PHP array_product() function is used to calculate the product of all values ​​in the array, and returns the calculation result as an integer or floating point number, non-numeric The unit will be treated as 0.

Grammar:

number array_product( array array )

Copy after login

Example:

<&#63;php
$arr_a = array(1, 2, 3);
echo array_product($arr_a);  //输出:6
$arr_b = array(1, 2, 3, "abc");
echo array_product($arr_b);  //输出:0
&#63;>

Copy after login

Readers who are interested in more PHP-related content can check out the special topics of this site: "Summary of PHP operations and operator usage", "Summary of PHP network programming skills", "Introduction Tutorial on PHP Basic Syntax", "Summary of PHP Office Document Operation Skills (Including Word, Excel, Access, PPT)", "Summary of PHP Date and Time Usage", "Introduction Tutorial on PHP Object-Oriented Programming", "php String (string) usage summary", "php+mysql database operation introductory tutorial" and "php common database operation skills summary"

I hope this article will be helpful to everyone in PHP programming.

The above introduces the PHP method of calculating the sum and product of values ​​in an array, the array_sum and array_product functions, including product and array content. I hope it will be helpful to friends who are interested in PHP tutorials.

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