Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP PHP Basics (Retired) PHP Data & Structure PHP Statements & Comments

Scott Mowry
Scott Mowry
7,161 Points

Why 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>
Scott Mowry
Scott Mowry
7,161 Points

I've filled in all the code I have for my index page.

3 Answers

Sue 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
Kelly Drewett
2,430 Points

My 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
Sue Dough
35,800 Points

I 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
Sue Dough
35,800 Points

Okay 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
Scott Mowry
7,161 Points

I 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
Sue Dough
35,800 Points

NO. Think about that. Your still echoing that variable. Your magic comment isn't going to make the variable $location dissapear.

Scott Mowry
Scott Mowry
7,161 Points

Well then Hampton must be some sort of wizard because I don't see anything like an isset () function in his code.

Sue Dough
Sue Dough
35,800 Points

You 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
kabir k
Courses Plus Student 18,036 Points

Hi 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
Sue Dough
35,800 Points

I fixed it, sorry I didn't test when I wrote this.