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

Alex Forseth
Alex Forseth
8,017 Points

Can someone walk me through this Quiz Question?

In the PHP Build A Basic Website course in the listing and sorting lessons there is a quiz titled "Working with Functions" - https://teamtreehouse.com/library/build-a-basic-php-website/listing-and-sorting-inventory-items/working-with-functions

For the life of me I cannot figure why the answer to the below is 6. Can someone walk me through it?

What does the following code display?

$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;
    }
}

echo $sum;

2 Answers

Hello there, Simply because when it comes to the last item which is 4, the if condition is false cause 4 isnt strict inferior to 4, so it doesnt add 4 to 6 ($sum). Take a pen and piece of paper, and walk through the foreach you ll get it better. Keep it up

Alex Forseth
Alex Forseth
8,017 Points

I have never taken the time to break down a problem with a pencil and paper before but this worked. Its much easier to keep track of in your head this way. I always scoffed at it before. Thanks for the advice.

You're welcome, I'm glad I could be of help to you. it always helped when I didn't get a break from a loop or smthg. Keep up the great work. peace out

The foreach loop is going to take a value from the array, set it to $number and then run the code underneath. Every time the loop runs the $loop variable increases by one '$loop = $loop + 1' is the same as take the variable and add 1 to it. The final line is then very similar to what i have just explained for the loop. The $number variable is the number that has been pulled from the array, so in the first instance $number = 1. This number is then added to $sum which currently = 0.

So after running through once $loop = 1 and $sum = 1. We then go through the process again but with $number = 2. Loop increases by 1 and sum increases by 2 as we are adding $number to it which is now equal to 2.

So we now have loop=2 and $sum =3. We run the process again but with $number = 3. Loop increases to 3 and sum increases to 6 as we are adding 3 to its current value of 3.

The next time the loop tries to run, when is reaches the statement $loop < $total it stops and doesn't run any further leaving $sum = 6. The reason is stops is because $total = the count of $numbers (essentially how many number are there and the answer is 4). so 3>4 and the loop stops.

Hopefully this was helpful and keep going as some of the questions like this really confused me at the start as well.