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

Iván Uruchurtu
Iván Uruchurtu
7,264 Points

I have a mess on my head with this variable.

In "Working with Concatenation and Whitespace" video, Randy explains how to create a variable for echoing all input fields in one variable, but, why create a variable with a blank value before the other variables?

$name = $_POST["name"];
$email = $_POST["email"];
$message = $_POST["message"];
$email_body = "";  // < This variable (???)
$email_body = $email_body . "Name: " . $name . "\n";
$email_body = $email_body . "email: " . $email . "\n";
$email_body = $email_body . "message: " . $message;

echo $email_body;

what it means "" ? Sorry for my bad english.

5 Answers

Hello, logically it could be done without the first declaration.

Try to do it like that and see what is the result

$email_body = "Name: " . $name . "\n";
$email_body = $email_body . "email: " . $email . "\n";
$email_body = $email_body . "message: " . $message;
Iván Uruchurtu
Iván Uruchurtu
7,264 Points

Hi Omar, thanks for you answer, but I'm still a little confused, I have played around with the code doing this:

$email_body = $email_body . "Name: " . $name . "\n" . "E-mail: " . $email . "\n" . "Message: " . $message . "\n";

and i have the same result. Why you have declare the $email_body variable two times? like this:

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

or like the example I gave above. I hope I explained

Hello again Ivan,

No, look at the first line I gave

$email_body = "Name: " . $name . "\n";

which is without using the same variable 2 times but next you have to use it again to concatenate the whole body together, I mean you need to insert the Name, email and the message within one variable so you should use the same variable you declared before.

Let me explain more :

First we declared the variable and assign it the name like this

$email_body = "Name: " . $name . "\n";

then we need to add the email to it so we need to take the variable and concat the email with it like this

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

So you should use the variable again to be able to concat the whole body together, got it ? I hope I helped :)

A duplicate answer, sorry :)

Iván Uruchurtu
Iván Uruchurtu
7,264 Points

Yeah, I think I got, so every time I want to add a text string to a variable I have to declare that variable 2 times, right? If that's true, I'm done.

One more question Omar, is not more easier to do something like:

$email_body = "Name: " . $name . "\n" . "E-mail: " . $email . "\n" . "Message: " . $message . "\n";

I have the same result. Thanks for your time.

Yess sure you can, but sometimes for more complex email body you might need to separate them, also for more readable, maintainable and scalable code you need to separate them.

No need to thanks, so happy to help you. And you are welcome anytime :)

Iván Uruchurtu
Iván Uruchurtu
7,264 Points

Great! I got it. You're very helpful Omar, thank you very much. :D