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

Jonathan Rogers
Jonathan Rogers
3,394 Points

PHP echo $name

Hey all,

Got a feeling its a silly one - But I'm rattling my brain trying to figure it out.

The work space for the Challenge starts completely blank. And requires two tasks.

1) Set the variable name to "Mike" - Complete.

<?php $name = "Mike" ?>

2) Echo the variable - The code below won't do anything.

<?php $name = "Mike" ?>

<?php echo "Mike" ?>

Am I missing something ?

index.php
<?php $name = "Mike" ?>

<?php echo $name ?>

2 Answers

Damien Watson
Damien Watson
27,419 Points

Hey Jonathan,

If you put them both in the same 'php' tags and don't forget the semi-colon:

<?php
  $name = "Mike";
  echo $name;
?>
Damien Watson
Damien Watson
27,419 Points

Actually the key would be the semi-colons but, I think the answer is looking for all in the one group... maybe.

Jonathan Rogers
Jonathan Rogers
3,394 Points

Thanks for the quick concise reply! Works fine either way it seems. But the challenge wanted it all in one code block (admittedly I didn't think about doing it this way.)

Stan Goldmann
Stan Goldmann
2,035 Points

I'm not 100% sure about it but I think it's because you're using 2 PHP tags.
I think the Server interprets this as "Oh you're starting php okay.. ok, you've finished".
.. Oh, you're starting php again? Let me execute the garbage collector. Okay, let's go.. Wait. What's this variable called $name? I don't know this one!

So basically I think it's fixed when you write it like that :
<?php $name = "Mike"; echo $name; ?>