PHP array learning to calculate the sum of array elements

青灯夜游
Release: 2023-04-10 13:04:01
Original
5511 people have browsed it

In the previous article "PHP Array Learning: Obtaining the First/Last Element (2)" we took you to understand the two PHP functions reset() and end() and introduced this Interested friends can read how the two functions obtain the first element and the last element of the array. In this article, we continue to learn the PHP array series!

This article will take a look at how to calculate the sum of all elements in an array, and introduce to you three methods: for loop, foreach loop and array_sum() function (yes, use built-in functions, there are many built-in functions in PHP function to help us develop), let’s take a look together.

Method 1: Use for loop

Copy after login

Output result:

1 + 2 + 3 +...+ 9 + 10 = 55
Copy after login

Isn’t it very simple, use for loop statement to traverse the array, in the loop body Use the "$sum =$array[$i];" statement to add the array elements obtained in each loop. [Recommended learning:PHP loop learning three: How to use for loop statements to traverse arrays]

Method 2: Use foreach loop

Copy after login

Output:

数组所有元素之和:55
Copy after login

Similarly, use the foreach loop statement to traverse the array, and use the "$sum =$value;" statement in the loop body to add the array elements obtained in each loop.

In the foreach loop statement, traverse the given$arrayarray, and assign the value of the current array to$valuein each loop.

[Recommended learning:PHP loop learning four: How to use the foreach statement to traverse and modify array elements]

Method 3: Use array_sum() Function

array_sum() is a built-in function in PHP that can calculate the sum of all elements in an array and return the sum of the elements.

Copy after login

Output:

PHP array learning to calculate the sum of array elements

Description:

  • if$arrayIf all elements in are integers, an integer value is returned; if one or more of the values are floating-point numbers, a floating-point number is returned.

  • If there are non-numeric type elements in$array, then PHP will convert them into a numeric value (PHP is a weak language type and will be based on the value of the variable. , automatically convert the variable to the correct data type), if the conversion fails, it will be used as the0value to participate in the calculation.

Copy after login

Output:

PHP array learning to calculate the sum of array elements

Okay, that’s all. If you want to know anything else, you can click this. → →php video tutorial

Finally, I would like to recommend a free video tutorial on PHP arrays:PHP function array array function video explanation, come and learn!

The above is the detailed content of PHP array learning to calculate the sum of array elements. 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!