PHP PHP Arrays and Control Structures PHP Conditionals Switch Statements

Samer Mahamid
Samer Mahamid
773 Points

I do not see the correct default output.

What is wrong with my code.?

switch.php
<?php
//Available roles: admin, editor, author, subscriber
if (!isset($role)) {
    $role = 'subscriber';
}

//change to switch statement
switch($role != 'admin') { 
 case "admin":
   echo "As an admin, you can add, edit, or delete any post.";
    break;

 default:
    echo "You do not have access to this page. Please contact your administrator.";
     break;


}                  
?>

1 Answer

Jason Tame
Jason Tame
5,033 Points

The problem with your code is the argument supplied to the switch statement - you are evaluating whether role is equal to 'admin' or not, which should be handled in the switch itself. . If you just pass it the $role variable like this:

switch($role) {
///Add your code here
}

then it will work fine.