I need to make a loop in php that does 1 + 2 + 3 + 4 .... + 10 = 55 but icant get it to work. I did this:
Hope you can help me thanks :]
asked Nov 21, 2016 at 9:15
8
This code should help you:
You are incorrect using loop.
Explanation
I think it will be easier to understand with following table:
_____________________
|JUMP | $i | $sum |
|1 | 1 | 1 |
|2 | 2 | 3 |
|3 | 3 | 6 |
|4 | 4 | 10 |
|5 | 5 | 15 |
|6 | 6 | 21 |
|7 | 7 | 28 |
|8 | 8 | 36 |
|9 | 9 | 45 |
|10 | 10 | 55 |
More about for
you can read in PHP: for
Update
If you want your structure it can be as follows:
And it will output
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55
answered Nov 21, 2016 at 9:18
0
Just make a for loop from starting 1 to 10, like given below. You need to initialize the counter as 0 and while the loop executes you need to collect / sum them to the counter and finally outside the loop print/ echo the counter.
$count = 0;
$string = '';
for [$i = 1; $i