Home  >  Article  >  Backend Development  >  How to implement numerical sum in php

How to implement numerical sum in php

青灯夜游
青灯夜游Original
2022-04-24 17:37:194685browse

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.

How to implement numerical sum in php

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(&#39;content-type:text/html;charset=utf-8&#39;);   
$a=2;
$b=3;
echo "数值1为:".$a;
echo "<br>数值2为:".$b;
echo "<br>两数相加求和:".($a+$b);
?>

How to implement numerical sum in php

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(&#39;content-type:text/html;charset=utf-8&#39;);   
$arr=array(1,2,3,4,5,6,7,8,9);
echo "多个数值:";
var_dump($arr);
echo "数值之和为:". array_sum($arr);
?>

How to implement numerical sum in php

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!

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