dean7 Posted July 17, 2009 Share Posted July 17, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/166372-user-levels/ Share on other sites More sharing options...
Alex Posted July 17, 2009 Share Posted July 17, 2009 if ($logged['username'] && $logged['level'] == 'mod' || $logged['level'] == 'admin' || $logged['level'] == 'owner') Quote Link to comment https://forums.phpfreaks.com/topic/166372-user-levels/#findComment-877375 Share on other sites More sharing options...
GingerRobot Posted July 17, 2009 Share Posted July 17, 2009 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')) Quote Link to comment https://forums.phpfreaks.com/topic/166372-user-levels/#findComment-877409 Share on other sites More sharing options...
DeanWhitehouse Posted July 17, 2009 Share Posted July 17, 2009 $levels = array("mod","admin","owner"); if($logged['username'] && in_array($levels,$logged['level'])) { A shorter version Quote Link to comment https://forums.phpfreaks.com/topic/166372-user-levels/#findComment-877423 Share on other sites More sharing options...
Stephen Posted July 18, 2009 Share Posted July 18, 2009 Not to mention you forgot the semicolon after: echo 'You are not meant to be here.' Quote Link to comment https://forums.phpfreaks.com/topic/166372-user-levels/#findComment-877427 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.