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

What does this mean? $output = $number . $output;

Here is the quiz and the answer is 321. What does ”$output = $number . $output;” mean?


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;

?>

2 Answers

Carlos Federico Puebla Larregle
Carlos Federico Puebla Larregle
21,074 Points

The "." (dot) is used for concatenation, so in this case you are going to assign the concatenation between the "$number" variable and the variable "$output" to the variable "$output".

Here's another example of concatenation using the "." (dot).

<?php
$a = "Hello ";
$b = $a . "World!"; // now $b contains "Hello World!"

$a = "Hello ";
$a .= "World!";     // now $a contains "Hello World!"
?>

I hope that helps a little bit.

Thank you very much. :) I could understand well. If you have facebook account, please add me. https://www.facebook.com/GangstaCatAkidsuki

Matthew Smart
Matthew Smart
12,567 Points

$output = $number . $output;

$output is first made an empty string by $output = ''";

Now what we are saying it that the new $output is the $number and the current $output.

So if $output = 1

And i say $output = $output .'-'. $output;

then when i echo $output it would say 1-1

the dot is concatenating the values together.

Thank you very much. :) I really appreciate it. If you have facebook account, please add me. https://www.facebook.com/GangstaCatAkidsuki