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

Ronny Ewanek
Ronny Ewanek
4,385 Points

Echo statement not correct?

Why does the challenge exercise say this is not correct?

index.php
<?php

$name = "Mike"; 

?>


<?php echo $name ?>

3 Answers

Greg Kaleka
Greg Kaleka
39,021 Points

Hm. This should work. However, the challenge is being finicky, and wants all your code inside one php block. You should really be using semicolons everywhere, even though you don't need one in a single line php block. Once we combine the two statements, you do actually need it. Here's the solution:

<?php

$name = "Mike"; 

echo $name;

?>

It's good practice to always put the semicolons in but it's not needed on the last statement. The closing ?> php tag can end your last statement for you.

Juan Aviles
Juan Aviles
12,795 Points

I had the same problem as Ronny. While I appreciate the answer, I don't really understand why the challenge is asking for the echo inside the block. I thought the PHP block is where the variables are stored, and then they are called in the HTML.

My code is exactly like Ronny's, but before I move on by changing it to "just work", I'd like to know the reasoning. It seems to me that if you call the echo in the block, how do you use the HTML to display where on the page you want it?

Hi Juan,

The echo statement is a php statement and so it needs to be inside a php block in order to be interpreted and executed as php. The issue here was whether those 2 php statements could be in their own php blocks or if they had to be in 1 php block. In this case, the challenge was expecting them to be in the same php block.

What Ronny and Greg posted will both ultimately do the same thing it's just that the challenge is expecting what Greg posted.

Let me know if it's still not clear.

Juan Aviles
Juan Aviles
12,795 Points

Hey Jason,

I do understand the echo statement having to be inside the php block. It just makes for a confusing code challenge since it wasn't explained in the course this way. The way I understand it, the first block is where the variables are being initialized and then called later in the HTML in a separate php block.

I guess I just don't understand enough of php yet, and can't visualize a situation where an echo statement would be called the way they were asking for it in the challenge. I do appreciate the reply though.

Juan