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 trialJerry Schrader
4,396 PointsUndefined variable: wptreehouse_username in...
I am getting the error: Undefined variable: wptreehouse_username in when I put the 2nd if statement: if( isset( $wptreehouse_username ) || $wptreehouse_username != ''):
it goes away when I put a username in, and Mike the Frog appears, but when I refresh without a username, the error comes back.
EDIT:
I had error reporting turned on in wp-config, when I turned it off, it went away, nonetheless, why would this show the error?
george george
5,755 Pointsyou forgot the " ! " sign in front of the isset
Jerry Schrader
4,396 PointsThanks but I think I may not have been clear on the part of code, this is for the sidebar, which should show the box if there is a username submitted, OR the input is not blank, AKA someone has put something in the input. If I add the !, that says if no username was submitted.
What I am trying to produce is : if username IS set:
if(isset( $wptreehouse_username )
OR (||)
$wptreehouse_username is NOT blank :
$wptreehouse_username != '')
show the box. It is throwing the error that the variable is not defined, yet in the code above this, it works fine, (for the main part of the body).
3 Answers
Ben Attenborough
Front End Web Development Techdegree Graduate 32,769 PointsOkay here is my attempt at the answer.
I believe the code is wrong and here's why:
$wptreehouse_username is only set once the form has been submitted. So the line
if( isset( $wptreehouse_username ) || $wptreehouse_username != ''):
first checks to see if $wptreehouse_username is SET(does the variable exist and if it does is not NULL). Since this fails it checks the OR statement $wptreehouse_username != '' which throws an error since the variable it's been asked to check hasn't been created yet.
The solution is just to stop after checking if the variable has been set:
if( isset( $wptreehouse_username ) ):
Or alternatively you could switch the OR symbol for an AND:
if( isset( $wptreehouse_username ) && $wptreehouse_username != ''):
However I cannot see the point of this as the second half of that statement is just repeating the first half (but doing it in a way that throws an error - circumvented here by first checking if the variable exists, it doesn't and as both statements need to evaluate as true the program moves on before evaluating the non-existent variable).
Ben Attenborough
Front End Web Development Techdegree Graduate 32,769 PointsActually I was slightly wrong. The second option:
if( isset( $wptreehouse_username ) && $wptreehouse_username != ''):
is the one to go for, as will become clear in step 9 (CRUD with the WordPress Options Table). It is possible to have the variable set but with no data, such as when the user just press enter with nothing entered in the field.
Yan B
2,785 PointsThanks Ben, it works!
Zhenlin Jin
Courses Plus Student 167 PointsThanks, Ben! The teacher doesn't know what he's talking about!
george george
5,755 Pointsgeorge george
5,755 Points