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
Jim Shepard
2,052 PointsWorking 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
Ernest Grzybowski
Treehouse Project ReviewerSimple. 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.
Randy Hoyt
Treehouse Guest TeacherIt 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?
Jim Shepard
2,052 PointsYes 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.
Jim Shepard
2,052 PointsJim Shepard
2,052 PointsThanks 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 :)
Ernest Grzybowski
Treehouse Project ReviewerErnest Grzybowski
Treehouse Project ReviewerTry this code:
It outputs:
You can see the code in the
ifstatement get's executed three times.At the end of the code the
$sum = 6and$i = 4. Maybe you're adding the two together in your head at the end for some reason?