Jump to content

User levels


dean7

Recommended Posts

Hi on my website i have got things like a mod panel witch mods admins and owners can veiw.

 

<?php
if ($logged[username] && $logged[level] ==  mod || admin || owner)
{
echo " ";
}else{
echo 'You are not meant to be here.'
}
?>

 

But its got an error

Parse error: syntax error, unexpected T_STRING in /home/a7502957/public_html/main.php on line 5

anyone know why?

 

Thanks :)

Link to comment
Share on other sites

Strings should be quoted, otherwise PHP treats them as constants. Depending on your PHP setup, you might find that things still 'work' however as PHP can 'guess' that an undefined constant is supposed to be a string. Relying on that is bad practice because of the aforementioned dependency on the setup.

 

Also, as AlexWD's solution shows, you cannot simply list the possible values a variable could be. If you have the following expression:

 

if(somecondition == somevalue || someothervalue)

 

Then the evaluation of that will first test if somecondition == somevalue is true. If it is, the the whole logical expression is true and hence someothervalue is not tested for truth owing to short-cut evaluation. If, however, somecondition == somevalue is false we then test if someothervalue is 'true' -- i.e. it could hold the boolean value TRUE, or any non-zero integer value. If that's still not clear, the above expression is logically equivalent to:

 

if(someothervalue || somecondition == somevalue)

 

There is, however, probably a problem with AlexWD's solution. Logical AND takes precedence over logical OR. Hence, the if statement evaluates to true if either $logged['level'] has the value 'admin' or 'owner', regardless of the value of $logged['username']. Of course, if that's what you meant that that's fine. Otherwise, you need some parenthesis:

 

if ($logged['username'] && ($logged['level'] ==  'mod' || $logged['level'] == 'admin' || $logged['level'] == 'owner'))

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.