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 Operators

I have no idea what to do. What does it mean when it says. "1. That a $username is set."?

I need help. I have no idea what to do!

index.php
<?php
$username = 'sketchings';
//Available roles: author, editor, admin
$role = 'editor';

//add conditional statement
echo "You do not have access to this page. Please contact your administratior.";
if ($role != 'admin' && $username == "sketchings") {
  echo "Not admin.";
  echo "Username is set.";
}

5 Answers

nico dev
nico dev
20,364 Points

Hi Dennis Boccardi ,

Hey, good effort, you're almost there!

Just a couple of things could bring you back on track:

  1. Remember that before the conditional you still don't know if the person is an admin or not, therefore you wouldn't want to be expelled from your own site if you're the admin! :) For that reason, you should check the conditions before you give a message about it to the user, right? And only when the condition is checked you would be able to display the message with echo. In other words you want the if() conditional to check the condition (is the user an admin? is the username set?) and, if so, display the message {echo this message (just the message provided, you don't need to change it)}.

  2. You're not asked to check if the username was 'sketchings', but to check if any username was set. So how would the username be if it was not set? Think of an input field if it helps. It would be empty, right? And how would an empty string look like? Then if you already have that idea, now 'translate it into PHP', like if (role is not an admin, and username is not empty).

Does that help somehow?

A little bit. Could you attach a piece of code as an example? I'm not trying to cheat and just copy, but I do better when I can see the actual code. If you could, that would be great. Thank you.

nico dev
nico dev
20,364 Points

Sure Dennis,

I am not sure if I understand correctly what you ask, but I guess you refer bits of code to explain what I mean, right? Hope I got you right, but otherwise please let me know.

  1. If you do:
<?php

// any code here

echo "You do not have access to this page. Please contact your administratior.";
if ($role != 'admin' && $username == "sketchings") {
  echo "Not admin.";
  echo "Username is set.";
}

// any code here

?>

... you're already printing the message "you don't have access" even before you check if the person is an admin or not. If in turn you print (or echo) that, as a result of checking the condition, then you're doing it if what you're checking for (the condition/s) are true. That is:

<?php

// any code here

if (conditionA && conditionB) {
  echo "You do not have access to this page. Please contact your administratior."; // I know, administrator is misspelled but I only copied it :)
}

// any code here

?>

Think about it, you only print that if you're sure the conditions you want are occurring (or not occurring).

  1. Here:
<?php

// any code here

if ($role != 'admin' && $username == "sketchings") {
  // any code here
}

// any code here

?>

... you're checking if a) the role is not an admin (and that's correct!) and b) if you the username is "sketchings". However, what the challenge is asking from you is that you make sure the username was set. In other words, that it's not possible for anyone to create a user without a username. That would not be good for many reasons (but that's off-topic, sorry :) ). The thing is you don't have a way to know what username will be chosen, so you cannot check if the username was sketchings, Nico or Dennis, etc. What you can check, though, is if the value of username is not empty. Because if you did not type a username then that variable ($username) should simply be an empty string, right?

So this would be the value of $username if the username was not set:

<?php
$username == ''; // an empty string, no username set.
?>

So, what you have to check in the conditions is that the $username is not an empty string (in other words, it was set to some string, like 'sketchings' or anything else) for the code inside the curly to be executed.

Does that clarify it a little more to you? It's OK to come back and follow up if something does not look clear!

Thank you. That helps so much! Now I can finally continue learning PHP. Thank you.

I did everything that you said, and I may sound stupid for getting this wrong, but it still keeps giving me an error. "I do not see the correct output." I don't know how to attach my code so I'll just write it out.

if ($username != "" && $role != "admin") { echo "You do not have access to this page. Please contact your administrator."; }

nico dev
nico dev
20,364 Points

Hey Dennis, glad it helped! :)

First of all, don't feel stupid for going through the same process we all go. We all hit walls. It's just normal part of the process. What those moments require is perseverance, and it seems you have that, so that's great!

Secondly, you're right. I guess the misspelling has some bad effect after all. I tried it like you and it doesn't work, but if I change administrator to administratior it does. (I guess maybe we're not supposed to touch the string.)

It would be a great idea if you report this to support. They will be able to correct it for others not to struggle with it (i.e.: that would help others).

Nevermind. I restarted the challenge and it worked thank you so much for the help. If you ever need help, which I doubt it, especially from me, I owe you a favor.

nico dev
nico dev
20,364 Points

Just pay it forward ;)