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 Build a Simple PHP Application Creating the Menu and Footer Adding Active States to the Navigation

Moritlha Madisha
Moritlha Madisha
5,014 Points

I'm getting undefined variable for the last section of conditional statements

I'm really struggling to solve this section of "Adding active states to navigation links". Everything worked up until this point. Here is the error: ( ! ) Notice: Undefined variable: section in C:\xampp\htdocs\Tutorial\treehouse\php\inc\header.php on line 18 Call Stack #TimeMemoryFunctionLocation 10.0024138032{main}( )..\index.php:0 20.0045140416include( 'C:\xampp\htdocs\Tutorial\treehouse\php\inc\header.php' )..\index.php:5 "> SHIRTS ( ! ) Notice: Undefined variable: section in C:\xampp\htdocs\Tutorial\treehouse\php\inc\header.php on line 19 Call Stack #TimeMemoryFunctionLocation 10.0024138032{main}( )..\index.php:0 20.0045140416include( 'C:\xampp\htdocs\Tutorial\treehouse\php\inc\header.php' )..\index.php:5 ">

Please find the code header table.

<html> <head> <title><?php echo $pageTitle; ?></title> <link rel="stylesheet" href="css/style.css" type="text/css"> <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Oswald:400,700" type="text/css"> <link rel="shortcut icon" href="favicon.ico"> </head> <body>

<div class="header">

    <div class="wrapper">

        <h1 class="branding-title"><a href="./">Shirts 4 Mike</a></h1>

        <ul class="nav">
            <li class="shirts <?php if($section == "shirts") {echo "on";} ?>"><a href="shirts.php">Shirts</a></li>
            <li class="contact <?php if($section =="contact") {echo "on";} ?>"><a href="contact.php">Contact</a></li>
            <li class="cart"><a href="#">Shopping Cart</a></li>
        </ul>

    </div>

</div>

<div id="content">

6 Answers

Moritlha,

Does the "error" prevent the page from loading or does the page load all the way and display the notice message as well. Notices shouldn't prevent the page from loading just display the notice message and continue to display the page like normal. The shirts.php and the contact.php is working since the pages add the value to the $section variable. Also, Try adding the error-suppressor '@' to your code as when you use the @ sign, it tells PHP to ignore any errors that may occur in the code; look at the revised code:

        <ul class="nav">
            <li class="shirts <?php @if($section == "shirts") {echo "on";} ?>"><a href="shirts.php">Shirts</a></li>
            <li class="contact <?php @if($section =="contact") {echo "on";} ?>"><a href="contact.php">Contact</a></li>
            <li class="cart"><a href="#">Shopping Cart</a></li>
        </ul>

I've never used the error-suppressor for IF statements so I forget if the @ goes before the IF or before the $section. As far as the php.ini goes, did you restart your server? Any changes that you do to your php.ini files needs to have the web server restart for the changes to take affect.

Cheers!

Hello,

Does the page still display regardless of the notice? Unless it's an error, warnings and notices will not disrupt the parsing process of PHP. Most of the time you will turn off notices and warnings under error reporting in the php.ini file and only allow errors and fatal errors to be displayed. Chances are you are accessing this page for the first time to which the variable $section hasn't been declared or a value hasn't been placed in it. For PHP, and for what you are using it for, it doesn't make a difference if the variable $section is good or not since the worse case scenario is that the CSS class "on" won't be displayed. Notices are there to give debuggers information while warnings are there to inform you that because of this warning, something may or may no happen that you are not expecting. Errors and fatal errors are there when something wrong has happened and the script cannot continue. Usually in a real world environment you will catch errors and display your own errors gracefully for your users. As long as the page is displaying properly, you are fine. Just turn off notices and errors in your php.ini file by putting: error_reporting = E_ALL & ~E_NOTICE & ~E_WARNING under the "Error handling and logging" section of the file. Hope this helps.

Cheers!

Moritlha Madisha
Moritlha Madisha
5,014 Points

Thanks Shaun. After carefully changing the error_reporting in the php.ini file I'm still getting the same error. I noticed I have php.in-development, php.ini-production and php.ini. I changed the settings in php.ini. Is is the correct one?

The code works when I go the shirts.php or contact.php link. However, on the homepage or the header page it does not work.

[bug](C:\Users\mmadi_000\Pictures\2014\bug.png "Title")

Moritlha Madisha
Moritlha Madisha
5,014 Points

Thanks Shaun. You are such a legend. Apologies for the delayed response.

I restarted XAMPP and it worked after making changes to the php.ini profile. There was something odd. I downloaded the files, which had completed the section I was battling to complete before making this change (restarting server) and it worked with the downloaded files. I looked at both the codes and they looked the same but somehow the original was not working prior to changing the server setting. Anything I could have missed?

Thanks again Shaun.

Moritlha,

I, too, downloaded the project files and took a look (before doing the PHP courses, I already was an advanced PHP programmer so I just breezed through the content) since I did the courses a long time ago when it became active. The only thing I can think of is that you didn't assign a value to the $section variable in the index.php file when you did it yourself seeing as if you did have the $section variable assigned in index.php, you wouldn't have gotten the notice in the first place. I know it could be difficult to troubleshoot an issue if it's from a file that isn't listed in the error message. You may have just forgotten to assign the variable.

Anyway, I'm glad you got it to work and remember, take your time and look at EVERYTHING as you program to make sure you don't miss a thing. When you get errors in real life, look at every script that is called in the page that has the error. Start from the top and work your way down and you will eventually find your error. Just remember, an error is just for reference; it gets you started in finding the problem code. Some errors won't even be on the line it states, but earlier in your code but the script-stopping error doesn't get caught until some time down the script. Good luck with your studies and have fun in the process. It's easier to learn when you are having fun.

Cheers!

Hi Moritlha,

The notice happens because we're not using the $section variable on the home page and so it's not defined on that page. The page still loads but the display of the notice inside the class attribute causes some unintended styling of the navigation. Each part of the notice is interpreted by the browser as a class name and so a few rules end up matching.

The way I handled this was to check if the variable has been set first before trying to use it. This way, you've fixed the problem that is causing the notice.

<li class="shirts<?php if ( isset($section) && $section == "shirts") { echo " on"; } ?>"><a href="shirts.php">Shirts</a></li>
<li class="contact<?php if ( isset($section) && $section == "contact") { echo " on"; } ?>"><a href="contact.php">Contact</a></li>