Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

Working with Concatenation and Whitespace

I'm working on the tasks. And face some problems on the second step of task:

"Create a variable called fullName and assign it a value equal to the concatenation of the firstName variable, a space, the middleName variable, a space, and the lastName variable."

The answer area already has code:

"<?php

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

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

?>"

Think i'm supposed to answer between "$lastName = "Frog";" & "echo". And I've tried out:

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

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

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

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

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

None of those works. :/

Anyone has suggestion? Thanx a lot!

1 Answer

Stone Preston
Stone Preston
42,016 Points

the task states: "Create a variable called fullName and assign it a value equal to the concatenation of the firstName variable, a space, the middleName variable, a space, and the lastName variable."

it seems like the main cause of your confusion is that you think \n creates a space perhaps? the \n escape sequence does not create a space, it creates a new line. you can read more about escape sequences here.

you want a space which you can create by typing " " . its just a pair of quotes with a space inside.

using that knowledge, you can concatenate the variables with spaces in between like so:

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

Oops! Thanx a lot! Happily it works now. I'm going to the next step!

Stone Preston
Stone Preston
42,016 Points

no problem glad I could help