2

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:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <?php 
        for ($i = 1; $i <= 10; $i++){
            $sul = $i + $i + $i + $i + $i + $i + $i + $i + $i + $i;
            echo "$i + $i + $i + $i + $i + $i + $i + $i + $i + $i = $sul<br>";

        };

    ?>
</body>

Hope you can help me thanks :)

Jelles
  • 115
  • 1
  • 2
  • 7

7 Answers7

16

This code should help you:

<?php
$sum = 0;
for($i = 1; $i<=10; $i++) {
    $sum = $sum + $i;
}
echo $sum;    
?>

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:

<?php
$sum = 0;
$str = '';
for($i = 1; $i<=10; $i++) {
    $sum = $sum + $i;
    $str .= $i == 10 ? $i." = " : $i." + ";
}
echo $str.$sum;
?>

And it will output 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55

Karol Gasienica
  • 2,825
  • 24
  • 36
7

Something like this perhaps?

$range = range(1, 10);
$sum = array_sum($range);

echo implode(' + ', $range) . ' = ' . $sum;

range() - http://php.net/range

array_sum() - http://php.net/array_sum

implode() - http://php.net/implode

Jonnix
  • 4,121
  • 1
  • 30
  • 31
3

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 <= 10; $i++){
    $count += $i;

    $string .= ($i == 10) ? $i : $i." + ";
}
echo $string." = ".$count; // 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55

This is a very simple and straight forward way, you can do this using some array functions which is built-in in PHP.

Murad Hasan
  • 9,565
  • 2
  • 21
  • 42
1

If you don't want to hardcode it, you can do this

<?php
$answer = 0;    
for ($i = 1; $i <= 10; $i++){
         $answer = $i + $answer;
         if ($i == 10) {
           echo $i." = ".$answer;
         }
         else {
           echo $i." + ";
         }       
    };
?>

Output is:

1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55
Rav
  • 1,327
  • 3
  • 18
  • 32
  • Notice: Undefined variable: answer in – Masivuye Cokile Nov 21 '16 at 09:27
  • @MasivuyeCokile, you downvoted my answer? Of course you have to initialize $answer = 0 before the loop. – Rav Nov 21 '16 at 10:13
  • @MasivuyeCokile, you should have realize that you should initialize the variable. SO is not an assignment-making website. – Rav Nov 21 '16 at 10:17
  • You should stated that when answering that the user needs to initialize answer, I know that I did not even run your code, but I knew there's a notice. You must remember when we answering we are also educating other people that might have the same problem in future. – Masivuye Cokile Nov 21 '16 at 10:23
0
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <?php
        $n = 10;
        $items = range(1, $n);
        $sum = array_sum($items);
        echo implode('+', $items) . ' = ' . $sum;
    ?>
</body>

This way is shorter. You generate the array from 1 to $n(10);

Then you calculate the sum of the items.

And then join each element with '+' and add the sum.

0

There you go:

$sum = 0;
for($i = 1; $i <= 10; $i++){
    if($i == 10){
        echo $i;
    } else {
        echo $i." + ";
    }
    $sum = $sum + $i;
}
echo " = ".$sum;
d3p4n5hu
  • 411
  • 4
  • 9
0

A for loop actually repeats a block of code n times, you syntax is right buy your semantic is wrong: initializes a counter in 0 and add i to the counter in each step as the other people say. Also, if is not compulsory to use a for, remember that the sum of first n natural numbers is n(n+1)/2, so no loop is actually needed