简体   繁体   中英

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. 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\>";
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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