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

I can not seem to get the switch statement to work?

Although I have tried several times to adjust the statement to work i can not seem to get it to work

switch.php
<?php
//Available roles: admin, editor, author, subscriber
if (!isset($role)) {
    $role = 'subscriber';
}

//change to switch statement
switch ($role)  {
  case 'subscriber':
    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;
  default:
    echo "You do not have acces to this page. Please contact your administrator.";
    break;
}

4 Answers

Patrik Horváth
Patrik Horváth
11,110 Points

if you want use variable you have to DECLARE it

try code below and you get no result because this variable never exists and never will exist

var_dubm($role); 

hovever you can use it on request / session / .... but never forget START SESSION !

if (!isset($_SESSION["$role"]) {
 your code;
}

Sorry, but i made some mistakes regarding the switch statement. And the last break statement was too much too.

I the case of a real world development enviroment, you are right this is awesome when you need to debug your code and thourough. As is the case when you are going for staging or live testing before officially going live or pushing an app to the appstore.!!!

Thanks

Thanks this you have great answers.

Patrik Horváth
Patrik Horváth
11,110 Points

i m glad to help :) mark my answere as answere :) fell free to ask in future :)

also never give ROLE or anything with $_GET because everyone can edit $_GET variable and change it :)

The only thing for me now is to get myself together past my anxiety and i will be starting to push first apps.

Fear and a good plan and i will be out there making my own stuff happen!!!