PHP PHP Arrays and Control Structures PHP Conditionals Switch Statements

Angelie Sioson
Angelie Sioson
Front End Web Development Techdegree Student 2,996 Points

correct answer please???

It says i don't have the correct default blah blah.

switch.php
<?php
//Available roles: admin, editor, author, subscriber
if (!isset($role)) {
    $role = 'editor';
}

//change to switch statement
switch ($role = 'admin') {
  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;
}

4 Answers

jamesjones21
jamesjones21
9,243 Points

you need to put the $role variable in the parenthesis without the the equals 'admin' and then for each case the switch statement tests it will output the correct option. P.S do check your case for admin, you are forgetting the single / double quotes around the wording admin.

<?php
//Available roles: admin, editor, author, subscriber
if (!isset($role)) {
    $role = 'editor';
}

//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;
}
Angelie Sioson
Angelie Sioson
Front End Web Development Techdegree Student 2,996 Points

Hi James,

The answer you provided is still not accepted. The error:

"Bummer: I do not see the correct output for admin. Did you leave off a "break"?

jamesjones21
jamesjones21
9,243 Points

Angelie Sioson can you provide me with your code? But going by the error message you are are missing the break keyword after the first check. Each case is checked against the variable $role, if it matches, it then outputs the string and it will need to break otherwise it will carry on.

Your code should look like this, but by all means use it as a reference guide to correct your errors.

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

    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.";
}
Angelie Sioson
Angelie Sioson
Front End Web Development Techdegree Student 2,996 Points

The above posted code is my answer.

The code you provided is still not accepted on my end. It is saying I don't have the correct default message. I even copied and pasted your exact code but it is still wrong. Could it be a bug... hmm..

Amber Stevens
STAFF
Amber Stevens
Treehouse Staff

Angelie, when I copied and pasted James code into the code challenge it passed - for both part 1 and part 2... In your original code above it looks like you were on part one of the challenge and aside from needing the quotation marks around 'admin' you were also just missing a comma right after the words "as an admin[comma here]...." But the 2nd snippet of code that James showed you passes both parts of the challenge....

First, you want the $role variable by itself in the parenthesis. Second, make sure what the case value is comparing the variable to is in parenthesis since this is a string.

<?php
//Task 1
//Available roles: admin, editor, author, subscriber
if (!isset($role)) {
    $role = 'subscriber';
}

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