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

SWITCH IF STATEMENT TO SWITCH STATEMENT

Source: https://teamtreehouse.com/library/php-arrays-and-control-structures/php-conditionals/switch-statements

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.

switch.php
<?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
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi 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! :sparkles:

Ah yes, thanks Jennifer!

Paul Heneghan
Paul Heneghan
14,380 Points

Agreed -- I originally left out the comma following "add, edit" and kept getting the same error msg "did you leave out a break?"

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

To 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.

vinayak sapkal
vinayak sapkal
9,802 Points

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

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; default: echo "You do not have access to this page. Please contact your administrator."; break; } }

why 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
mkooij
8,886 Points

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


switch ($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;}

Sorry, 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!