Checking if a number is either X, Y, or Z
by
05 Mar 2002
Frequently, you want parts of your hack to only be accessible by moderators and admins.
To do so, you use this if-block:
PHP Code:
if ($bbuserinfo['usergroupid'] == 5 or $bbuserinfo['usergroupid'] == 6 or $bbuserinfo['usergroupid'] == 7) {
/* Do stuff */
} else {
/* Access denied */
}
And if you want to give another usergroup access for that action, you need to add even another OR to that statement!
A much more elegant solution would be to use in_array():
PHP Code:
if (in_array($bbuserinfo['usergroupid'], array(5, 6, 7))) {
You can even declare an array, let's say $allowedgroups, that can be used throughout the code:
PHP Code:
/* Earlier... */
$allowedgroups = array(5, 6, 7);
/* Somewhere in your code */
if (in_array($bbuserinfo['usergroupid'], $allowedgroups)) {
/* Do stuff */
} else {
/* Access denied */
}
This is also not only useable when checking usergroups! Let's say you have feature you only want visible on forums 4, 6, 18 and 65:
PHP Code:
/* Earlier... */
$specialforums = array(5, 6, 7);
/* Somewhere in your code */
if (in_array($forumid, $specialforums)) {
/* Make use of the feature */
}
Not really a tip nor a trick, but I'm a bit bored so...
|