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 PHP Basics Daily Exercise Program String Manipulation

Having trouble adding space to the string.

I have reviewed the videos and did some searching online and cannot find how to add these two strings together and add space. What am I missing?

index.php
<?php

//Place your code below this comment
$firstName = 'Rasmus';
$lastName = 'Lerdorf';
$fullName = "$firstName" + "$lastName";

?>

1 Answer

Justin Radcliffe
Justin Radcliffe
18,987 Points

Try using PHP concatination like this:

$fullName = $firstName . ' ' . $lastName;

This:

. ' ' .

concatinates the two variables and adds one space character to the string.

It is still giving me an error and saying I did not add a space between first and last name. This course is frustrating because the code challenges often cover material not referenced in the videos.

Justin Radcliffe
Justin Radcliffe
18,987 Points

Looking at your original code, do this:

  1. remove the quotation marks as these are unnecessary. so: $fullName = $firstName + $lastName;
  2. remove the + character as this is a mathematical operator and the variables are strings. so: $fullName = $firstName $lastName;
  3. add the concatination. so: $fullName = $firstName . '' . $lastName;
  4. add a space character between the concatination. so: $fullName = $firstName . ' ' . $lastName; When I run this last code snippet in the challenge is passes.

Going through it again it passed. I appreciate your help.