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

Deleted User

Undefined variable in header (Shirts 4 Mike)

Im doing Mikes t shirt shop but i ran into an unexpected problem... It says my variables are undefined. I have defined them in each corresponding page as shown. Any idea why it says its undefined?

< 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>

<?php $pageTitle = "Mike's Full Catalog of Shirts"; $section = "shirts"; include('inc/header.php'); ?>

<?php $pageTitle = "Contact Mike"; $section = "contact"; include('inc/header.php'); ?>

edit: here is the error message: ( ! ) SCREAM: Error suppression ignored for ( ! ) Notice: Undefined variable: section in C:\wamp\www\Test\shirts4mike\inc\header.php on line 18 Call Stack #TimeMemoryFunctionLocation 10.0003243992{main}( )..\index.php:0 20.0006247320include( 'C:\wamp\www\Test\shirts4mike\inc\header.php' )..\index.php:3 ">

One for shirts and the other one for contact.

8 Answers

Randy Hoyt
STAFF
Randy Hoyt
Treehouse Guest Teacher

Hmmm ... I can't quite tell the problem looking at the code on pastebin. Like James says, putting it at PHP playground may help us track it down. I'm unfortunately not familiar with the default settings for WAMP Server; that's what you are using, right? The code samples will work with XAMPP and MAMP's default settings, as well as on most production servers.

Here are a couple of things to try.

(1) In the files that are throwing the errors, add this before the include():

$section = "";
include("inc/header.php");

(2) If that doesn't work -- and only if number one doesn't work -- try adding this after the include().

include("inc/header.php");     
$section = $section;

I have read about people having an issue like this on some server configurations, with variables not getting shared with include files like usual ... though I have never experienced it myself. Let me know if that works for you.

Matt Orme
Matt Orme
4,746 Points

Sorry to comment on an old thread, but I resolved this issue by adding:

$section = "";

to index.php per this suggestion on SO:

http://stackoverflow.com/questions/14503650/undefined-variables

Resolved the issue auto-magically though I'm not entirely sure why.

Hope this helps someone else, and thanks for the awesome site. /derp,...I just realized "in the files that are throwing the errors" = index.php. Sorry to reiterate, but still hopeful this may help a fellow newbie.

Deleted User

Addition: I see this page when i go into the index, aka click on the site logo to go "home". The actual contact and shirts button becomes underlined, how can the variables be undefined if thats the case? If they were undefined, the underlining shouldnt work, right?

Mike Gabriel
Mike Gabriel
8,402 Points

On your very first line of code you posted, you have a space between your variable symbol $ and section.

<li class="shirts <?php if ($section == "shirts"){ echo "on"; } ?>">Shirts< / li> < li class="contact <? php if ($section == "contact"){ echo "on"; } ?>">Contact< / li>
Deleted User

That space is only there because i wanted to make the code show, just like u did, Here is the exact code from my folder. im going crazy here :/

shirts.php: http://pastebin.com/szZmB0zj contact.php: http://pastebin.com/hZAr5TKV

header.php: http://pastebin.com/qsbbfbt6

James Barnett
James Barnett
39,199 Points

@Dave - The forum uses markdown to format forum posts. Indent each line of code 4 spaces, or use a grave (aka backtick) symbol like so <tag>.

Instead of using pastebin.com when pasting code to a forum, a better solution is to use a PHP playground which interactively executes the code and it's much easier to quickly try out fixes.

I had the same issue discussed above and (1) solution worked great. Thanks for your help Randy!

Brad Halstead
Brad Halstead
5,662 Points

I had the same issue here too (I'm using wampserver). (1) solution worked for me too, thanks for posting this issue, and thanks for the help Randy!!