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 trialKumbirai Ruth Huni
4,209 Pointsswitch statements
this is not displaying the correct output. Where could i be getting it wrong
<?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.";
} else
{switch ($role){
case 'admin':
echo "as an $role you can add, edit, or delete any post.";
break;
default:
echo "You do not have access to this page. Please contact your administrator.";
break;
}
}
?>
1 Answer
andren
28,558 PointsThere are two issues:
You are meant to replace the
if
statement with aswitch
statement, not add an else statement with aswitch
statement inside.challenges are very picky about printing stuff, if the thing you print doesn't match the example string to the dot your code will often be marked as wrong. The issue in your case is that you need to capitalize the A in the beginning of the admin messages string and also add a comma to the sentence that is missing after the "$role" part.
If you fix those two issues like this:
<?php
//Available roles: admin, editor, author, subscriber
if (!isset($role)) {
$role = 'subscriber';
}
//change to switch statement
switch ($role){
case 'admin':
echo "As an $role, you can add, edit, or delete any post.";
break;
default:
echo "You do not have access to this page. Please contact your administrator.";
break;
}
Then your code will pass the task.
Kumbirai Ruth Huni
4,209 PointsKumbirai Ruth Huni
4,209 Points<?php //Available roles: admin, editor, author, subscriber if (!isset($role)) { $role = 'author'; }
//change to switch statement switch ($role){ case 'admin': echo "As an $role, you can add, edit, or delete any post."; break; default: echo "You do not have access to this page. Please contact your administrator."; break; } ?>
did the above but it is still not passing
andren
28,558 Pointsandren
28,558 PointsIt should pass the first task, I verified that before posting my example, and the code you posted works too. This challenge has two tasks, are you sure the challenge did not move from task 1 to task 2? If if didn't then it likely a bug, the code checker is not always the most reliable.
You can restart the challenge and just paste in the code I posted above, that should definitively work.