Helpful Information
 
 
Category: vBulletin Article Depository
Checking if a number is either X, Y, or Z

Frequently, you want parts of your hack to only be accessible by moderators and admins.

To do so, you use this if-block:
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():
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:
/* 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:
/* 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... :)

Very nice firefly. This is very helpful... I really didnt like arrays but I am starting to see the usfulness in them. :D

Great info Firefly...very much appreciated, thank you! Can someone post something about joins? I am trying to something using joins and it just isn't working right.

Post in the PHP forum about it and I'll see what I can do. :)

Firefly, that's good, but for performance you should really use a switch as this would prevent the test of an array, convert to an array and loop through array stuff that happens partly implicitly...

try something like...


switch ($bbuserinfo['usergroupid']) {
case 5:
case 6:
case 7:
// Do stuff for usergroups 5 or 6 or 7
break;
case 2:
// Do stuff for usergroup 2
break;
default:
// Do stuff for everyone else
}


Just an improvement. Doesn't change functionality... but under strain this should shine.










privacy (GDPR)