A PHP Code Clarity Tip

This is a quick tip on making your PHP code easier to read (both for MODX logoothers, 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:

[code language=”php”]
if ($userGroup == ‘EditorUsers’ || $userGroup == ‘AdminUsers’ || $userGroup == ‘ContentEditors’ || $userGroup == ‘SubAdminUsers’ || $userGroup == ‘Publishers’) {
/* Do something */
} else {
/* Do something else */
}
[/code]

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:

[code language=”php”]
switch($userGroup) {
case ‘EditorUsers’:
case ‘adminUsers’:
case ‘ContentEditors’:
case ‘SubAdminUsers’:
case ‘Publishers’:
/* Do something */
break;
default:
/* Do something else */
break;
}
[/code]

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:

[code language=”php”]
if ($userGroup == ‘EditorUsers’ || $userGroup == ‘AdminUsers’) {
/* Action 1 */
} elseif ($userGroup == ‘ContentEditors’ || $userGroup == ‘SubAdminUsers’) {
/* Action 2 */
} elseif ($userGroup == ‘Publishers’) {
/* Action 3 */
} else {
/* Action 4 */
}
[/code]

Here’s the “switch” version:

[code language=”php”]
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;
}
[/code]

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.

 


For more information on how to use MODX to create a web site, see my web site Bob’s Guides, or better yet, buy my book: MODX: The Official Guide.

Looking for quality MODX Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , | Posted under MODX, MODX | RSS 2.0

Author Spotlight

Bob Ray

Bob Ray is the author of MODX: The Official Guide and over 30 MODX add-on components. He hosts Bob's Guides, a source of valuable information for MODX users, and has been very active in the MODX Forums with over 19,000 posts.

Leave a Reply

Your email address will not be published. Required fields are marked *