Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Build a Simple PHP Application Working With Functions Working with Functions

Emil Wallgren
Emil Wallgren
11,737 Points

Can't understand how this loop works, do you?

Hi! My name is Emil Wallgren. I'm currently doing a Quiz on Build a simple php application > working with functions > working with functions. I've seen the videos but I don't understand why the solution to this question is as it is. The question is like this:

What does the following code display:


$numbers = array(1,2,3,4);

$total = count($numbers);

$sum = 0;

$output = "";

$i = 0;

foreach($numbers as $number) {

    $i = $i + 1;

    if ($i < $total) {

        $sum = $sum + $number;



echo $sum;


I understand it like this:

$total = 4 since it counts the amount of variables in the array. $i is also 4 since it adds the number 1 everytime it loops (which is 4 times).

Then the if-statement cant execute since 4 aint less than 4.

We echo $sum which hasn't changed...so we should get 0...right?

Still when i try the code on my computer...i get 6.


3 Answers

6 is correct. Go through number by number

First, number = 1. i = 1, sum = 0+1 = 1
number = 2, i=2, sum = 1+2 = 3
number = 3, i=3, sum = 3+3 = 6
number = 4, i=4, sum is not executed.

There ya go, echo sum=6.

Emil Wallgren
Emil Wallgren
11,737 Points

Thanks Ryan.

But why ain't sum executed on number 4?

I think the only error in your understanding is that $i doesn't become 4 right away. You seem to realize that the loop will execute 4 times. This means the code inside the loop will execute 4 times. Each time through the loop only 1 is being added to $i so it doesn't become 4 until the very last time through the loop. Which means the if block is executed the first 3 times through the loop but not the 4th time as you correctly pointed out.

Emil Wallgren
Emil Wallgren
11,737 Points

Thanks Jason :-)

So the if-statement executes each time the $i is below 4. Now I get it! You helped me alot there :-)

Thanks again :-D


You are correct that, when i = 4, the if statement is false, thus sum is not executed. Since this is a loop, the code is executed as many times as the loop is iterated. In this case, 4 times.

First time through, i =1, the if statement is processed true. Second time through, i=2...etc

Try adding this right before the if statement:

echo $i < $total;
Emil Wallgren
Emil Wallgren
11,737 Points

Yupp. Now I get it :-)