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

Why does this not work in teh coding excercise

<?php

//Place your code below this comment

$firstName = "Rasmus"; $lastName = "Lerdorf"; $fullName = $firstName + $lastName;

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

?>

index.php
<?php

//Place your code below this comment

$firstName = "Rasmus";
$lastName = "Lerdorf";
$fullName = $firstName + $lastName;

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

?>

3 Answers

Felipe Giovanni de Moura Santos
Felipe Giovanni de Moura Santos
21,331 Points

What's up man...

We concatenate strings in PHP with a dot. Your code shouldn't have passed the second challenge (if you preview it instead of clicking on 'check work' you'll see that $fullname is echoing 0 ).

<?php

//Place your code below this comment

$firstName = "Rasmus";
$lastName = "Lerdorf";
$fullName = $firstName . '  ' . $lastName; 

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

?>

You may also use double quotes when defining $fullname (just like you did while echoing):

<?php

$fullname = "$firstName $lastName";

?>

ok thanks that worked... hmm php at all is completly new to me... and i didnt catch this way of combining 2 variables into one...

Daniel LittleThunder
Daniel LittleThunder
6,975 Points

It would seem to me that this should work to output the same thing:

<?php

$firstName = "Rasmus";
$lastName = "Lerdorf";
$fullName = "$firstName "."$lastName "."was the original creator of PHP.\n";

echo $fullName;

?>

As well as this:

<?php

$firstName = 'Rasmus';
$lastName = 'Lerdorf';
$fullName = $firstName .' '.$lastName;
$fullName .= " was the original creator of PHP.\n";

echo $fullName;

?>

But...I guess it won't accept those because it's changing the actual value of the $fullName variable? Seems to me to be an easier way to echo, but I am not familiar with PHP, so maybe there is an issue with adding to the value of the $fullName variable instead of adding the text to the echo command? I don't know.

Felipe Giovanni de Moura Santos
Felipe Giovanni de Moura Santos
21,331 Points

Yes, The variable $fullName was created to store a full name, compound by a $firstName and a $lastName.

Adding strings to it to display a message will go against its purpose/use. What you could do is create a different variable, like $message for example, and use it for displaying the message. You'll see that it'll pass the challenge because it did not ignore the instructions.

<?php

//Place your code below this comment

$firstName = "Rasmus";
$lastName = "Lerdorf";
$fullName = "$firstName $lastName"; 

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

 echo $message; 

?>