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 trialErin Braxton
666 PointsHow do i echo back to the screen in php?
Asking me to echo a name back to the screen and it keeps saying it's wrong.
<?php $name = "Mike"; ?> <title><?php $name ?></title>
<?php
$name = "Mike";
?>
<title><?php $name ?></title>
8 Answers
Josh Cummings
16,310 PointsHi Erin,
In PHP, we use echo to output a string to the browser like so:
<?php
$name = "Mike";
echo $name;
?>
Hope this helps.
Ted Sumner
Courses Plus Student 17,967 PointsThis would work with your original code:
<?php
$name = "Mike";
?>
<title><?php echo $name ?></title>
The only difference is I added the key word echo in front of $name on the last line.
Ted Sumner
Courses Plus Student 17,967 PointsYou could also write it this way:
<?php
$name = 'Mike';
echo '<title>' . $name . '</title>';
?>
Erin Braxton
666 PointsI guess I'm confused because I didn't see that in the preceding lesson. Maybe I missed it?
Erin Braxton
666 PointsThanks for your response. So would this need to be included in the opening PHP code every time? The video seems to only show the echo to recall the name or location, etc.?
Josh Cummings
16,310 PointsEcho will only need to be used to output a string.
For example if we wanted to output Hello World to the browser, we would write:
<?php
echo 'Hello World';
?>
The example you provided used a variable called $name to store the string 'Mike' and than used echo to output that string to the browser.
More on echo here: http://php.net/manual/en/function.echo.php
Ted Sumner
Courses Plus Student 17,967 PointsYou only include echo when you want to output something to the screen. It can be HTML, a string, etc.
Erin Braxton
666 Pointsokay...thanks. Perhaps I just didn't understand the question but I think I get it.
Erin Braxton
666 PointsThanks, Ted. When the quiz asked me to echo Mike to the screen, I thought I had done that. So maybe I wasn't understanding the working of the question.
thomascawthorn
22,986 PointsHey Erin Braxton!
The correct code to pass the challenge is:
<?php
$name = "Mike";
echo $name;
Your code:
<?php
$name = "Mike";
?>
<title><?php $name ?></title>
is correct, and would indeed echo Mike's name to the screen. However, Treehouse code challenges are best described as 'picky'. It would be wise to answer questions without adding extra markup :)