How to use foreach in php to output the sum of an array

PHPz
Release: 2023-04-18 10:52:10
Original
1045 people have browsed it

With the rapid development of network technology, PHP language is increasingly used in Web development. In PHP programs, arrays are a very common data type that can store large amounts of data, and each element in the array can be traversed through a loop. However, in some cases, we need to add up or sum the elements in the array, so how to use a foreach loop to traverse the array and calculate the sum of the array elements? This article will take you through the details.

1. Use foreach loop to traverse arrays

Before learning how to use foreach to output the sum of an array, you first need to understand how to use foreach loop to traverse an array. foreach is a way to iterate through an array without knowing the number of elements in the array. It is usually used to iterate through each element in an array and perform some specific operations.

The following is a simple PHP array example:

$arr ​​= array('apple', 'banana', 'orange', 'pear');

Use The foreach loop traverses the array, you can use the following method:

foreach ($arr as $value) {
echo $value . '
';
}

above In the code, we use a foreach loop to traverse the array and assign the value of each array element to the variable $value, and then output the value of each array element through the echo statement in the loop body.

2. Use foreach to output the sum of the array

If we need to sum all the elements in the array, we need to use PHP's arithmetic operators and loop control statements. The specific implementation method is as follows:

1. First define an array $arr:

$arr ​​= array(1, 2, 3, 4, 5);

2. Define a variable $sum to save the sum of the array elements and initialize it to 0:

$sum = 0;

3. Use a foreach loop to traverse the array and change the value of each element Add to the $sum variable:

foreach ($arr as $value) {
$sum = $value;
}

4. Finally, use the echo statement to output $sum Value of variable:

echo $sum;

The complete code example is as follows:

$arr = array(1, 2, 3, 4, 5);
$sum = 0;
foreach ($arr as $value) {

$sum += $value;
Copy after login

}
echo $sum;
?>

Executing the above code will output the sum of array elements 1 to 5, which is 15.

It should be noted that when calculating the sum of array elements, you can use PHP's built-in function array_sum(), which can directly calculate the sum of all elements in the array. For example:

$arr ​​= array(1, 2, 3, 4, 5);
echo array_sum($arr);

Execute the above code and you can also output array element 1 The sum to 5 is 15.

3. Summary

This article introduces the method of using a foreach loop to traverse a PHP array and output the sum of the array elements. Through the understanding and use of the foreach statement, as well as the proficient use of PHP arithmetic operators and loop control statements, we can easily implement the sum operation of array elements. Therefore, in the process of developing web applications, using foreach loop to traverse arrays has become one of the indispensable skills for PHP developers.

The above is the detailed content of How to use foreach in php to output the sum of an array. For more information, please follow other related articles on the PHP Chinese website!

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