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 trialAmy Hsieh
6,023 PointsUsing Nested Condition Statement, I can't pass the challenge.
I think I am right. But I can't pass the challenge. Can't anyone point me out the problem?
The objective is: Without changing the variables, create a SINGLE conditional statement around the echo command that checks:
That a $username is set. The users $role is NOT admin
<?php
$username = 'sketchings';
$role = 'editor';
if ($username) {
if ($role!= 'admin')
echo "You do not have access to this page. Please contact your administrator.";
}
2 Answers
calp
10,317 PointsHello, your code is nearly correct. I have identified the following problems. You're not using the isset function to check if the $username variable is set. You're not using the and operator to check if both conditions are met. You also have some syntax errors in your if statement.
To check if the $username variable is set you need to use isset($username) as a condition in your if statement. You have got the condition check correct for your role but to tie them both together in the same if statement (so they both have to be true), you need to use the and operator.
Here is what your if statement should look like
if(isset($username) && $role != "admin") {
echo "You do not have access to this page. Please contact your administrator.";
}
Pontus Bolmér
12,471 PointsI would also like to point out that you are missing a space after you are checking your variable role.
Amy Hsieh
6,023 PointsAmy Hsieh
6,023 PointsThanks for pointing out. I got the "isset" part.
Since the "Nested condition Statement" can work like AND statement, would you mind showing me the correct code of using "Nested condition Statement" to achieve the same effect of using the "&&"? I guess I put the curly braces at wrong places?