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

Andrew Lenti
seal-mask
.a{fill-rule:evenodd;}techdegree
Andrew Lenti
Front End Web Development Techdegree Student 7,193 Points

Switch statement

I am trying to answer objective number 2 in the PHP conditionals module which is asking to allow a user with a role of 'admin' to enter while all the other users (e.g. subscriber), will not have permission. With the code below I get an an error message stating that the default output is not correct.

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

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

What's your question?? Please don't just copy-paste the challenge without even bothering to ask a question or explain what your problem is......

Daniel Gauthier the question was modified with more info since my comment. Initially it only had the exercise title and nothing else. Sometimes I find it amusing how people expect help or solutions without even actually asking something. Like "yeah I need help, thnx".

I'm here to help, and I enjoy doing so. I just get frustrated when I see low-quality questions, and they frequent around here. That last post of yours is neat - I'll copy that for future use if you don't mind.

/end rant.

Oh, and have an upvote.

Daniel Gauthier
Daniel Gauthier
15,000 Points

Hey Christian,

I completely understand, and I was just lightening the mood a little :D

I figured it had been modified, since I've seen the kinds of questions you're talking about before and even digging I can't always find where the student is stuck.

Considering your profile picture, I figured you'd appreciate the attempt.

In the end, Andrew was helped out, and I think we're both happy about that.

Thanks for the upvote!

Daniel Gauthier
Daniel Gauthier
15,000 Points

Hey again Christian,

Also, definitely feel free to use the posting instructions. I wrote it up after being here a few months and realizing that the method to post code wasn't made very clear.

Have a great day :D

Thanks. btw, that short url is broken. just fyi

Daniel Gauthier
Daniel Gauthier
15,000 Points

Hey Christian,

Thanks for pointing that out :D

I created a placeholder, so the "Bummer" page won't show up when someone tries to click through the link.

With that, you've just become the most helpful person in this thread.

Thanks again!

4 Answers

Daniel Gauthier
Daniel Gauthier
15,000 Points

Hey Andrew,

Looking over your code, it seems you were stuck on the first step of the challenge, and not the second.

That said, the issue is that you forgot the comma after the word edit in your admin case. So, if this was the issue, then it was just a simple typo and you were really close :D

This is your code, placed back into the code challenge, formatted and with that comma added and it passes:

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

//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;

}
}
?>

If something was missing from your post and you really were stuck on the second half of the challenge, this code will pass:

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

    case "editor":
      echo "As an editor, you can add or edit any post, and delete your own posts.";
      break;

    case "author":
      echo "As an author, you can add, edit, or delete your own post.";
      break;

    default:
      echo "You do not have access to this page. Please contact your administrator.";
      break;

}

The main goal is to make sure you know how the switch statement works, and to try to highlight the importance of the break statement when working with them.

Good luck with the course!

Daniel Gauthier
Daniel Gauthier
15,000 Points

Also, going to throw this in here in case you ever need to post code to the forums in the future.

How to Post Code on the Forums

There are two ways to share your code on the forums here, excluding using external tools from outside of Treehouse.

Method One

The first method is to use a series of three ` (backticks, located at the top left of the keyboard) without any spaces on one line, paste all of your code starting on the second line, then closing the code block with a second series of three backticks. Take a peek at the link for the "Markdown Cheatsheet" located above the "Post Answer" button anytime you're about to post a comment or answer. Using this method, the code will look like this:

```css
(code here)
```

Method 2

The second method is a little more convoluted, but it lets us look at your entire project and make our own copy to try fixing issues. I'll order the steps since it can be a little confusing the first time around:

  1. Open the workspace you're having trouble with.

  2. On the menu bar located to the left of the "Preview Workspace" button is the "Snapshot Workspace" (camera icon), click that.

  3. A popout will appear with another button that says "Take Snapshot", click that.

  4. It should create a new snapshot, which will appear beneath the "Take Snapshot" button in the popout. Click the snapshot you want to share.

  5. The snapshot should open a new browser tab. Highlight the url in the snapshot's browser tab (it should look like this: https://w.trhou.se/5q36uv0z0f ).

  6. Create your forum post and paste the snapshot's url into the post. Other Treehouse users will be able to open the snapshot, then 'fork' a new copy to check out your code.

Keep in mind that you can only ever have five snapshots, but you can delete them by hovering over the snapshot located below the "Take Snapshot" button in the popout and clicking the trash bin icon.

Good luck!

Andrew Lenti
seal-mask
.a{fill-rule:evenodd;}techdegree
Andrew Lenti
Front End Web Development Techdegree Student 7,193 Points

Gentlemen, thanks alot for the guidance, mentorship, and spiritual healing :)

I am new to this so excuse me with the syntax issues.

As a feedback, one thing that I do find a bit annoying is when I wake up at 430 to study code and get blocked on one of your challenges and have to stop studying altogether to wait for someone to answer my question. Your response time is certinainly lighting fast (no doubt about it) but it does stop the flow a bit.

Daniel Gauthier
Daniel Gauthier
15,000 Points

Hey again Andrew,

We're students just like you on this forum :D You may spot the ultra rare teacher post, but even people with mod tags are just other students here.

I can completely understand the frustration though, since it's one of the quirks of the system here. I always found it strange how the staff were almost never around on the forums, but the system does seem to work for the most part.

It would definitely be nice to have a way to get into contact with the staff beyond "Hey follow me on Twitter" though, especially since this is a paid platform.

As always, good luck with the course!

+1 to that. Andrew, use google! It might sound dumb or cheeky, but I'm my self a full-time full-stack webdeveloper and I google my issues all the time. 50% of the time, or more, I find answers, ideas or guidance on there.

www.stackoverflow.com is also a great site for QA - they have an enormous bank of solved problems, and there's virtually no beginner-questions that haven't been asked there before. Just be wary if you ever decide to ask a question yourself, you have to elaborate on your question thoroughly, show with code what you've tried and why other answers you may have found on the site didn't solve your problem. Otherwise you'll get downvoted into oblivion. :D

Good luck!

Andrew Lenti
seal-mask
.a{fill-rule:evenodd;}techdegree
Andrew Lenti
Front End Web Development Techdegree Student 7,193 Points

Thanks for that Christian. And yes, stackoverflow is quite the resource as well. For your information, I am trying my best to become a full-stack webdeveloper and am trying to learn PHP, CSS, and jQuery so I can stop paying heaps of cash each month to two programmers which are currently building a software of mine which I built in Visual Basic and of which had a great success story as a prototype. After a year, I am doing OK with the HTML/CSS part, am starting to become OK with jQuery, but the PHP and linking to MySQL part, i am still having major difficulty gettting my head around. Do you have any words of wisdom on how to become an effective object oriented programmer in minimal time ??