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

Please help on the Switch Statement Quiz

Please help,

I keep getting "i do not see the default message correctly" and i think my code is right

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

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

Your code isn't right. Try this and it should work.

I hope this can help.

<?php

#Make sure to define the variable $role first by assigning a value to it first
$role = 'someRoleFromSomewhere';

//Available roles: admin, editor, author, subscriber
if (empty($role)) {
    $role = 'subscriber';
}

//change to switch statement
switch ($role) {
  case 'roleName':
    echo 'Role mesasge here';
    break;
  case ($role !== 'roleNameNotFound'):
    echo 'roleNameNotFound is not found';
    break;
  default:
    echo 'default message';
    break;
}

3 Answers

Hi Yoga,

Your problem is with the switch statement switch ($role != 'admin') {

You only need to do switch ($role)

You put in the variable you want to check and then its value will be compared against each case statement.

You're almost correct. I tested a few things with this challenge and found that if I failed it, 90% of the time it was not because my code did not produce the correct output, but because of grammar and spelling.

One time I added an additional "e" to "edit" and it failed, even though the code was correct. Another time I left the period (full stop) off of the end of one of the echo statements and it also failed.

This challenge is very very fussy about the spelling and punctuation so if you're struggling, firstly check each echo line word by word for the spelling. Then check your periods, commas and speech marks. If all of that is spot on then it is likely to be a code issue. Chances are though, if you've checked it and know the code is ok, you should pass.

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

Hi everyone, thanks for the answer.

I haven't be able to test it out again, been busy with work this week. I'll be sure to test your code out and will give feedback after it.

Thanks. Yoga