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

Working with Functions Quiz Question - PHP

Hello, I'm doing the last quiz on the Working with Function section inside the Build a Simple PHP Application project.

I'm having a diffuculty understanding a question in the quiz. The question is the following:

What does the following code display?

<?php

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

?>

My answer was 10 but apparantly the correct answer is 6. Could someone please explain why that is? Thank you :)

2 Answers

Simple. You think it's 10 because you're not listening to the equation $i < $total.

If that equation was $i <= $total then the output would be 10. Try it for yourself!

The last time you loop around in the foreach you think it's 10 because you go into the if statement. Which you should because 4 is not less than 4.

Thanks that helps but I still don't understand why the value of $i is 10 when the last $number loops and not 4. I thought that the value of $i will be increased by 1 every time $number loops and not by the value of every $number. I'm not sure I'm making any sense :)

Try this code:

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

$total = count($numbers);

$sum = 0;

$output = "";

$i = 0;

echo "Before foreach total = ".$total." and sum = "
. $sum . "and output =" . $output . "and i = " . $i . "<br> <br>"; 
foreach($numbers as $number) {

    $i = $i + 1;

     if ($i < $total) {

echo "IN THE IF STATEMENT total = ".$total." and sum = "
. $sum . "and output =" . $output . "and i = " . $i . "<br> <br>";

          $sum = $sum + $number;

   }

}

echo $sum;
?>

It outputs:

Before foreach total = 4 and sum = 0and output =and i = 0

IN THE IF STATEMENT total = 4 and sum = 0and output =and i = 1

IN THE IF STATEMENT total = 4 and sum = 1and output =and i = 2

IN THE IF STATEMENT total = 4 and sum = 3and output =and i = 3

6

You can see the code in the if statement get's executed three times.

At the end of the code the $sum = 6 and $i = 4. Maybe you're adding the two together in your head at the end for some reason?

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

It sounds like you generally understand what's going on and how the numbers are getting added together. Notice that the conditional inside the foreach loop checks if $i is less than $total. At the end of the third time through the foreach loop, $sum is 6. The fourth time through the foreach loop, $i is 4 and $total is 4. Since $i is not less than $total, the line of code that adds $number to $sum does not get executed.

Imagine someone asked you, "Take an array of numbers and sum all of them except the last one." This is the block of code that you would write to do that.

Does that help?

Yes that and Ernest's response helped a lot. The part that confused me was the $total. I thought $total counted the numbers of each array element so I thought $total == 10 and not 4 because 1+2+3+4 = 10. Obviously $total counts how many values the array holds, not what I thought.