PHP PHP Arrays and Control Structures PHP Conditionals Switch Statements

Calvino Miguel
Calvino Miguel
4,193 Points

I dont' get why I'm not getting the default message of my Switch Statement

I had a role variable that was defined like this:

$role = 'subscriber';

then I created a 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;
}

I expected the "You do not have access to this page. Please contact your administrator." to be echoed, but the other case was being echoed the whole time. And when I assign the string 'admin' to the variable $role, I get the default message.

I thought that a switch statement works the same as a if statement. In this case the if statement would be something like:

if ($role != 'admin'){ echo 'You do not have access to this page. Please contact your administrator.'; } else { echo 'As an admin, you can add, edit, or delete any post.'; }

switch.php
<?php
//Available roles: admin, editor, author, subscriber
$role;
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;   

}

3 Answers

Adam Pengh
Adam Pengh
28,898 Points
<?php
//Available roles: admin, editor, author, subscriber
if (!isset($role)) {
    $role = 'subscriber';
}

//change to switch statement
switch ($role) {
    case 'admin':
        echo 'As an admin, you can add, edit, or delete any post.';
        break;
    case 'editor':
        echo 'As an editor, you can add or edit any post, and delete your own posts.';
        break;
    case 'author':
        echo 'As an author, you can add, edit, or delete your own post.';
        break;
    default:
        echo "You do not have access to this page. Please contact your administrator.";
        break;
}
Calvino Miguel
Calvino Miguel
4,193 Points

Hi Adam, thank you for taking your time to help me. I have already cleared the exercise. And I wrote exactly the same thing that you did and it worked out. The thing is that I have the impression that I didn't really understand how the switch statement works. Can you explain to me what happens when I write switch ($role != admin) ? What does the server process when I write that ? Thank you for your help!

Adam Pengh
Adam Pengh
28,898 Points

For a switch statement, you only put the variable to be evaluated. Then each case is equivalent to an "if" statement.

<?php
if ($i == 0) {
    echo "i equals 0";
} elseif ($i == 1) {
    echo "i equals 1";
} elseif ($i == 2) {
    echo "i equals 2";
}

switch ($i) {
    case 0:
        echo "i equals 0";
        break;
    case 1:
        echo "i equals 1";
        break;
    case 2:
        echo "i equals 2";
        break;
}
?>
Calvino Miguel
Calvino Miguel
4,193 Points

Thank you Adam, now it makes sense to me :)