Home >Backend Development >PHP Problem >How to store 10 numbers in an array in php and sum them
Implementation method: 1. Use the array() function to declare an array and store 10 numbers. The syntax "$arr=array(value 1, value 2, value 3, value 4, value 5... Value 10);"; 2. Use array_sum() to calculate the sum of all elements in the array, the syntax is "array_sum($arr)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
" Store 10 numbers in the array And sum " can be seen as two parts:
Declare an array and store 10 numbers
Calculate the array The sum of all elements
In PHP, there are two functions that can implement the above two parts respectively: array() function and array_sum() function
Let’s follow Let’s take a closer look.
1. Use the array() function to declare an array and store 10 numbers
<?php header('content-type:text/html;charset=utf-8'); $arr=array(1,2,3,4,5,6,7,8,9,10); var_dump($arr); ?>
2. Use the array_sum() function to calculate the sum of all elements in the array
$sum=array_sum($arr);//求和 echo "数组元素之和:".$sum;
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to store 10 numbers in an array in php and sum them. For more information, please follow other related articles on the PHP Chinese website!