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.
Pepe Suarez18,267 Points
Concatenation best practices
Hey Guys! I have been wondering for a while now why do we concatenate in PHP when we can echo a the variable inside a string? To make myself more clear let me do an example:
$name = "Mike"; echo "Hello $name"; echo "Hello " . $name;
Why do we use always use the second echo statement in PHP? Why don't we use the first one.... Thanks for the help but the doubt is killing me and I couldn't find anywhere an answer.
In those cases it's more a matter of preference. Still, an advantage is that it can help with making code more immediately readable.
If you choose to continue as a PHP developer, you'll find yourself in situations in which it will be much more technically convenient to use the concatenation operator, but examples like the one you mentioned are probably more for the sake of familiarizing learners with the operator in and of itself.
The Build a Basic PHP Website course here at Treehouse provides a useful example of using the concatenation operator when building a string that involves the use of PHP in conjunction with retrieving data from HTML forms.
Interestingly (source: http://artprima.eu/925-string-variables-concatenation-vs-variable-interpolation.html)... the compiler tends to work faster with the non-concatenated approach.