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

PHP not reading output correctly?

Hi guys, this is such a simple task I don't see what I did wrong. At this point I literally copied the PHP from the workspaces from the video and it still is not working.

Simply trying to echo a variable in PHP.

<?php

$name = "Mike"

?>

<?php echo $name ?>

What oh what have I done wrongly?

index.php
<?php 

$name = "Mike";

?>

<?php echo $name ?>

2 Answers

Tiffany McAllister
Tiffany McAllister
25,806 Points

You forgot a semicolon after echo $name.

Also, you don't need to open a new PHP block, you can just echo $name underneath where you created the variable.

<?php
$name = "Mike";

echo $name;

?>

Goodness Gracious that worked. Thanks, Tiffany.

Just a quick follow-up question: Is the semi-colon required because there is no other language (such as HTML, which is in the Workspace for the video) between the two PHP statements? In other words, using only PHP will require semi-colons after each statment, regardless of whether a new PHP block has been opened. Correct? Sorry if this is confusing. Trying to get the concept down.

Tiffany McAllister
Tiffany McAllister
25,806 Points

Not a problem! :)

The semicolon for the last statement in a PHP block is optional, however, It is usually good practice to end each statement with a semi-colon. Sometimes the Treehouse interpreter for the challenges can be pretty strict which is probably why it wasn't letting your answer go through :)

You can read more about it at the following link:

http://php.net/manual/en/language.basic-syntax.instruction-separation.php