 2014-11-02 12:37

# 如何使用for循环使用php对数字求和？

I am learning PHP. Can someone help me understand how to sum numbers?

For example, I want to use a for loop to sum all numbers from 1 to 10:

``````        '1+2+3+4+5+6+7+8+9+10=?'
``````

#### 图片转代码服务由CSDN问答提供 功能建议

``` ```'1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 =？'

``````
` `
``` 点赞 写回答 关注问题 收藏 分享 邀请回答 ```
``` 8条回答 默认 最新 duanchi4184 2014-11-02 12:40 已采纳 Since you specifically said for loop: <?php \$start = 1; \$end = 10; \$sum = 0; for (\$i = \$start; \$i <= \$end; \$i++) { \$sum += \$i; } echo "Sum from " . \$start . " to " . \$end . " = " . \$sum; 点赞 评论 分享 dongshi1215 2014-11-02 12:41 Yes, it is pretty easy to do: array_sum(range(1, 10)) or \$sequence = array(1,2,3,4,5,6,7,8,9,10); array_sum(\$sequence); 点赞 评论 分享 dongqiangse6623 2014-11-02 12:42 this will do ... you have a lot of options to do this \$a=0; for(\$i=0;\$i==10;\$i++) { \$a=\$a+\$i; } echo 'Sum= ' . \$a ; 点赞 评论 分享 douchen7555 2014-11-02 12:42 Not sure if I understand the question or not, but \$sum = 0; for (\$i = 1; \$i <= 10; \$i++) { \$sum += \$i; } echo 'The sum: ' . \$sum; Should sum the numbers between 1 and 10 into the \$sum variable. 点赞 评论 分享 dsadsa123111 2014-11-02 12:42 When learning PHP from scratch, you should probably start with some tutorials. You could start with a tutorial on variables, a tutorial on operators and then a tutorial on for loops. You should be able to solve this problem on your own afterwards. Doing this on your own will give you a much better learning experience, than just copying other answers here. 点赞 评论 分享 ds261634878 2017-03-31 20:34 for (\$i = 0; \$i <= 10; \$i++) { echo \$i; } 点赞 评论 分享 drl9940 2017-05-12 05:43 Try like this: <form method="post"> Start:<input type="text" name="a"> End: :<input type="text" name="b"> <input type="submit" > </form> <?php \$start = \$_POST['a']; \$end = \$_POST['b']; \$sum = 0; for (\$i = \$start; \$i <= \$end; \$i++) { \$sum += \$i; } echo "<h2>Sum from " . \$start . " to " . \$end . " = " . \$sum; ?> 点赞 评论 分享 dqp4933 2019-07-23 10:41 Make 1+2+3+4+5 = ? by recursion function <?php \$n=1; echo Recursion(\$n); function Recursion(\$n){ if (\$n <=5){ if(\$n<5){ echo "\$n+"; } else echo "\$n="; return \$n+Recursion(\$n+1); } } ?> 点赞 评论 分享 提交 关注问题 相关推荐 如何使用for循环使用php对数字求和？ php 2014-11-02 12:37 回答 8 已采纳 Since you specifically said for loop: &lt;?php \$start = 1; \$end = 10; \$sum = 0; for (\$i = \$star Python：字符串中数字求和 python 2019-10-29 19:03 回答 4 已采纳 cloudyzhao所说方案实现简单有效，此处列一个基础的写法，不依赖正则库 s = "Abc34g8" result = 0 i = 0 while i = '0' a PHP根据foreach循环内的值求和数组值 php 2018-03-07 18:51 回答 1 已采纳 You don't need to worry about previous/next values to group your array. Just iterate it and use t PHP - >如何使用foreach对数组值求和 php 2017-04-28 10:52 回答 1 已采纳 You can use this code. It will sum all values and put in the \$sum variable. if your array object( php - 如何对多维数组求和 php 2018-03-14 04:35 回答 3 已采纳 You can loop thru your array and use array_sum \$arr = array( "A" =&gt; array ( PHP使用相同的键求和数组值 php 2016-06-23 18:07 回答 1 已采纳 The first thing that you need to do is iterate through the outer array. Then, for each row in the 2重for循环，如何循环赋值，以及判定求和的条件 2018-03-07 09:40 回答 1 已采纳 因为 你这里写了 int b1 = { 2,4,3 }; int b2 = { 3,4,2 }; 也就是 b1=2, b1=4, b1=3, b1=0; thymeleaf 循环 sum求和 html5javascriptjquery 2021-04-26 11:30 回答 1 已采纳 可以了  [[\${#aggregates.sum(consumeOrderDetailList.![orderAmount])}]] select - 如何使用jquery对所选选项的值求和？ htmljavascriptjqueryphp 2016-08-01 13:44 回答 2 已采纳 Try following code. \$(document).ready(function() { \$('#btn-add').click(function(){ 带'for'循环的系列求和 php 2014-09-30 13:38 回答 2 已采纳 I think this is what you are looking for \$sum =0; for (\$i = 1; \$i&lt;=10; \$i++){ \$sum = \$sum ```
``` 点击登录 提问题 ◇ 欢迎建议意见 ◇ 回答问题能得到什么 ◇ 如何写高质量的提问和回答 ◇ 问答平台结题规则说明 加急问题 ¥300 Python爬虫提取数据 ¥100 使用AForge.NET打开USB摄像头时遇到弹出框（ShowDialog类型）就会出现主界面（线程）卡死 ¥100 安卓节目编辑功能有没有人推荐一下? ¥100 爬取私募排排网部分基金历史净值数据 ¥100 这个问题怎么解决，为什么会报错？ ¥55 怎么确定一个float值是否经过某值？ ¥50 gazebo如何获取加速度呢 ¥50 fanuc sdk函数下发NC程式到cf卡 ¥50 pjsip 呼出后，马上挂断，出现死锁deadlock ,为什么？ ¥50 CSDN问答花钱请你来找茬，Bug悬赏第二弹！ ```
``` ```
``` window.csdn.sideToolbar = { options: { afterFinished: function () { setTimeout(function () { var helphtml = '<a class="option-box" data-type="help" href="https://ask.csdn.net/questions/7441224" target="_blank">'+'<img src="https://g.csdnimg.cn/side-toolbar/2.5/images/bangzhucopy.png" alt="" srcset="">'+ '<span class="show-txt">帮助</span></a>'; \$('.csdn-side-toolbar > a:last').before(helphtml); }, 300); }, contentEl: document.getElementsByClassName('floor') } } ```