 # Functions question in "Making a Basic PHP website"

Hello, good people.

I just can't grasp the logic here - I think the concatenation is giving me trouble. Why is the answer 321, can someone walk me through?

Thank you so much!

Code:

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

?>

## 2 Answers Hello!

I'll try to break it down as best I can.

Here are your starting variables -> /********* \$numbers = [1,2,3,4]; (This is another way to write an array) \$total = 4; (count of the \$numbers array) \$sum = 0; (Doesn't seem to be used anywhere in the code though)

\$output = ""; \$i = 0; *********/

(1st loop) Inside your foreach loop, you start with \$i = 1 ( \$i = 0 + 1) and \$number = 1 (the first index of your \$numbers array). if ( 1 < 4 ) Which is TRUE \$output = 1; (because the concatenation of 1 and "" is just 1)

(2nd loop) Now increment \$i by 1 to get 2 and your \$number = 2 (the second index of your \$numbers array). if ( 2 < 4 ) Which is TRUE \$output = 21; (because the concatenation of 2 and 1 is 21)

(3rd loop) Now increment \$i by 1 to get 3 and your \$number = 3(the third index of your \$numbers array). if ( 3 < 4 ) Which is TRUE \$output = 321; (because the concatenation of 3 and 21 is 321)

(4th loop) Now increment \$i by 1 to get 4 and your \$number = 4 (the fourth index of your \$numbers array). if ( 4 < 4 ) Which is FALSE \$output stays the same

echo \$output; //Gives you 321

Hope that helps!