Home >Backend Development >PHP Tutorial >PHP array learning to calculate the sum of array elements

PHP array learning to calculate the sum of array elements

青灯夜游
青灯夜游Original
2021-07-30 17:46:075677browse

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

<?php
$array= array(1,2,3,4,5,6,7,8,9,10);
$sum=0;
for ($i=0; $i < count($array); $i++) { 
    $sum+=$array[$i];
} 
echo &#39;1 + 2 + 3 +...+ 9 + 10 = &#39;. $sum;
?>

Output result:

1 + 2 + 3 +...+ 9 + 10 = 55

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

<?php
header("Content-type:text/html;charset=utf-8");
$array= array(1,2,3,4,5,6,7,8,9,10);
$sum=0;
foreach ($array as $value) { 
    $sum+=$value;
} 
echo &#39;数组所有元素之和:&#39;. $sum;
?>

Output:

数组所有元素之和:55

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 $array array, and assign the value of the current array to $value in 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.

<?php
header("Content-type:text/html;charset=utf-8");
$array= array(1,2,3,4,5,6,7,8,9,10);
var_dump($array);
echo &#39;数组所有元素之和:&#39;. array_sum($array);
?>

Output:

PHP array learning to calculate the sum of array elements

Description:

  • if$array If 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 the 0 value to participate in the calculation.

<?php
header("Content-type:text/html;charset=utf-8");
$array= array("10.1xy", 100, &#39;1&#39;, "0.01");
var_dump($array);
echo &#39;数组所有元素之和:&#39;. array_sum($array);
?>

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!

Statement:
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