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,314 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
9,433 Points

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