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 trialniloufarsedarati
Courses Plus Student 17,665 PointsOverloading when we using concatenation in $email_body?
Hi there, I'm wondering when we use the concatenation in each line for $email_body, is there any overloading because of repeating all the body in each line and if we have lot's of other details in our email body, do we face an overloading problem??
<?php
$name = $_POST["name"];
$email = $_POST["email"];
$details = $_POST["details"];
echo "<pre>";
$email_body = " ";
$email_body .= "Name: " . $name . "\n";
$email_body .= "Email: " . $email . "\n";
$email_body .= "Details: " . $details . "\n";
echo $email_body;
echo "</pre>";
?>
2 Answers
jcorum
71,830 PointsAs you know, it's the equivalent of this:
$email_body = " " . "Name: " . $name . "\n" . "Email: " . $email . "\n" . "Details: " . $details . "\n";
You have an interesting question about the effect, if any. But whatever is going on behind the scenes, there isn't much difference, in performance or memory terms, between these two ways of building a String from smaller Strings and variables. Plus as a programmer you have to do one or the other frequently. The advantage of the former is that it fits more easily, for longer Strings, in a coding window.
jcorum
71,830 PointsNot sure why you say "every line is repeated from scratch". New Strings are being created, yes, but the issue isn't how many, as new Strings are being created in each version. Rather, the issue you are raising, I think, is how many more are being created in this version:
$name = $_POST["name"];
$email = $_POST["email"];
$details = $_POST["details"];
$email_body = " ";
$email_body .= "Name: " . $name . "\n";
$email_body .= "Email: " . $email . "\n";
$email_body .= "Details: " . $details . "\n";
If I am counting correctly, this one starts with 4 Strings and ends up with 16.
In this version:
$name = $_POST["name"];
$email = $_POST["email"];
$details = $_POST["details"];
$email_body = " " . "Name: " . $name . "\n" . "Email: " . $email . "\n" . "Details: " . $details . "\n";
You start with 3 Strings and end up with 14. So perhaps slightly more efficient. But not enough to cause any concern.
You could be more efficient by doing this:
$email_body .= "Name: " . $name;
$email_body .= "\nEmail: " . $email;
$email_body .= "\nDetails: " . $details . "\n";
Thereby eliminating 2 extra Strings. But you could do the same in the second version.
P.S., I don't understand what you mean by "use echo instead". You echo out the result of either version. You can't use echo to create $email_body.
niloufarsedarati
Courses Plus Student 17,665 Pointsniloufarsedarati
Courses Plus Student 17,665 PointsThank you for your answer :) Yes, I understand ,but I'm still thinking it has a different performance in memory usage , because every line should be repeat from the scratch, while we can use echo instead.