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
https://forums.phpfreaks.com/topic/166372-user-levels/
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
https://forums.phpfreaks.com/topic/166372-user-levels/#findComment-877409
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.