Home  >  Article  >  Backend Development  >  How to sum all array elements in php using loop

How to sum all array elements in php using loop

青灯夜游
青灯夜游Original
2023-01-12 14:56:252258browse

php method to use a loop to sum all array elements: 1. Use the for statement to loop through the array, add the elements one by one and sum them, the syntax is "for($i=0;$i

How to sum all array elements in php using loop

The operating environment of this tutorial: windows7 system, PHP8 version, DELL G3 computer

Method 1: Use the for statement Loop through the array, add and sum the elements one by one

The for loop will pre-define the variable that controls the number of loops in the for statement, so the for loop statement can follow the known loop The number of times to perform a loop operation is suitable for situations where the number of times the script needs to be run is clearly known (according to the numeric subscript of the index array).

Note: the for statement is only valid for index arrays and cannot traverse associative arrays

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

How to sum all array elements in php using loop

Description: for loop

The syntax format of the for loop is as follows:

for (初始化语句; 循环条件; 变量更新--自增或自减) {
    语句块;   
}

The for loop statement can be disassembled into 4 parts: the three expressions in () and {}## The "statement block" in #, let's analyze it below.

Statement analysis:


  • Initialization statement (expression 1): mainly initializes a variable value, used to set a counter, that is, the value at the beginning of the loop ; This statement is only executed during the first loop and will not be executed again in the future.

  • Loop condition (expression 2): Restriction condition for loop execution, used to control whether to execute the code in the loop body; if the condition is TRUE, the loop continues, if the condition is FALSE , the loop ends and exits the loop immediately.


  • Variable update (expression 3): an expression with an increment or decrement operation. Every time the loop is executed, the value of the counter is immediately modified so that the loop The conditions gradually become "untenable".


  • Statement block: Several codes that need to be executed when the condition is judged to be true.


Is the above description a bit convoluted? Let’s take a look at the

execution flow chart of the for loop statement to understand the execution of the for loop more intuitively. Process:

How to sum all array elements in php using loop

##Method 2: Use a foreach loop to traverse the array, add and sum the elements one by oneforeach is a statement specially designed for traversing arrays. It is a commonly used method when traversing arrays. It provides great convenience in traversing arrays. After PHP5, you can also traverse objects (foreach can only be applied to arrays and objects).

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$array= array(2,4,6,8,10,12,14,16,18,20);
$sum=0;
foreach ($array as $value) { 
    $sum+=$value;
} 
var_dump($array);
echo &#39;数组所有元素之和:&#39;. $sum;
?>

How to sum all array elements in php using loop

Description: foreach statement

The foreach statement traverses the array and has nothing to do with the array subscript, and can be used for discontinuous indexes Arrays and associative arrays indexed by strings.

The foreach statement has two syntax formats:

    Syntax format 1:
  • foreach ($array as $value){
        语句块;
    }
  • Traverse the given
$array

Array, assign the value of the current array to $value in each loop.

    Syntax format 2:
  • foreach ($array as $key => $value){
        语句块;
    }
  • Traverse the given
$array

array, and in each loop The value of the current array is assigned to $value, and the key name is assigned to $key.

When the foreach statement loops, the pointer inside the array will move forward one step, so that the next array element will be obtained in the next loop until the end of the array is traversed, the traversal stops and exits cycle.

Recommended learning: "
PHP Video Tutorial

"

The above is the detailed content of How to sum all array elements in php using loop. 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