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 trialdouglas gray
888 PointsChanging set of if's to single switch statement
the original looks like this:
<?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."; }
My attempt is attached. I also tried leaving the negation operator bc it says not to change the code above the switch statement (i assume it means inside the "switch()", and making the message to administrator the default instead. That obviously fails to satisfy another instruction. Please help.
<?php
switch (isset($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.";
}
?>
1 Answer
Jason Anello
Courses Plus Student 94,610 PointsHi Douglas,
You have to keep the first if statement there. You're only changing the second if to a switch statement.
You're checking the value of $role to see if it matches any of the case statements.
You only need switch ($role)
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsYou had a break after your first echo statement in your original code but you're missing that here. And did you keep the first
if
condition? because you're not showing it here.Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsSorry, I missed in your first comment that you had the switch wrong too.
It should be
switch ($role)
as I had in my answer and you also need thebreak
like you had in your original code.Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsAlso, this post will show you how to properly format your code: https://teamtreehouse.com/forum/posting-code-to-the-forum
douglas gray
888 Pointsdouglas gray
888 PointsThank you kindly for your continued input, Jason. I omitted the first if statement to make the comment shorter and I did type switch (role). That's why i deleted my first comment. I realized my mistake. I am still getting a syntax error though- "unexpected echo"- while using this code:
The error is in line ten at the second echo
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsI think part of the problem you're having is that when you go to make the suggested changes, you're also changing things that you previously had correct.
You want to keep the case statement then way you had in your original code.
case 'admin':
Your most recent attempt will pass if you make that change and keep everything else the same.
douglas gray
888 Pointsdouglas gray
888 PointsThank you, Jason. You're right, it passed.