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!
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

Alex Plaza
22,923 PointsHelp with PHP functions
I don't know how should i read this code, so the result give 321 which is the correct result. I thought it was 1234, like a string of the numbers on the array. This might be very easy but i cannot notice in what i'm wrong. If someone can point it out i would really appreciate.
<?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;
?>
THIS excersice and another similar to this appears on this quiz: http://teamtreehouse.com/library/programming/build-a-simple-php-application/working-with-functions/working-with-functions
Thanks!
8 Answers

Randy Hoyt
Treehouse Guest TeacherHey Alex,
Here are two points:
(1) Why is 4 missing from the output?
The $total
variable has a value of 4 because the array has four elements. (The count
function returns the number of elements in the array.)
As we loop through the foreach loop, the $i
variable increases by one each time so that it always corresponds to what item we're on as we loop. On the first element, $i
has a value of 1. On the second element, it has a value of 2, and so on.
Inside the foreach loop, the conditional checks if $i
is less than $total
. For the first three elements, it is. But for the fourth item, $i
has that same value (4) as $total
, So the fourth item is not included.
(2) Why are the numbers in reversed order?
The output starts blank. Look carefully inside the conditional to see how the output gets changed. The output gets assigned a new value equal to the current number first followed by the old value. The first time through the foreach loop, the number is one and the ouput is blank, so it is essentially doing this (and setting the output to "1"):
$output = "1" . "";
The second time through, the number is two and the output is already one, so its essentially doing this (and setting the output to "21"):
$output = "2" . "1";
The third time through, the number is three and the output is already two-one, so its essentially doing this (and setting the output to "321"):
$output = "3" . "21";
(As I mentioned above, the fourth time through $i
is not less than $total
, so the code inside the conditional is not executed.)
Does that help?

Alex Plaza
22,923 Pointsdon't know how to make the entire code appears as code, either :(

Jimmy Smutek
Python Web Development Techdegree Student 6,629 PointsHey Alex, try indenting with 4 spaces, or with a tab to create a code block.
<body>
Like this!
</body>
(note - tab doesn't work for me, pressing tab just selects the next item on the page)

Alex Plaza
22,923 Points@Randy Thank you so much! (1) now i understand how the 'if' works inside a foreach!
(2) silly me. I should, at least, see that one.
@Jimmy thanks, i think i got it

Randy Hoyt
Treehouse Guest TeacherGlad that straightened it out! :~)

Mauro Bonucci
5,953 PointsHad the same problem, i understood about the foreach and and the would be 1,2,3 since is $i< $total but i wasnt understanding why it was going reversed order.

Yasemin Sipahioglu
Courses Plus Student 1,241 Points"The output gets assigned a new value equal to the current number first followed by the old value. " as mentined $output = $number . $output; $number[3]:new.$number[2]:older.$number[1]:oldest 321

Ben Goldman
14,626 PointsWow, this took me a while to get. Are these equations really the best reflection of what is being taught in the video? I guess, theoretically they are. But in a more practical sense, maybe not so much?