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

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

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.

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; 

?>