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 Simple PHP Application Working With Functions Working with Functions

Mike Miello
Mike Miello
5,181 Points

confused on how to calculate $output

$output = "";
$i = 0;

foreach($numbers as $number) {

    $i = $i + 1;

    if ($i < $total) {

        $output = $number . $output;

    }

}

echo $output;

?>

Can someone explain how to calculate output? I ran the php code and see the answer to be 321, but I don't understand how it is calculating the values. For example, I don't understand where the 3 is coming from.

Would appreciate some help. Thanks!

2 Answers

Is this the full code for the example?

Chris Shaw
Chris Shaw
26,676 Points

To go on what Kenya said you have quite a bit of code missing, E.g. the $numbers array and $total variable which I assume equals the value of 4, essentially based on that assumption this is what's happening.

With each iteration of the loop the value of $i is been tested against the number value for $total, if that number is less than $total its allowed to continue appending numbers onto the $output string which by the end of it should equal 321 based on my earlier assumption.

Of course without seeing the full code its very hard to say what the value of $number truly is.