简体   繁体   English

添加for循环的所有结果并回显它

[英]add all results from for loop and echo it

So I have this code: 所以我有这个代码:

<?php
for ($x=1; $x<=1000; $x++)
{

    if ($x % 15 == 0 || $x % 26 == 0)
    {
        echo $x;
        echo "<br \>";
    }
}
?>

This code will output all numbers divisible by 15 or 26. But what I really want is to add up all the results and echo it. 此代码将输出所有可被15或26整除的数字。但我真正想要的是将所有结果相加并回显它。 So what's the best way to achieve this? 那么实现这一目标的最佳方法是什么?

<?php
$sum = 0;
for ($x=1; $x<=1000; $x++)
{
    if ($x % 15 == 0 || $x % 26 == 0)
    {
        $sum += $x;
        // echo $x;
        // echo "<br \>";
    }
}
echo $sum. "<br\>";
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM