Home  >  Article  >  Backend Development  >  How to store 10 numbers in an array in php and sum them

How to store 10 numbers in an array in php and sum them

青灯夜游
青灯夜游Original
2022-04-29 15:25:432349browse

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)".

How to store 10 numbers in an array in php and sum them

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(&#39;content-type:text/html;charset=utf-8&#39;);   
$arr=array(1,2,3,4,5,6,7,8,9,10);
var_dump($arr);
?>

How to store 10 numbers in an array in php and sum them

2. Use the array_sum() function to calculate the sum of all elements in the array

$sum=array_sum($arr);//求和
echo "数组元素之和:".$sum;

How to store 10 numbers in an array in php and sum them

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!

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