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

Changing set of if's to single switch statement

the original looks like this:

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

My attempt is attached. I also tried leaving the negation operator bc it says not to change the code above the switch statement (i assume it means inside the "switch()", and making the message to administrator the default instead. That obviously fails to satisfy another instruction. Please help.

switch.php
<?php

switch (isset($role)) {

  case 'admin': 
    echo "As an admin, you can add, edit, or delete any post.";
    break; 

  default:
    echo "You do not have access to this page. Please contact your administrator.";


} 
?>

1 Answer

Hi Douglas,

You have to keep the first if statement there. You're only changing the second if to a switch statement.

You're checking the value of $role to see if it matches any of the case statements.

You only need switch ($role)

You had a break after your first echo statement in your original code but you're missing that here. And did you keep the first if condition? because you're not showing it here.

Sorry, I missed in your first comment that you had the switch wrong too.

It should be switch ($role) as I had in my answer and you also need the break like you had in your original code.

Also, this post will show you how to properly format your code: https://teamtreehouse.com/forum/posting-code-to-the-forum

Thank you kindly for your continued input, Jason. I omitted the first if statement to make the comment shorter and I did type switch (role). That's why i deleted my first comment. I realized my mistake. I am still getting a syntax error though- "unexpected echo"- while using this code:

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

     default:
       echo "You do not have access to this page. Please contact your administrator.";
       break;
}

The error is in line ten at the second echo

I think part of the problem you're having is that when you go to make the suggested changes, you're also changing things that you previously had correct.

You want to keep the case statement then way you had in your original code.

case 'admin':

Your most recent attempt will pass if you make that change and keep everything else the same.

Thank you, Jason. You're right, it passed.