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 trialcaroline
6,974 PointsNeed help with quiz question
In the quiz Working with Functions, the answer to the question 'What does this code display"? is apparently 321. I cannot for the life of me figure out how 321 is the answer.
For a start, if $output = $number . $output, it would echo
1 ""
after the first cycle, because $number = 1, and $output = "".
Or am I being really stupid?
If anyone could walk me through this I'd be really grateful. (Code pasted below.)
<?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) {
$output = $number . $output;
}
}
echo $output;
?>
4 Answers
filipepacheco
Full Stack JavaScript Techdegree Student 21,416 PointsHi, Caroline,
Remember that the foreach loop will loop through each array value. So if it has 4 values, it will loop 4 times. Let's try to go through the code each time the loop runs it. Remember the first line of code inside the foreach loop.
$i = 0;
foreach($numbers as $number) {
$i = $i + 1;
}
So running through it the first time, $i will be 0 + 1, so when the loop ends the first time, $i = 1. This value will keep stored in this variable for the next loop, so in the second loop, the code will be $i = 1 + 1; So now, $i is equal to 2. Again, this value will be stored in the variable $i. Now the third time, $i = 2 + 1;, which is equal to 3. Now the fourth time, $i = 3 + 1, which is 4. Since the array has only four values, the loop runs only 4 times. So now will know the first time, $i was equal to 1, the second time it was 2, the third was 3 and the fourth was 4.
Now we will go to the second line of code.
foreach($numbers as $number) {
$i = $i + 1;
if ($i < $total) {
$output = $number . $output;
}
}
The if statement says to run the line of code inside it only if $i is less than the $total. Since $total = count($numbers);, it mean that $total is equal to 4, so the if statement says if($ i < 4). Which means it will run until 3. So the conde inside that statement will be executed only 3 times.
Now let's execute that code each time until 3, to see what happens.
In the first time , it will fetch the first value of the array, so $number will be equal to 1. Now the $output will be 1 concatenated to the previous output. Since this is the first loop, the output has no value, so $output = 1 + 0;
$number = 1;
$output = "";
$output = $number . $output;
$output = 1 . "";
$output = 1;
Now that it looped the first time, the value 1 was stored in the output for the next loop. Since it's now the second loop, it will fetch the second value from the array, which is 2, so now $number = 2 and $output = 1, if you concatenate them, $output will be 21.
$number = 2;
$output = 1;
$output = $number . $output;
$output = 1 . 2;
$output = 21;
Now for the third and last time, the value fetched will be 3, so $number = 3 and $output = 21. If you concatenate them, $output will be 321.
$number = 3;
$output = 21;
$output = $number . $output;
$output = 3 . 21;
$output = 321;
nfs
35,526 PointsAmazing explanation @filipepacheco.
filipepacheco
Full Stack JavaScript Techdegree Student 21,416 PointsThanks Nafis
caroline
6,974 PointsOK - I have now done my own homework (!) and found the answer here http://us2.php.net/manual/en/function.empty.php. The "" means that the variable is empty. My next question is - why use '"" instead of NULL or FALSE? Substituting NULL or FALSE also seems to work.
filipepacheco
Full Stack JavaScript Techdegree Student 21,416 PointsI'm glad to know it helped.
That's, right. And yes, you can use null, or 0 (I'm not sure about the false), but It just means that the variable is empty. I'm still studying php, so I put inside the quotation marks because this is what I see most people usually doing. But anyways, in the first loop you can just ignore the concatenation to the output, since it's empty.
caroline
6,974 PointsThanks Felipe!
caroline
6,974 Pointscaroline
6,974 PointsHi Filipe
Thank you so much for taking the time to write such a detailed explanation - it's so helpful! I understand how the foreach loop works, and why the code only executes 3 times. I'm getting closer to understanding why we end up with 321, but I'm still confused about the $output variable.
What I don't understand is how: $output = 1 . "";
becomes
$output = 1;
I would have thought concatenating 1 and "" (ie 1 . "") would give us 1"". However, that's clearly not the case. Can you explain what those quotation marks are doing? Is it just because the $output variable can't be empty when it's declared? I suppose this would tie up with what you say about $output initially having 'no value'.