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 trialFabian Pijpers
Courses Plus Student 41,372 PointsI 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
<?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
11,110 Pointsif 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;
}
Fabian Pijpers
Courses Plus Student 41,372 PointsThanks this you have great answers.
Patrik Horváth
11,110 Pointsi 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 :)
Fabian Pijpers
Courses Plus Student 41,372 PointsThe only thing for me now is to get myself together past my anxiety and i will be starting to push first apps.
Fabian Pijpers
Courses Plus Student 41,372 PointsFear and a good plan and i will be out there making my own stuff happen!!!
Fabian Pijpers
Courses Plus Student 41,372 PointsFabian Pijpers
Courses Plus Student 41,372 PointsSorry, but i made some mistakes regarding the switch statement. And the last break statement was too much too.
Fabian Pijpers
Courses Plus Student 41,372 PointsFabian Pijpers
Courses Plus Student 41,372 PointsI 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