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 trialTomas Vilemaitis
Courses Plus Student 424 PointsWhy output is in a reverse mode?
<?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;
?>
Why It outputs 321 instead of 123?? I Don't get It..
1 Answer
jcorum
71,830 PointsBecause 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;
Tomas Vilemaitis
Courses Plus Student 424 PointsTomas Vilemaitis
Courses Plus Student 424 PointsYeah... I realized it 10seconds ago, but much thanks to you, mate! :)