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

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!!