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

What's Wrong?

What am idoing wrong? The challenge is to put "Mike" (the name) on the screen.

index.php
<?php

$name = "Mike";

?>

    <h1><?php echo "Mike" ?></h1>

2 Answers

Erik McClintock
Erik McClintock
45,783 Points

Meg,

They want you to echo the variable out in the same PHP block in this challenge, they don't want you to create h1 tags or a new PHP block.

Note: this challenge has failed students before for opening a second PHP block, hence the suggestion. Sometimes they can fail out for the oddest, smallest things.

Erik

Shane Meikle
Shane Meikle
13,188 Points

And if I am not mistaken, they want the use of the variable instead of hardcoded.

so:

echo $name;

instead of

echo "Mike";

They don't have to be in the same code block, but you are correct that there shouldn't be any elements in the page.

This is perfectly valid and will pass, as well:

<?php
$name = "Mike";
?>
<?php
echo $name;
?>
Erik McClintock
Erik McClintock
45,783 Points

Yes, Marcus Parsons, it looks like they have updated this challenge. Before, it was failing out for people when they had a second PHP block opened. Now it appears that is no longer the case, though you still don't need/want to open up a second block to echo this out.

But you can and it is valid to do so ;)

Erik McClintock
Erik McClintock
45,783 Points

Not disagreeing that it is now valid for the challenge, I'm merely proposing better practices and simplicity!

I didn't say it was a best practice just that it was valid lol! Thanks. Come again.

Just like it's helpful to tell people that they can't put code into a new code block. LOL. That's very helpful.

Ohhh, OK.

Craig Watson
Craig Watson
27,930 Points

Hi Meg,

Sorry, I didn't get your answer request till this morning, it looks like you have had the correct answer provided in one way or another already.

What Marcus has mentioned is correct you can use two PHP blocks to achieve what the code challenge is asking and it would be perfectly valid PHP.

However, if you had the need to execute this code on a live site as we all know we would take the option of using as little code as possible.

Sooo......

<?php

    $name = "Mike";

    echo $name;

?>

Again apologies for the delay :) Craig

In a live site, you often wouldn't just echo out the name in the same code block. You would often get the name from a database query or from a cookie early on in the page and then use it somewhere in the body using up two different code blocks. Even though Meg's code doesn't pass the challenge, that is how this information would often be used. Thank you. Come again.

Btw, Craig, you're not simplifying your code block. You have too many spaces and extra lines and are therefore not using the simplest approach. So, nice try, but not quite. Let me help you out with that since you need it though:

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

You guys need some lessons in simplicity it looks like.

When someone comes on to an answer and gives a correct, valid answer, you shouldn't do whatever you can to try to discredit that answer unless the answer is wrong which it is anything but. You will more often see variables created in one block and used in another in PHP. Again, you all need some practice with PHP before you figure this out, but that's okay.

Craig Watson
Craig Watson
27,930 Points

Very sad really, I can assure you Meg not every question on treehouse is met with such a negative tone.

I had no negative tone whatsoever. If you interpret it that way, I feel sorry for you.

I am almost always positive in my responses except for when people who are still obviously new to a certain language come on to a question and try to discredit an answer I give for no reason. It wasn't that long ago that you were asking basic HTML and CSS questions.

Craig Watson
Craig Watson
27,930 Points

Thank you so much treehouse for the Unfollow Question button!!!!!