Back to vBulletin 3 Articles

Checking if a number is either X, Y, or Z
by Chen 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'] == or $bbuserinfo['usergroupid'] == 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(567))) { 
You can even declare an array, let's say $allowedgroups, that can be used throughout the code:
PHP Code:
/* Earlier... */
$allowedgroups = array(567);

/* 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(567);

/* 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...

vblts.ru supports vBulletin®, 2022-2024