PHP PHP Arrays and Control Structures PHP Conditionals Switch Statements

June Westerfield
June Westerfield
2,138 Points

Having problem with switch challenge

I know my code is correct, but it's saying that the default isn't there, but it is and it is correct. Unless I'm really missing something

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

//change to switch statement $role = 'admin'; switch ($role) { case 'admin': echo "As admin, you can edit, or delete any post."; break; default: echo "You do not have access to this page. Please contact your administrator."; break; }

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

//change to switch statement
$role = 'admin'; 
switch ($role) {
    case 'admin':
        echo "As admin, you can edit, or delete any post.";
        break;
  default:
    echo "You do not have access to this page. Please contact your administrator.";
    break;
}

4 Answers

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

//change to switch statement
$role = 'admin'; // why is this needed
switch ($role) {
    case 'admin':
        echo "As admin, you can edit, or delete any post."; // there are words missing in this statement.
        break;
  default:
    echo "You do not have access to this page. Please contact your administrator.";
    break;// do you need a break, after the last statement, which is generally the default statement.
}
Amber Stevens
STAFF
Amber Stevens
Treehouse Staff

I agree with Antonio, if you correct your first echo statement (which is missing words) and also remove the part that says $role = 'admin' your code will work

June Westerfield
June Westerfield
2,138 Points

When I don't put $role = 'admin' it says there is a missing break;

<?php

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

?>