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

concatenation php heplz!

concatenation php

Works in the refresh preview. But it's not accepted.

<?php

$firstName = "Mike"; $middleName = "the"; $lastName = "Frog";

$fullName = $firstName . "\n" . $middleName . "\n" . $lastName;

echo "The designer at Shirts 4 Mike shirts is named $fullName ";

?>

preview: The designer at Shirts 4 Mike shirts is named Mike the Frog

8 Answers

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

A \n produces a new blank line in PHP, not a single space.

It looks like it's working because HTML ignores blank lines. (I should probably add a friendly message for that scenario.)

You want to change this ...

$fullName = $firstName . "\n" . $middleName . "\n" . $lastName;

... to this ...

$fullName = $firstName . " " . $middleName . " " . $lastName;

Does that help?

yay thank you!

(Was I asleep when you mentioned " " ;-)

I have just completed Concatentation!

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Nice! No, you didn't miss me talking about spaces. I suppose I never exactly covered how to write a space by itself, but we've definitely been writing spaces in our variable declarations and our echo statements. :~)

Thanks Randy. I had been writing spaces " " ... but didn't know it. lol

So, now I am a bit confused.

Question:

$email_body="";

Is this a variable with a space after - or a variable that is a space - or ...

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

No, this is blank. It doesn't have a space in it; it has nothing in it. I didn't talk about it, but there are two reasons why I recommend this approach:

(1) Often times with concatenation, you want to rearrange the pieces of text. You can usually do this by rearranging the lines of code. But let's say you have these two lines that display "AB":

<?php
    $text = "A";
    $text = $text . "B";
    echo $text;
?>

If you rearrange them, you won't get "BA"; you'd just get "A":

<?php
    $text = $text . "B";
    $text = "A";
    echo $text;
?>

When building a long piece of text like this, I like to create an empty variable first and then concatenate all the values to the end of it.

<?php
    $text = "";
    $text = $text . "A";
    $text = $text . "B";
    echo $text;
?>

(2) In a lot of programming languages, you have to do something different to declare a variable the first time you want to use it. JavaScript in strict mode is a good example. You use the keyword var the first time you declare a variable:

<script>
    var  text;
    text = text + "A";
    text = text + "B";
</script>

PHP is a little looser than many other programming languages, so this isn't strictly necessary. But I find that creating the variable empty first and then adding values to it still makes good sense. (I plan to talk about that later in the intermediate course when we talk ind detail about variable types.)

Thank-you for taking the time to explain all this to me.

Now I'm on to more php videos :-)

Whew! glad this was on here... :)