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

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,885 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
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Amber Stevens
Treehouse Project Reviewer

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

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

?>