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 trialJohn Vicencio
6,413 PointsSWITCH IF STATEMENT TO SWITCH STATEMENT
Question: In the code provided, there is an if statement that displays a message IF the user role is not equal to "admin". We want to display separate messages based on the role of the logged in user. First we're going to add a message for our admin and keep the current message as the default. Change the if statement to a switch statement, keeping the current message as the default. Add a check for the role of "admin" and display the following message: As an admin, you can add, edit, or delete any post.
My solution: it was if($role != 'admin') { echo "You do not have access to this page. Please contact your administrator."; }
to
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."; }
Advise.
<?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.";
}
7 Answers
Jennifer Nordell
Treehouse TeacherHi there! You're doing great! And this boils down to the challenge being ultra picky. That echo about the admin is supposed to have a full stop at the end. Take a look:
echo "As an admin, you can add, edit, or delete any post.";
If I copy/paste your code and add the period/full stop, it passes! Good luck!
vinayak sapkal
9,802 Pointsfirst task answer <?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;
default: echo "You do not have access to this page. Please contact your administrator."; }
Raman Matthew
13,286 Pointsif (! 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 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; } }
omer cohen
2,549 Pointswhy isnt it working ? i have en error massage :"syntax error, unexpected end of file in switch.php on line 16 " (line 16 is "?>")
if ($role != 'admin') { 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."; }
?>
mkooij
8,886 PointsWorst question in my 6000+ Treehouse points I have encountered!
Please fix the the question to be more clear, also fix the code recognition it is to picky.
The working answer.. just because this question is unclear and code recognition is to picky.
<?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; default: echo "You do not have access to this page. Please contact your administrator."; break; } ?>
Arthur Rich
6,401 Pointsswitch ($role) { default: echo 'You do not have access to this page. Please contact your administrator.'; break; case 'admin': echo 'As an admin, you can add, edit, or delete any post.'; break;}
Massimo Ceron
22,372 PointsSorry, but I have a problem for this question......if I copy/paste the new code echo "As an admin, you can add, edit, or delete any post."; and change if statement with switch statement at line 8, compare this message: Bummer! syntax error, unexpected 'echo' (T_ECHO), expecting case (T_CASE) or default (T_DEFAULT) or '}' in switch.php on line 9 help me!
John Vicencio
6,413 PointsJohn Vicencio
6,413 PointsAh yes, thanks Jennifer!
Paul Heneghan
14,380 PointsPaul Heneghan
14,380 PointsAgreed -- I originally left out the comma following "add, edit" and kept getting the same error msg "did you leave out a break?"
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherTo anyone reading this answer please note that what I'm referring to as "your code" is not the code marked in the black box in the OP, but rather the switch statement in the text above that.