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

Visar Kosova
Visar Kosova
2,816 Points

How do I check if the username is SET?? Or am i suppose to check if username is actually set as 'sketchings' ??

it would be nice to have a feature where I can see the correct answers when we as users are unable to answer the questions.

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

//add conditional statement

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

1 Answer

Shayne Laufenberg
Shayne Laufenberg
4,213 Points

Currently you are checking if the $username variable is equal to a string of '' even though this looks right its actually not the same as checking if the variable is set. You are basically saying if the user's username is equal to '' and they are admin to display the result. Which means the condition would never be true in this case and result would never be displayed use the isset function this will check if your variable is set and is not null. You can check out the solution below to see how it is used. If you have any other questions feel free to ask.

Solution:

<?php

  // Shayne Laufenberg //
  // Php Arrays and Control Stuctures -- Operators -- //

  // Declare Username //
  $username = 'sketchings';

  // Declare User Role //
  $role = 'editor';

  // Check if username is set and user is admin //
  if(isset($username) && $role != "admin"){

    // Output Result //
    echo "You do not have access to this page. Please contact your administrator.";

  }

?>