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 Conditionals & Loops If / ElseIf Conditionals

Undefined variable

when i try to echo $role variable in html <p> tag php interpreter gives an error Notice: Undefined variable: location in /home/treehouse/workspace/index.php on line 29

Can you please post the code you are having trouble with.

5 Answers

Nicholas Jacob
Nicholas Jacob
7,536 Points

Has the $role variable been assigned a value? For example:

$role = "some value";

If so, make sure your syntax is correct. For example, maybe you're missing a semi-colon at the end of the statement.

$variable = 'yourdata';

can put your code to see what's going on?

Samuel Tissot-Jobin
Samuel Tissot-Jobin
7,350 Points

There is no code snippet so I can't be sure but usually a "NOTICE" like that probably means that you are accessing a variable that has not been declared yet.

//this will create the error
echo $variable;
//this won't

$variable = '';
echo $variable;

it's a question of initializing the variable to something before accessing it.

Mohamed Hak
Mohamed Hak
17,195 Points

I think this is not your first time learning PHP, and it seems you configured PHP to handle errors. maybe that's why it show you that there is something wrong with your syntax. hope this helps.

I guess this is expected behaviour although it doesn't show on Hamptons videos. Presumably you get the error when you're not a student or teacher and have "Foo" in the $role variable?

I get the same but I'm assuming this is because neither of the two scenarios apply and we're not definining the $info variable.. yet. Hopefully this will be picked up as the video progresses.