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 Switch Statements

Shin Mahilum
Shin Mahilum
958 Points

Help.. Wrong instruction? or my code is wrong.

What is wrong with my code.. If followed exactly as what the instructions told me but why is it still incorrect?

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

//change to switch statement
switch ($role != 'admin') {

  case 'admin':
    echo "You do not have access to this page. Please contact your administrator.";
    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 'As an admin, you can add, edit, or delete any post.';
    break;

}

2 Answers

Benjamin Larson
Benjamin Larson
34,055 Points

Hi Shin-

You have most of the format correct for a switch statement, but there's a couple problems with your code.

The main thing is that when you change from an IF statement, you're no longer evaluating the case ($role != 'admin'). Instead, you are just evaluating $role, and each of your cases will then check against the given role. Therefore, the first line of the switch statement should look like this:

<?php
switch ($role) {
  // cases to check
}
?>

Lastly, you'll need to flip the echo statements for default and admin as they are the reverse of what they ought to correspond with.

Shin Mahilum
Shin Mahilum
958 Points

Thank you very much.. It is very clear to me now.. my huge error was the switch statement.. Finally, now I understand.. thank very much for your time.