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

What is wrong with my challenge code?

I have the following code for the first step of the switch case challenge. I can't proceed because it says I don't have a case for 'admin.' But I do have a case for admin in there... did I do something wrong?

On a general note, it would be really helpful if the challenges showed the correct answer, rather than just saying 'you're wrong' and not letting you proceed.

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

//change to switch statement switch ($role) { case $role == 'admin': echo 'As an admin, you can add, edit, or delete any post.'; break; case $role != 'admin': echo 'You do not have access to this page. Please contact your administrator.'; break; default:
echo "You do not have access to this page. Please contact your administrator."; }

1 Answer

Matthew Lang
Matthew Lang
13,483 Points

You have a misunderstanding of how a switch() statement works. Let me revise it for you.

$array = 1;
switch($array) {
  case 1:
    // do something here if $array == 1
    break;
  case 2:
    // do something here if $array == 2
    break;
  case x: // you get the idea. Whatever. x = whatever you're checking $array is equal to.
    break;
}

Think of it as inspecting a variable, and writing a case for each value it could potentially be. For example, array is 1, so in that case do this, and so on and so forth.

I hope this clears it up for you.