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 (Retired) PHP Data & Structure PHP Variables

Erin Braxton
Erin Braxton
666 Points

How 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>

index.php
<?php 
$name = "Mike";
?>
<title><?php $name ?></title>

8 Answers

Josh Cummings
Josh Cummings
16,310 Points

Hi Erin,

In PHP, we use echo to output a string to the browser like so:

<?php

  $name = "Mike"; 
  echo $name;

?>

Hope this helps.

This 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.

You could also write it this way:

<?php
$name = 'Mike';
echo '<title>' . $name . '</title>';
?>
Erin Braxton
Erin Braxton
666 Points

I guess I'm confused because I didn't see that in the preceding lesson. Maybe I missed it?

Erin Braxton
Erin Braxton
666 Points

Thanks 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
Josh Cummings
16,310 Points

Echo 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

You only include echo when you want to output something to the screen. It can be HTML, a string, etc.

Erin Braxton
Erin Braxton
666 Points

okay...thanks. Perhaps I just didn't understand the question but I think I get it.

Erin Braxton
Erin Braxton
666 Points

Thanks, 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.

Hey 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 :)