Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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

Rasbin Rijal
PRO
Rasbin Rijal
Pro Student 10,863 Points

Quiz Question 3 of 9 Could someone explain how this code works / how it provide the output?

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

?> 

1 Answer

Matthew Bilz
Matthew Bilz
15,829 Points

Sure!

The variable $total stores the number of items inside the $numbers array (in this case, 4, because the count function will output the number of items in your array).

The $i variable is commonly used when you want to essentially auto-increment a count, i.e. every time the variable $i is passed through a loop, the value will be +1 from its iteration in the previous loop. (So, the values within the first loop are associated with an $i = 1, the values in the second loop are associated with $i = 2, the values in the 15th loop are associated with $i = 15 and so forth).

The $output variable starts off as a blank string because it will become a continuously 'mutated' variable throughout the looping process being executed. It will keep adding the $output value to itself over and over again. If it's an integer, the number will get higher, if it's a string, the string will become longer, etc. etc. This is good if you're adding up numbers in an array and looking for the sum or listing items in a statement or things of that nature usually.

In this specific example, whenever $i is less than 4 (this would be 3 instances because $i starts off as 1), the $output variable stores the number in the array at its respective position (technically, the number 1 is position 0 index) and concatenates the output variable to itself. There are no line breaks or spaces, so the final output string would be one single series of numbers with no spaces or line breaks.

The output eventually becomes 321 because the first loop generates a 1. The second loop adds a 2 to the existing $output variable, then becoming 21, then the 3rd loop adds the 3 to the existing $output of 21, finally ending the loop at 321 because $i becomes 4 and ends the loop because the conditional is then exhausted of $i being less than 4.

I hope this makes sense, I tried to be thorough yet simple! :)