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

Shahid khan
Shahid khan
790 Points

How to solve this problem set???I have no idea

I am getting error

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

1 Answer

PHP switch statements are written like this:

<?php
switch ( /* some kind of condition or variable */ ) {
  case ( /* some value */ ):
    // do something if the condition/variable equals this value
    break;
  case ( /* another value */ ):
    // do this is the condition equals this other value
    break;
  default:
    // do this if none of the other cases equal the condition
}

So this is probably something along the lines of:

<?php
switch ($role) {
  case "subscriber":
    // do something
    break;
  case "admin":
    // do something else
    break;
  // etc.
}