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 Challenge Task 2 of 2 Not Passing

I keep getting the message: "Bummer! I do not see the correct output for editors. Did you leave off a "break"?" When I try to submit this. Can't figure out what I'm doing wrong. Any help would be greatly appreciated.

The Task: Add a check for the role of "editor" and display the following message: As an editor, you can add or edit any post, and delete your own posts. Add a check for the role of "author" and display the following message: As an author, you can add, edit, or delete your own post.

My Code:

//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;
  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;
}

5 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! You're doing very well, but your switch statement is a little off. If I change this:

switch($role == 'admin'){

To this:

switch($role){

your code passes! Remember, we are just looking at the value of $role. It will go through all those cases and find the one that matches. Hope this helps! :sparkles:

Thanks, Jennifer! It was so obvious after I read your reply; don't know why I didn't see it before. Many thanks.

Harrinson Ariza
Harrinson Ariza
8,189 Points

why my code doesn't work?

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

//change to switch statement if ($role != 'admin') { echo "You do not have access to this page. Please contact your administrator."; } else {

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;

}

}

Note: Thanks for help me!

Hi Harrinson. You should delete if statement (if ($role != 'admin') { echo "You do not have access to this page. Please contact your administrator."; }else{}) and keep just switch statement.

Harrinson Ariza
Harrinson Ariza
8,189 Points

Hello, Ilya Zaberezhnyy. Thanks for you support. Thanks.....

Nasir Atta
Nasir Atta
2,177 Points

Help!!

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

//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.";
} ?>

error message: Bummer! I do not see the correct output for editors. Did you leave off a "break"?

Hi! You should add "break" for "default".

Samandar Mirzayev
Samandar Mirzayev
11,834 Points

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

}

//change to switch statement if ($role != 'admin') { echo "You do not have access to this page. Please contact your administrator."; }else{ 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;

} } ?>

should be ok, just try it

vinayak sapkal
vinayak sapkal
9,802 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; } ?>