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 Arrays and Control Structures PHP Conditionals Operators

Michael Mason
Michael Mason
4,677 Points

PHP Arrays & Control Structures: Preview shows I'm getting the right result, but I'm not passing the code challenge...

I'm attempting to pass a code challenge, and I feel I've done everything right whilst following the instructions... I even get the right result when I preview my code; however, the error prompt says 'Bummer! Use the negation operator to check that $role is NOT EQUAL to "admin".

Could this be a bug in the code that parses the text for the answer? I'm confused...

index.php
<?php
$username = 'sketchings';
//Available roles: author, editor, admin
$role = 'editor';

//add conditional statement
if ($username!='' && $role!='admin') {
echo "You do not have access to this page. Please contact your administratior.";
}

3 Answers

Michael Mason
Michael Mason
4,677 Points

(So I can mark this Community Discussion as "Answered") As it turned out, a lack of white space around the inequality statement for " $role!='admin' " was triggering the Challenge to display an error message, for lack of a better term. When I added space between $role, the inequality statement, and the 'admin' String, Treehouse then proceeded to evaluate the additional criteria needed to complete the code challenge, but had I not gotten help from Jennifer Nordell, I may have still been stuck on the other criteria.

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! No, it's not a bug. Setting a variable to an empty string is still setting a value. Even though it's an empty string it is not the same thing as NULL. You can see more more documentation on this and about the isset language construct.

Also, your condition should be checking if either of those are true, not both. If the value for $username is not set OR the $role is not equal to 'admin', then the echo should occur. Otherwise, nothing should print.

I hope this helps, but let me know if you're still stuck! :sparkles:

Michael Mason
Michael Mason
4,677 Points

That is great insight, and very helpful info! However, even if I erase both conditions and just add the condition "if ($role!='admin') ", I get the same response: 'Bummer! Use the negation operator to check that $role is NOT EQUAL to "admin".'

Just to be sure, though, I did try out the full statement again, this time using OR ("||") and the isset function: if (!isset($username) || $role!='admin') { echo "You do not have access to this page. Please contact your administratior."; }

I get the same error. :-(

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Michael Mason you're correct. That's a fairly misleading Bummer! message. But you won't believe how close you were with the code you posted in your comment. In essence, it boils down to the placement of your negation operator before the isset language construct. Take a look:

//add conditional statement
if(!(isset($username)) || $role != 'admin'){ 
   echo "You do not have access to this page. Please contact your administratior.";
}

You need the negation operator on the outside of that construct and then check if role is not equal to admin. Now why it shows that particular Bummer! message, I have no idea.

Hope this helps! :sparkles:

Michael Mason
Michael Mason
4,677 Points

Thanks so much for taking the time to try and help me, Jennifer. Your input was invaluable. I finally figured out what was triggering the error - turns out, it was because I did not place a space on both sides of my evaluation statement. Instead of " $role!='admin' ", it worked when I used " $role != 'admin' ".

Such a simple mistake to induce an error, but hey -- that's why it's called "learning," right?

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Michael Mason actually, that might should be reported as a bug as I'm convinced that PHP in itself isn't picky about the white space. But I know that some of these challenges can be.

Clint Gorman
Clint Gorman
534 Points

I was stuck on this for about 20 minutes. Michael had the right answer in my case, there was no space on both sides of the != and I was getting the same error.

here's the code that worked:

if (isset($username) && $role != "admin"){
echo "You do not have access to this page. Please contact your administrator.";
}