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 trialChristian Agosto
1,242 PointsPHP test on Switch Statement.
It keeps telling me that I'm missing a "break;" somewhere in the 'editor' case, but I've checked again and again and I really can't find the problem.
<?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 admin, you can add, edit, or delete any post.";
break;
case 'author':
echo "As an author, you can add, edit, or delete your own post.";
break;
case 'editor':
echo "As an editor, you can add or edit any post, and delete your own posts.";
break;
default:
echo "You do not have access to this page. Please contact your administrator.";
break;
}
}
?>
Christian Agosto
1,242 Points"Add a check for the role of "editor" and display the following message: As an editor, you can add or edit any post, and delete your own posts. Add a check for the role of "author" and display the following message: As an author, you can add, edit, or delete your own post."
That's it, it just asks me to include those 2 cases with their own specific messages. And then it keeps asking me to check for a "break;" that I missed.
Thanks for the prompt response Alex!
Alex Arzamendi
10,042 PointsI see, which section is this in so I can see it and find the solution to it? And no problem! I love helping with code whenever I can!
Christian Agosto
1,242 PointsIts the las chapter of PHP Arrays and Control Structures; a test with 2 tasks. Here's the link: https://teamtreehouse.com/library/php-arrays-and-control-structures/php-conditionals/switch-statements
Christian Agosto
1,242 PointsIt worked!! Thank you so much for making this clearer to me.
2 Answers
Alex Arzamendi
10,042 PointsFound it, and I see where the error was. It was the same logic error I pointed out earlier. Basically, you are going to completely remove the if-else statement and work by making it go through the switch:
<?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;
// your other checks will go in here
default:
echo "You do not have access to this page. Please contact your administrator.";
break;
}
Try It, the only thing that is missing is the case check for the author and the editor. Make sure everything is spelled out correctly.
Now, the reason as to why this did not work properly on the first try.
If you structure your code using the if-else as before, the check for admin will ONLY run what is inside your if statement and will never work with the else because $role is essentially not admin. The switch case was initially meant to just redo everything that your would normally do inside the if-else statement with a set of conditions. If you wanted to, you could very well structure everything to work only with the if-else. but it would be more code to write which is why the switch was introduced for this case.
Jacob Mishkin
23,118 PointsJust a couple of pointers But you are really close. First the question asks you to replace the if statement with a switch, and keep the default echo string. You have the echo string in your switch default object, but its still in the if statement as well. So start by removing the if and else. We are replacing the if conditional with a switch for the var $role. After that as far as I can tell everything looks good.
Alex Arzamendi
10,042 PointsAlex Arzamendi
10,042 PointsHi Chris!
It is working on my side when I run it as a script inside my server. But is it exactly what the code is asking you to do? The logic would never run on the switch statement because subscriber is not a case for the switch, also the else will never run because the if will continue to check just for what is inside the if statement. Which problem set is this? I have not taken the whole php class but can definitely help you out given more context.