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

Amy Hsieh
Amy Hsieh
6,023 Points

Using 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

index.php
<?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
calp
10,317 Points

Hello, 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.";
}
Amy Hsieh
Amy Hsieh
6,023 Points

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

Pontus Bolmér
Pontus Bolmér
12,471 Points

I would also like to point out that you are missing a space after you are checking your variable role.