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 Statements & Comments

Eric C
Eric C
7,998 Points

when i comment out the php variable $location = "Orlando, FL" in workspaces, the preview has Undefined Variable error

Notice: Undefined variable: location in /home/treehouse/workspace/index.php on line 24

I don't see this error in the video, is this normal?

Julian Gutierrez
Julian Gutierrez
19,201 Points

Can you post the rest of your code?

3 Answers

Hi Eric!

If you look closer at your code you will see that this variable $location were used to display string in html.

 <h1><?php echo $name ?></h1>
 <p><?php echo $location ?></p>

When you comment or remove a variable declaration you will eventually receive such kind of error. Because when you call it, it holds nothing in it and php interpreter tells you that this variable is undefined. To remove this error, you should declare it and assign value to it, in order to receive proper output. In your case, you just need to remove comment and everthing will work as before ;)

Best regards!

sarahd0183
sarahd0183
Courses Plus Student 3,167 Points

Hi Serhii!

I, too, am having Eric's same preview problem. Although your answer makes a ton of sense, it does not reflect the same result that Hampton is showing in the video. Here's my snapshot: https://w.trhou.se/z3e3meoi39 Maybe I missed something? BTW: I'm using Mozilla Firefox but i tested my code on Microsoft Edge and still had the same issue. Perhaps it works correctly on Chrome?

I think the Workspace Hampton is using must have error reporting turned off server side which results in no error message appearing when you try to call $location.

Adding the line error_reporting(0); to the top of your PHP code will reflect the results that Hampton has but I would recommend against turning it off for learning purposes as it won't alert you to problems with your code.

<?php 

error_reporting(0);

// This is my first name
$name =  "Mike";

// $location = "Orlando, FL";
$full_name = "Mike The Frog";
$name = $full_name;

?>

Hi sarahduran!

As far as I can see in your workspace, you still have those comments in code.

Just remove them on line 7:

//$location = "Orlando, FL";

Without comments it will be simple variable declaration:

$location = "Orlando, FL";

And you will see the output ;)