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

How can I display the string with the sentence?

I've tried several things and I am stuck with this task for three days now. How can I display the string with the sentence or did I misunderstand the task (not native English)? Thanks for help.

index.php
<?php

//Place your code below this comment
$firstName="Rasmus";
$lastName="Lerdorf";
$fullname="$firstName $lastName";
//$fullname="$firstName $lastName was the original creator of PHP \n";
echo $fullname
  ?>

4 Answers

Sue Dough
Sue Dough
35,800 Points
$fullname="$firstName $lastName";

This is wrong because variables are not strings. They do not need quotes around them since the $variables already are going to output strings. You will need to concatenation to have more than 1 variable like this.

$fullname=$firstName . $lastName;

and this part needs fixing up too ( you would need to uncomment it aka get rid of the double slash in the front )

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

and do not forget your semi-colon at the end of the statement.

echo $fullname;

Variables will be expanded within double quoted strings in php so there was nothing wrong with $fullname = "$firstName $lastName";

If you reassign to $fullname the entire sentence then task 2 is no longer going to pass because it's expecting the $fullname variable to only have the first and last name in it.

You have to either echo out the sentence directly or you could probably assign the sentence to a different variable and then echo out that variable.

thanks a lot. now I have code this:

$firstName="Rasmus"; $lastName="Lerdorf"; $fullname=$firstName . " " . $lastName; $fullname=$fullname . ' was the original creatot of PHP' . "\n"; echo $fullname;

The output in workspaces looks fine, but in the Challenge Task it says: Ooops! It looks like Task 2 is no longer passing. How can that be?

Kristijan

$firstName="Rasmus";

$lastName="Lerdorf";

$fullname=$firstName . " " . $lastName;

$fullname=$fullname . ' was the original creatot of PHP' . "\n";

echo $fullname;

Hi Kristijan,

As mentioned in my other comment, you can't change the value of the $fullname variable.

Instead you can echo out the sentence directly without first assigning to a variable.

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

Another issue you had with your original code is that you had a space before the newline. This would have resulted in another error message since it wasn't specifically asked for in the instructions.

Okay now I understand. Thanks a lot! :D

Kristijan