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 PHP Basics Daily Exercise Program String Manipulation

Can someone help with string concatenation?

I've tried a few different ways but can't seem to get it right. For example...

index.php
<?php

//Place your code below this comment
$firstName = 'Rasmus';
$lastName = 'Lerdorf';
$fullName = "$firstName " . "$lastName";
"$fullName was the original creator of PHP." . "\n";

?>

3 Answers

David Tomko
David Tomko
7,744 Points

You need to add echo at the end.

echo "$fullName was the original creator of PHP." . "\n";

Pedro Oliveira
Pedro Oliveira
1,272 Points

Hi, James Ackerman, everything fine?

To concatenate one string in PHP, we have several methods. I'll show you some of them:

$string = "String can have some $variables directly, but you will need always doublequote"; $string = "We can also ".$concatenate." some strings this way."; $string = sprintf('Or even, we could %s and insert our values', 'use a function to let things more clear');

Remember that as pointed before, you will always need to echo your string, otherwise, it will be just inside your code!

@David That got it. I thought I had tried it with echo, but apparently I used echo with an incorrect submission. I ended up assuming I was adding something that wasn't being called for. Thanks much!

@Pedro Thanks for the additional information!