Home >Backend Development >PHP Problem >How to use loop to find the sum of arrays in php

How to use loop to find the sum of arrays in php

PHPz
PHPzOriginal
2023-04-18 15:25:40975browse

In PHP programming, it is often necessary to calculate the sum of all elements in an array. At this time, we can use loop control statements to implement the summation operation. This article will introduce how to use a loop to find the sum of an array.

Suppose we have an array $arr, which contains several numbers. Our task is to calculate the sum of these numbers. First, we need to define a variable $sum to store the accumulated results. To iterate through all elements in an array, we can use a for loop statement.

The code is as follows:

$arr = [1, 2, 3, 4, 5];
$sum = 0;
for ($i = 0; $i < count($arr); $i++) {
    $sum += $arr[$i];
}
echo "数组的总和为:" . $sum;

After running the code, the output result is:

数组的总和为:15

We can see that using the for loop statement, we successfully calculated the array sum.

In addition to the for loop statement, we can also use the foreach loop statement to traverse the array. At this time, the code is as follows:

$arr = [1, 2, 3, 4, 5];
$sum = 0;
foreach ($arr as $value) {
    $sum += $value;
}
echo "数组的总和为:" . $sum;

The running result is the same as using the for loop, which is 15.

It should be noted that when using a loop statement to calculate the sum of an array, you need to first determine whether the array is empty. If the array is empty, the result should also be 0. You can use the count() function to determine the number of elements in the array. The code is as follows:

$arr = [];
$sum = 0;
if (count($arr) > 0) {
    foreach ($arr as $value) {
        $sum += $value;
    }
}
echo "数组的总和为:" . $sum;

In the above code, since the array is empty, the calculation result of $sum is 0.

In addition to the above methods, we can also use the array_sum() function to calculate the sum of an array. This function can directly calculate the sum of the elements of an array without using a loop statement to accumulate. The code is as follows:

$arr = [1, 2, 3, 4, 5];
$sum = array_sum($arr);
echo "数组的总和为:" . $sum;

The running result is the same as using the loop, which is 15.

To sum up, using a loop to find the sum of an array is a very common operation in PHP programming. We can use a for loop or a foreach loop to iterate over the array and then accumulate the elements in the array using a variable. At the same time, we can also use the array_sum() function to directly calculate the sum of the elements of the array, making the code more concise.

The above is the detailed content of How to use loop to find the sum of arrays in php. 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