This is a quick tip on making your PHP code easier to read (both for others, and for you). Complicated if statements can be fairly obscure, so we'll look at an alternative that I think is a little easier to read.
The Problem
Consider this line of code that tests a user's primary user group:
if ($userGroup == 'EditorUsers' || $userGroup == 'AdminUsers' || $userGroup == 'ContentEditors' || $userGroup == 'SubAdminUsers' || $userGroup == 'Publishers') {
/* Do something */
} else {
/* Do something else */
}
The code above is difficult to read, especially if you're not that used to reading PHP code. It's also easy to make a fatal typing error (like using = instead of == or | instead of ||) when entering or modifying complex if statements like the one above.
An Alternative
Here's another way to do the same thing using PHP's switch statement:
switch($userGroup) {
case 'EditorUsers':
case 'adminUsers':
case 'ContentEditors':
case 'SubAdminUsers':
case 'Publishers':
/* Do something */
break;
default:
/* Do something else */
break;
}
I think the second version is a little easier to understand and much easier to modify without making a mistake. Of course, this technique only works if the variable being tested is the same for each condition, you use "OR" for each test, and you are always testing it for equality.
Another Example
Here's another use case involving multiple if and elseif statements:
if ($userGroup == 'EditorUsers' || $userGroup == 'AdminUsers') {
/* Action 1 */
} elseif ($userGroup == 'ContentEditors' || $userGroup == 'SubAdminUsers') {
/* Action 2 */
} elseif ($userGroup == 'Publishers') {
/* Action 3 */
} else {
/* Action 4 */
}
Here's the "switch" version:
switch($userGroup) {
case 'EditorUsers':
case 'AdminUsers':
/* Action 1 */
break;
case 'ContentEditors':
case 'SubAdminUsers':
/* Action 2 */
break;
case 'Publishers':
/* Action 3 */
break;
default:
/* Action 4 */
break;
}
Again, I think the switch version is much easier to follow.
Performance
If you've been reading my Blog articles, you know that I'm a big fan of faster code. I benchmarked the two methods in the first example with 1,000 iterations for each one. The difference in speed depends somewhat on the value of the $userGroup variable and the position of that variable in the list. In my tests, the if statement was slightly faster in most cases, but the largest difference I was able to produce was less then one ten-thousandth of a second. Remember — that's for 1,000 iterations. That means (if my math is correct) that in a single pass, the speed difference is less than one ten-millionth of a second! That's a performance hit I can live with.
Wrapping Up
Admittedly, the technique described here won't work for all situations, but it's a nice option to have on hand for those cases where it does work.

Comments (2)
MellFotoStudio — Oct 05, 2015 at 09:07 AM
Alternatively, if all you wish to test is if the user belongs to one of x groups (or a variable is set to one of X values), you can use this:
$groupsCheck = array();
$groupsCheck['EditorUsers'] = 1;
$groupsCheck['adminUsers'] = 1;
$groupsCheck['ContentEditors'] = 1
$groupsCheck['SubAdminUsers'] = 1;
$groupsCheck['Publishers'] = 1;
if ( isset($groupsCheck[$userGroup]) ){
// some code for success
} else {
// some code for failure
}
The value for the array entries can be anything, really - I've opted for '1' because it's simple. The reason to use array keys instead of values is because it's much, much quicker to check for array key existence than it is to walk the hash table and find a match (ie. in_array() incurs a rather heavy performance penalty). Complexity O(n) for the entire code, and it should - IIRC - be a little quicker than the switch() statement (but I can be wrong on that).
You can, of course, break that array up into smaller groups and use if / else if to check for various actions.
If you wish to make it a little more readable, you can always define it as a list and do a little bit of magick to make it easier, like so:
// this is a bit more readable in human standards...
$groupsCheck = "EditorUsers, adminUsers, ContentEditors, SubAdminUsers, Publishers";
$groupsCheck = explode(", ", $groupsCheck);
foreach($groupsCheck as $id => $val){
$groupsCheck[$val] = 1; // set a new entry as key being the value, equal 1
unset($groupsCheck[$id]); // remove the old entry in the array
}
if ( isset($groupsCheck[$userGroup]) ){
// some code for success
} else {
// some code for failure
}
... though I'm not sure if in_array() doesn't beat this one in terms of efficiency. You can't have both, I suppose.
Bob Ray — Oct 09, 2015 at 11:56 PM
Using isset() is definitely much faster, but if you need to "or" things, the switch is much easier, imo.
Please login to comment.