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 Build a Simple PHP Application Adding a Contact Form Working with Concatenation and Whitespace

Aaron Lafleur
Aaron Lafleur
10,474 Points

Could someone explain the logical process of this expression? confused...

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

If the variable $email_body is first assigned an empty string, then reassigned to itself plus some concantenated strings and variables, why assign the variable $email_body at all? What is it for?

7 Answers

When a variable is declared it is assigned a memory location by the "compiler". That memory location may already hold a value so it's a good idea to "initialize" the variable. In the example you offered we want to make sure we are not concatenating with some junk. So we get rid of the potential junk by saying $email_body = "";. Empty quotes without a space in between them creates an empty string.

Stephen Crispini
Stephen Crispini
726 Points

I'm a little confused too as to why combining all the variables from the form into a single variable was needed. The instructor explains, "when we send out this email, we're going to need the entire body of the email in a single variable: we can't echo out the variables one at a time like we do on this page." Why is that exactly; is it just good practice as Aaron suggested?

Becky Castle
Becky Castle
15,294 Points

Hi Stephen, In a later video, the instructor will need to use that " $email_body " variable as an argument in the PHP Mailer code. (Part of that code looks like this: $mail->MsgHTML($email_body);. Since that method accepts just one argument, the instructor needed to condense the body of the email into a single variable ( $email_body ), beforehand.

Hi Aaron,

I think the example you've provided is just an exercise in concatenation and string variables. If the complete variable was displayed in an email it would look something like this:

Name: Some Name
Email: some@email
Message: the message

Without the newline character (\n) the display would look like this:

Name: Some NameEmail: some@emailMessage: the message

Hope this clears up some confusion.

Also, if you look just below the text box area you will see something that says Markdown Cheetsheet. Also, there is a post here titled: Posting Code to the Forum by Dave McFarland. If you don't understand that maybe this will make sense: How to display code at Treehouse.

Jeff

Aaron Lafleur
Aaron Lafleur
10,474 Points

Thank you, Jeff.

I'm wondering specifically about this block of code:

$email_body = "";
    $email_body = $email_body 
    $email_body = $email_body 
    $email_body = $email_body 

Are you saying that this is only to provide a space before "Name", "email", and "message"? I can't imagine what else it'd be for....

I have no idea what that is for. Where/what is it from? Please provide a link if youve one. The first $email_body = "": just declares an empty string.

Aaron Lafleur
Aaron Lafleur
10,474 Points

That code snippet is from the block I posted originally.

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

It is from the "Working with Concantenation and Whitespace" lesson. I understand the end result, however I don't understand why $email_body was used if it was assigned an empty string. My assumption is that it provides a blank space.

Mike Costa
PLUS
Mike Costa
Courses Plus Student 26,362 Points

Some developers do that so you have a set value (here its an empty string) when you instantiate the variable. I'm not a big fan of that method personally, I would just do this instead:

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

I think this is cleaner and easier to read for the developer who is working on your project, or a new developer just coming into the project.

Aaron Lafleur
Aaron Lafleur
10,474 Points

OK so it's a 'good practice' kind of thing. Gotcha. Makes a bit more sense. Will probably make more sense as I go through the lessons.

Thanks, guys!