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

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