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

Echo variable

Where am I going wrong?

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

<html>
  <body>
    <h1><?php echo $name ?></h1>
  </body>
</html>

7 Answers

Matt Trask
Matt Trask
10,027 Points

youre missing a semi colon. I dont wanna give it up, but remember, most lines of PHP will always end with a semi colon.

Bummer! I am not seeing the word "Mike" in the output. Did you echo something different?

This is all I get trying to print Mike on the screen, after adding semi colon to the end of the first code block after the "mike"

Matt Trask
Matt Trask
10,027 Points
<?php
$name = "Mike"; <- semi colon here
?>

<html>
  <body>
    <h1><?php echo $name;(since you close out the PHP block, you can forgo a semi colon, but for good practice I added it) ?></h1>
  </body>
</html>

I didn't know this was best practice, have you got a reference by any chance :-)

Matt Trask
Matt Trask
10,027 Points

Ive seen it before Tom Cawthorn, but lemme look to find it. I think its apart of the PSR's

<?php
  $name = "Mike";
?>

<html>
  <body>
    <p><?php echo $name?></p>
  </body>
</html>

This is what I have, its still showing the same message. Im stumped.

Matt Trask
Matt Trask
10,027 Points

add a semi colon after the echo statement in the html body.

Steven McKay Lowry
Steven McKay Lowry
2,015 Points

Could try:

<?php
$name = "Mike";

echo '<html>';
   echo '<body>';
      echo '<p>';
         echo $name
         echo '?';
      echo '</p>';
   echo '</body>';
echo '</html>';
?>

I think it might be because the variable you're trying to choose is between two different PHP segments. (Not global)

Your code looks totally fine - are you struggling passing a challenge or is this in your own local environment?

It was the code challgne, I have passed it now. The echo php had to be done outside of html elements

Code challenges are very picky! If your feel it wasn't explained properly in the question, you can always send in an email to the support team, or leave feedback at the end of the badge to get it updated.

Glad you got it through!