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

Daniel Espinoza
Daniel Espinoza
796 Points

concatenation

$email_body= "";

$email_body.="name:".$name."\n"; $email_body.="email:".$email."\n"; $email_body.="message:".$message;

echo $email_body;

Niclas Valentiner
Niclas Valentiner
8,947 Points

What is the problem here? Is it not concatenating correctly or what? If that is the problem: what's the output?

Daniel Espinoza
Daniel Espinoza
796 Points

The problem is here: Create and assign variable called fullName equal to the concatenation of the firstName variable, space, MiddleName variable, space, and the lastName variable.

My syntax was the following:

< ? php

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

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

echo " The designer at Mike shirts Shirts April is named : " . $ fullName ;

? >

and I have an error: Bummer ! $ fullName is set, but it 's not the concatenation of $ firstName , $ middleName , and $ lastName Between them with spaces .

why?

2 Answers

Sean Do
Sean Do
11,933 Points

The problem is here: Create and assign variable called fullName equal to the concatenation of the firstName variable, space, MiddleName variable, space, and the lastName variable.

My syntax was the following:

< ? php

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

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

echo " The designer at Mike shirts Shirts April is named : " . $ fullName ;

? >

and I have an error: Bummer ! $ fullName is set, but it 's not the concatenation of $ firstName , $ middleName , and $ lastName Between them with spaces . ? why?

"\n" is a new line character. So, your output would look like this:

Mike

the

Frog

They're asking for spaces (" "), so:

<?php
$fullname = $firstName . " " . $middleName . " " . $lastName;
?>

Output:

Mike the Frog

It looks like the code you entered in your reply to Niclas Valentiner is somewhat misformatted and you also have some logical errors as well. So let me reflect upon your code before I show you the solution:

<?php

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

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

echo " The designer at Mike shirts Shirts April is named : " . $fullName ;

?>
  1. Your first line of code looks quite okay, although you have a white-space in the $firstName variable (" Mike") which shouldn't be there. Same error on $lastName -- remove the white-space. I would also write each variable on a separate row to make it easier to read ... but it's not wrong the way you have it now.

  2. Your second line of code has quite a few errors. 2a. First of all, with $fullName = ""; you declare a variable and give it an empty string as its' value. This isn't wrong programming-wise, but it's completely unnecessary here.

2b. Next, you have $fullName but you don't do anything with it (pehaps you forgot to add the = sign?).

2c. Your next error is this: $firstName = "\ n " $middleName "\ n " $lastName . . . . . ;. Here you are giving your variables $fullName and $middleName a new value of \n - which is the new-line character (like your enter-key on your keyboard).

2d. $lastName......;. I don't know what you were trying to do here, but this is also a syntax error.

  1. Your last line of code seems correct!

Anyway, so here is the solution:

<?php
  $firstname = "Mike";
  $middlename = "the";
  $lastname = "Frog";

  $fullName = $firstName . " " . $middleName . " " . $lastName;
  echo "The designer at Mike shirts Shirts April is named: " . $fullName; 
?>

So after you have initialized your variables with the correct values, you assign $fullName the value of:

  • the value of $firstName
  • a white-space
  • the value of $middleName
  • a white-space
  • the value of $lastName

which you then echo out.

Note that whenever you want to concatenate 2 different things (string with string, or string with variable, or variable with variable, etc) you use the period-symbol between the 2 elements. Think of it as a "+" sign for strings.

Hope this helped :)