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

No correct answer for two questions at Working With Functions of Build Simple PHP Application

Please see the following two questions at Working With Functions of Build Simple PHP Application

I do no find any correct answer at the list for the following two questions.

Q1, date, count, foreach, isset are all the PHP functions

Q2. My answer is 123, but does not match any answer at the list

Could you find the right answer?

Which of the following is NOT a PHP function? date count foreach isset

What does the following code display? <?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; ?>

4321 321 6 1234

3 Answers

Hi Boj,

Q1. foreach is not a function. It's a looping construct.

Q2. output would be: 321

Inside the loop you have the statement $output = $number . $output; So each number is being added to the beginning of the previous $output.

After the first time through the loop, $output is "1". Then next time through the loop, 2 is added to the beginning of "1". So you get "21"

Had the statement been: $output = $output . $number; Then you would be correct and it would be "123"

HI Jason,

I'm feeling pretty dense right now, but I don't understand your answer. I'm not understanding how each variable is interacting with each other. Could you talk me through how the loop is working? A step by step explanation of how the first output ends up as 1. I thought I understood loops, but for some reason I'm not wrapping my head around this one.

Hi S Ananda ,

I'll repost the for loop code here for reference:

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

Since there are 4 numbers in the array, the foreach loop will loop a total of 4 times.

First time through the loop -

$number will have the value 1 since that was the first number in the array.

$i gets incremented by 1. It was initialized to 0 before the loop so $i now equals 1.

The if condition is checking if $i is less than $total which was initialized to 4 since that's how many numbers are in the array. Since 1 < 4 is true the $output statement will be executed.

$output = $number . $output means, take the value of $number and then concatenate onto the end of it the value of $output and then store that value back in $output.

$number currently equals 1 and $output is still the empty string that it was initialized too before the loop. So you have $output = 1 . "": That gives the string "1" because adding on an empty string doesn't change anything. The new value of $output is now "1".

2nd time through the loop

$i now becomes 2.

The condition 2 < 4 is true so the $output statement gets executed.

Remember that the current value of $output is "1" and $number is now 2 so the assignment statement becomes $output = 2 . "1" This produces the string "21" and becomes the new value for $output.

3rd time through the loop

$i becomes 3 and number is also 3.

The condition 3 < 4 is true so we have $output = 3 . "21"; $output now has the value "321".

4th and final time through the loop

$i is now 4

The condition 4 < 4 is now false and the assignment statement won't be executed this time. $output keeps its current value of "321".

So the final output from the echo after the loop is "321".

Let me know if that helps clear it up for you.

Answer: Q1: foreach Q2: 321

Hi Jason Anello,

Thank you sooooooo much for going through the entire sequence. I'm seeing I need to slow down and really look at what each part of the code is doing. I didn't get the concatenation part correct, so it meant I got the wrong answer, duh. Sorry it took me so long to respond, I had a back injury and was laid up for a bit. Am back coding now and with a refreshed reminder to walk through the code slowly, so I really understand it. I'm in such a hurry to learn and realize it is not doing me justice to do so. It will just hurt me in the long run. Slow and stead wins the race...

Yikes, should be slow and steady wins the race...