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

PHP Build a Basic PHP Website (2018) Listing and Sorting Inventory Items Working with Functions

Why output is in a reverse mode?


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


Why It outputs 321 instead of 123?? I Don't get It..

1 Answer

Because of the way you concatenate $number with $output. Each time through the loop you put $number in front of $output, not behind:

$output = $number . $output;

Yeah... I realized it 10seconds ago, but much thanks to you, mate! :)