###### Seth Johnson

14,877 Points# I've hit the wall of incomprehension.

Okay, I seriously need help here. I plugged the code in for this portion of the quiz to my own local machine, and so got the correct answer. **BUT.**

*I absolutely have no [expletive deleted] idea* **HOW** *or* **WHY.**

I took things as incrementally as I feel like I possibly could have, and even went so far as to var_dump everything and make notes to myself so I could figure out what was (or wasn't) happening. Like so:

```
<?php
$numbers = array(1,2,3,4);
$total = count($numbers);
$sum = 0;
$loop = 0;
foreach($numbers as $number) {
$loop = $loop + 1;
if ($loop < $total) {
$sum = $sum + $number;
}
}
var_dump($number);
echo " <--- this one is 'number'" . "</br>";
var_dump($loop);
echo " <--- this one is 'loop'" . "</br>";
var_dump($loop + 1);
echo " <--- this one is 'loop plus one'" . "</br>";
var_dump($total);
echo " <--- this one is 'total'" . "</br>";
var_dump($sum);
echo " <--- this one is 'sum'" . "</br>";
var_dump($sum + $total);
echo " <--- this one is 'sum plus total.' Allegedly." . "</br>";
//echo $sum;
```

And it all checks out, and got all of this as my output:

- int(4) <--- this one is 'number'
- int(4) <--- this one is 'loop'
- int(5) <--- this one is 'loop plus one'
- int(4) <--- this one is 'total'
- int(6) <--- this one is 'sum'
- int(10) <--- this one is 'sum plus total.' Allegedly.

I'm certainly no mathemagician, but if my basic arithmetic is correct, **"$sum" starts out as 0**, and **remains at 0** once the loop runs...and **$number is 4** once the loop runs...so...*how do the laws of time and space bend so that 0 + 4 = 6?!?* (Since, y'know, that's presumably what the "$sum" + "$number" adds up to for the "correct" answer.) My brain is melting. **Please advise.** Thanks very much in advance.

## 1 Answer

###### Steven Parker

177,727 PointsThe second part of your premise *"$sum ... remains at 0 once the loop runs*" is __incorrect__ because of this line:

```
$sum = $sum + $number;
```

Each time through the loop, $sum gets bigger:

- when $number is 1, $sum becomes 1 (0 + 1)
- when $number is 2, $sum becomes 3 (1 + 2)
- when $number is 3, $sum becomes 6 (3 + 3)
- when $number is 4, nothing happens since 6 is not less than 4 ($total)

So when the loop finishes, **$sum is 6** — *and the laws of time and space remain intact.*

## Seth Johnson

14,877 Points## Seth Johnson

14,877 PointsSomewhere off in the distance, you may hear something

resemblingthe most tumultuous thunderstorm that ever was or ever will be...but it is, in fact, only the sound of my palm hitting my forehead, upon figuring out the situation with this loop. Thank you @Steven. :)## Steven Parker

177,727 Points## Steven Parker

177,727 PointsGlad I could help, and happy coding!