It would be better if you use a for
instead of a while
, but if it’s an exercise requirement than you can write:
$int = 8;
$i = 1;
$sum = 0;
while ($i <= $int) {
$sum += $i;
$i++;
}
A more advanced notation would be: $sum += $i++;
(so you can remove the final $i++
).
And a better algorithm would be: $sum = ($int * ($int + 1)) / 2;
, this wouldn’t require any loop.
2
solved I need help at a simple while loop