Home > Article > Backend Development > How to implement numerical sum in php
Methods to implement numerical sum: 1. When there are only two values, directly use the "value 1 value 2" statement to sum; 2. When there are multiple values, you can use "array(value 1, value 2, value 3...value n)" statement to store the value in the array, and use the "array_sum($arr)" statement to calculate the sum of all elements in the array.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
realize numerical values in php Method of summing
When there are only two values, summing the values is very simple. Just use the " " operator to add the two numbers:
<?php header('content-type:text/html;charset=utf-8'); $a=2; $b=3; echo "数值1为:".$a; echo "<br>数值2为:".$b; echo "<br>两数相加求和:".($a+$b); ?>
If there are multiple values, you can also use the " " operator to add the numbers, but it is troublesome.
At this time, we can use the array
to store the value in the array
Use the array function array_sum() to calculate The sum of all elements in the array
<?php header('content-type:text/html;charset=utf-8'); $arr=array(1,2,3,4,5,6,7,8,9); echo "多个数值:"; var_dump($arr); echo "数值之和为:". array_sum($arr); ?>
Recommended study: "PHP Video Tutorial"
The above is the detailed content of How to implement numerical sum in php. For more information, please follow other related articles on the PHP Chinese website!