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 Challenge (#2 of 4) - Build a Basic PHP Website: I'm getting an error when I use a concatenation method.

The challenge asks to assign the $fullName variable so that it is equal to the first three defined variables ($title, $firstName, and $lastName) with spaces in between.

I attempted to use the following code, but the site indicated that it broke the first task (setting $lastName to equal "Bowman"). Notice that I added a period (.) to the 2nd and 3rd instances where I define the value for $fullName.

$title = "Dr."; $firstName = "David"; $lastName = "Bowman";

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

Since this method didn't work, I used the other method:

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

which was less satisfying, but worked. I'd like to know if my first attempted code is valid or not, and if not, then why.

2 Answers

Daniel Gauthier
Daniel Gauthier
15,000 Points

Hey docgunthrop,

First of all, your code was definitely valid, but if you were typing it the way you wrote it out in this post then the problem was that you were putting a space between the . and the =, which would cause the code to fail.

The concatenation operator is written as follows: .=

I ran through the code challenge using your code and fixed it up to make certain it passed, which it did once I spaced things out a little more and fixed the concatenation operator.

$title = "Dr.";
$firstName = "David";
$lastName = "Bowman";
$fullName = $title . " "; $fullName .= $firstName . " "; $fullName .= $lastName;

echo "The lead character from 2001: A Space Odyssey is named ____";

Good luck with the course!

Christopher Parke
Christopher Parke
21,978 Points

$title = "Dr."; $firstName = "David"; $lastName = "Bowman";

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

This is valid but not not valid for the task because the ";" character tells the compiler that your statement is finished. So what you're telling the computer to do is :

"fullname is set to Title variable with a space at the end. Now fullName is set to the firstName variable with a space at the end. Now we're changing the fullName variable to equal the lastName variable with a space at the end (erasing the last step I just told you to do, mr computer)."

You didn't actually tell it to concatenate any variables. It's like saying something that you think is like "have a nice day" to somebody in Russian, but you're actually saying "The corner store is in the fish market". So no wonder you both look at eachother like "???".