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

Alex Nielson
Alex Nielson
2,300 Points

Instructions Poorly Worded; Objective Unclear

These are the instructions provided for the challenge:

In the code provided, there are two if statements. The second if statement displays a message IF the user role is not equal to "admin". We want to display separate messages based on the role of the logged in user. First we're going to add a message for our admin and keep the current message as the default. Step 1: Change the second if statement to a switch statement, keeping the current message as the default. Step 2: Add a second check to the switch statement for the role of "admin" and display the following message: As an admin, you can add, edit, or delete any post.

Even after I've changed the second if statement to a switch statement, I'm met with the error message that reads: "I do not see a case for 'admin'", despite the fact that the 'admin' role is the first case within the switch statement.

I also don't know what it means to "add a second check to the switch statement", as outlined in Step 2 of the instructions.

I feel like this challenge could benefit from a rewrite and instructions that are much more clear.

switch.php
<?php
/* In the code provided, there are two if statements. The second 
if statement displays a message IF the user role is not equal to "admin". 
We want to display separate messages based on the role of the logged in 
user. First we're going to add a message for our admin and keep the 
current message as the default.
Step 1: Change the second if statement to a switch statement, keeping the 
current message as the default.
Step 2: Add a second check to the switch statement for the role of "admin"
and display the following message:
As an admin, you can add, edit, or delete any post.
*/
//Available roles: admin, editor, author, subscriber
if (!isset($role)) {
    $role = 'subcriber';
} else {
  $role = 'admin';
}

//change to switch statement
switch ($role) {
  case 'admin' :
  break;
  case 'editor' :
  break;
  case 'author' :
  break;
  case 'subscriber' :
    echo "You do not have access to this page. Please contact your administrator.";
  break;
  default : 
    echo "You do not have access to this page. Please contact your administrator.";
  break;
}

4 Answers

Cool, I'm glad I helped!

can you set this as solved so other people who stuck on this can find help here

In the first if statement you don't have to add else at all.

in switch you're missing the messages for each role, here is an example how you do it:

<?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.";
}
Alex Nielson
Alex Nielson
2,300 Points

Thank you for the response =) This was actually the first time that I've posted a question, so I didn't realize that it would post the code that I had currently written.

The additional else condition that I added to the first if statement was me just scrambling for ideas because the code that I had first attempted to submit only produced the error code. In fact, the code that kept producing the "Could not find case for 'admin'" error looked identical to yours, except that 'editor' and 'author' were left blank.

--UPDATE-- I copied your code exactly as you've written and it's still producing the same error.

--2ND UPDATE-- I found the error. It was a syntax problem. I had a space between each of the cases and their corresponding colons. After removing those spaces, it all worked fine.

However, I am still confused as to what Step 2 meant by "Add a second check to the 'admin'." When I read that, I interpreted it to mean that we were supposed to perform a second check to verify that the value of $role was 'admin', which struck as me redundant. I still think clearer speech would help.

Thank you again for your response!

Hmm, i just tested it again, and it's working for me -> http://prntscr.com/g7o28p

Try again, maybe you are missing bracket or something

Alex Nielson
Alex Nielson
2,300 Points

It was a syntax error. When I copied and pasted yours I noticed that I had accidentally left some of the characters from my code, which produced an error. When I went through line by line to compare your code to mine, I noticed I had a space between each of my cases and subsequent colons. Thanks for the help though, Ivan =)

This really helped! They should provide more details as to how to complete the task. There's no way that I could've figured this out on my own. Thanks.