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.
}