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 trialScott Mowry
7,161 PointsWhy do I get "Notice: Undefined variable: location" on my preview page?
How come when I comment out [$location = "Orlando, FL";] in workspace I get "Notice: Undefined variable: location in /home/treehouse/workspace/index.php on line 26" on my preview page? When Hampton does it in the video he just gets a blank space where the location would be. I can't find any differences between his html and mine:
<?php
// This is my first name
$name = "Mike";
// $location = "Orlando, FL";
$full_name = "Mike the Frog";
// use full name as our name
$name = $full_name;
?>
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
<title><?php echo $name ?> | Treehouse Profile</title>
<link href="css/style.css" rel="stylesheet" />
</head>
<body>
<section class="sidebar text-center">
<div class="avatar">
<img src="img/avatar.png" alt="<?php echo $name ?>">
</div>
<h1><?php echo $name ?></h1>
<p><?php echo $location ?></p>
<hr />
<p>Welcome to PHP Basics!</p>
<hr />
<ul class="social">
<li><a href=""><span class="icon twitter"></span></a></li>
</ul>
</section>
</body>
</html>
3 Answers
Antonio Jaramillo
15,604 PointsSue obviously hasn't seen the video and doesn't understand where this video fits into our PHP learning process. "No, think about." isn't a productive comment. It's condescending. Hampton does indeed say that the $location variable will not display anything in the browser when commented out. He does not mention anything about turning on/off error reporting. This is the third, or something like that, video in PHP Basics. In that context, suggesting the use of extra code to troubleshoot is not productive either.
I understood why my browser was throwing the error, but it should be mentioned in the video that this will happen so that all learners understand why their results are different than their instructor's.
Kelly Drewett
2,430 PointsMy partner is a PHP coder and he says we get the error when we comment out $location because WorkSpaces PHP has got error_reporting on. You would not have this on a production server, in which case it would be silently ignored. I think this is an over site with Team Tree House.
Sue Dough
35,800 PointsI don't see any commented out code you are talking about.
First you should close your PHP statements with semi colons.
<h1><?php echo $name; ?></h1>
<p><?php echo $location; ?></p>
Second if your variable is undefined, you can check if its set.
<h1><?php echo $name; ?></h1>
<p><?php if ( isset( $location ) ) { echo $location; } ?></p>
Sue Dough
35,800 PointsOkay thanks for updating your code. My answer should work. If you comment out the variable, its going to be undefined. When you go to echo the variable, its not defined what it should be so its commented out. That is why you should use the isset() function to check if the variable is set. If its set then it will echo out the location otherwise it won't.
And the difference is probably environement. Your showing these types of errors while his settings may be different. You would be surprised how many production sites have undefined variables when you turn on more strict error reporting.
Scott Mowry
7,161 PointsI updated my question to include all the code. According to Hampton, if you comment out a statement like "$location = "Orlando, FL";" using "//" then any $location values should just disappear from the page. That's what he demonstrates in this video. Instead, when I do it, I get that eyesore of an error message.
Sue Dough
35,800 PointsNO. Think about that. Your still echoing that variable. Your magic comment isn't going to make the variable $location dissapear.
Scott Mowry
7,161 PointsWell then Hampton must be some sort of wizard because I don't see anything like an isset () function in his code.
Sue Dough
35,800 PointsYou can turn error reporting off. You could run that code without the error showing.
And NO he would be a wizard if he could make all the logic stop because of 1 comment.
kabir k
Courses Plus Student 18,036 PointsHi ghost code,
When I tried out your code for removing the initial error from commenting out $location variable, it threw out a syntax error on the preview page as follows:
Parse error: syntax error, unexpected '}', expecting ',' or ';' in /home/treehouse/workspace/index.php on line 27
Sue Dough
35,800 PointsI fixed it, sorry I didn't test when I wrote this.
kabir k
Courses Plus Student 18,036 PointsOK it works now, thanks.
Scott Mowry
7,161 PointsScott Mowry
7,161 PointsI've filled in all the code I have for my index page.