Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP PHP Arrays and Control Structures PHP Conditionals Switch Statements

I just can't simple figure this out for some reason. Can someone quickly help if they can.

The current code I have displayed now isn't what I have been trying, I am barley trying this out now. I have been trying to figure this out for days so someone please help.

switch.php
<?php
//Available roles: admin, editor, author, subscriber
if (!isset($role)) {
    $role = 'editor';
  case 'editor':
  echo 'As an editor, you can add or edit any post, and delete your own posts.';
  break;
}

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

2 Answers

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

Hi there! You're amazingly close here, so I'm going to give some hints, and I feel like you'll be able to get it from these.

  • Your switch should not contain a comparison. You should simply switch on the value of the variable. For example: switch($myValue)
  • The echo in your default statement is missing. a semicolon at the end.
  • The default statement does not need a break

Try it again with these hints in mind, but let me know if you're still stuck! :sparkles:

Could you please show me a full example of what it should be so that I can analyze the different. I am a very hands on learner so I would need to see the bigger picture and then see the difference between them. Thank you!

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

There are only a few small errors in your code so I'm going to omit the admin, editor, and author cases as you did those just perfectly!

switch ($role) {  // This should not be a conditional.  This should simply look at the value of $role

   // The omitted cases go here

  default:
    echo 'You do not have access to this page. Please contact your administrator.';  //note the addition of a semicolon at the end
    // remove the break statement
}

Hope this helps! :sparkles:

Yesssssssssss!!!!!! It worked!!! Thank you!!