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 Build a Basic PHP Website Listing and Sorting Inventory Items Working with Functions

Can anyone tell me why the below code outputs the array in reverse?

Quiz Question 1 of 8 What does the following code display?

$numbers = array(1,2,3,4);
$total = count($numbers);
$sum = 0;
$output = "";
$loop = 0;

foreach($numbers as $number) {
    $loop = $loop + 1;
    if ($loop < $total) {
        $output = $number . $output;
    }
}

echo $output;

?>

Thanks all

Edited to add markdown

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there, Matt Nickolls! I took the liberty of adding some markdown to your question so that it is a bit easier to read. Sure, I can guide you through why this is the case.

The first thing to keep in mind is that $output is an empty string and we're going to be concatenating it onto the end of another string each iteration.

Here's the loop part:

foreach($numbers as $number) {
    $loop = $loop + 1;
    if ($loop < $total) {
        $output = $number . $output;
    }
}

At the first iteration, $number will be equal to 1. $loop will also be set to 1. The $total variable holds the number of numbers in that array. So it is 4 and will not change. Is 1 less than 4? Yes, it is. So take the empty string and set it to the $number concatenated with the empty string and overwrite $output. So now $output is the result of "1" concatenated with "", which gives $output the value of "1".

Now onto the second iteration. The $number is now 2 and $loop is now 2. Is 2 less than 4? Yes, it is. So take $output which is now the string "1" and set it equal to the number (which is 2) concatenated with $output ("1") on the end. Now $output is equal to "21". Because we're taking the number and concatenating the previous version of $output on the end.

Third iteration. $number is now 3. $loop is 3. Is 3 less than 4? Yup. $output is "21". So we take the number (which is 3) and concatenate "21" onto the end of 3 and overwrite $output. The $output variable now has a value of "321".

Final iteration. $number is now 4. $loop is 4. Is 4 less than 4? Nope. Loop ends here.

So then we echo out the value of $output which is "321".

Hope this helps! :sparkles:

edited for additional information

It's the order of the concatenation that's important here. If this line:

$output = $number . $output;

had instead been...

$output = $output . $number;

then it would have echoed out "123" :smiley:

Thanks Jennifer, makes total sense.