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 trialIván Uruchurtu
7,264 PointsI 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
Omar Zeidan
8,893 PointsHello, 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
7,264 PointsHi 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
Omar Zeidan
8,893 PointsHello 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 :)
Omar Zeidan
8,893 PointsA duplicate answer, sorry :)
Iván Uruchurtu
7,264 PointsYeah, 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.
Omar Zeidan
8,893 PointsYess 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
7,264 PointsGreat! I got it. You're very helpful Omar, thank you very much. :D