In: ,
On: 2006 / 05 / 12
Shorter URL for this post: http://ozh.in/d1

In my neverending amateur journey through the wonders of coding, here is something I've learnt in PHP today : how to conveniently store a set of flags into one value, with bitmask comparisons made easy thanks to PHP bitwise operators. Sounds greek to you ? Read on, it's this kind of things that are piece of cake to use while they still make you look brilliant when you explain it to your wife.

Example of use

Say you're making a WordPress theme and want to give users control over whether a particular block should be in left sidebar or right sidebar (if displayed at all). To do so you've set up this kind of form :

Sort of display preferences

First : DEFINE

First, define a set of constants

  1. <?php
  2. define('BLOCK_ARCHIVES' , 1);
  3. define('BLOCK_CALENDAR' , BLOCK_ARCHIVES << 1);
  4. define('BLOCK_LASTCOM'  , BLOCK_ARCHIVES << 2);
  5. define('BLOCK_QUOTE'    , BLOCK_ARCHIVES << 3);
  6. define('BLOCK_CCNUMBER' , BLOCK_ARCHIVES << 4);
  7. define('BLOCK_BABE' , BLOCK_ARCHIVES << 5);
  8. define('BLOCK_ADMIN'    , BLOCK_ARCHIVES << 6);
  9. ?>

All these constants now have the following values : 1, 2, 4, 8, 16, 32, 64 (powers of 2).

Now use

Once the user submits the form in the above screenshot, you would create and store two values, something like :

  1. <?php
  2. $display_left = BLOCK_ARCHIVES | BLOCK_QUOTE | BLOCK_ADMIN;
  3. $display_right = BLOCK_CALENDAR | BLOCK_LASTCOM | BLOCK_BABE;
  4. ?>

The beauty of it is that, no matter how many options you have, you just need to store 2 integers to remember them all.

Testing whether a block should display left, right or nowhere is now easy :

  1. <?php
  2. if (constant($block) === ($display_left & constant($block))) {
  3.        echo "$block goes left";
  4. } elseif (constant($block) === ($display_right & constant($block))) {
  5.        echo "$block goes right";
  6. } else {
  7.        echo "$block goes /dev/null";
  8. }
  9. ?>

Testing the above code with $block = 'BLOCK_ARCHIVES' would for example echo BLOCK_ARCHIVES goes left. Easy, isn't it ?

Other ideas of use

Really, this trick is a useful one. You could imagine storing a lot of things this way :

  • display options for blocks in sidebars as explained above
  • mail options for a forum in which you want the user to be able to specify if particular notices and messages are delivered via regular mail, internal messaging a la phpBB or just ignored ($send_email = ALERT | CHANGEPWD; $send_internal = USER_MSG | THREADWATCH_REPLY;)
  • display advanced options or not depending on user profile ($access_full = ADMIN | MODERATOR | BIGBOSS; $access_restrict = GUEST | USER;)
  • etc…

Notes

I believe the number of options you can specify is therefore depending on the plaftorm you're on (32 bits or 64 bits), which makes anyway a reasonable variety of user defined settings.

Depending on the number of options you're having, you will be storing in a database small or great integers (up to 2^8-1 = 255 if you have 8 options, up to 16777216 if you have 24 options, etc…). You can therefore fine-tune your database scheme down to the smallest integer type needed for the option value field, i.e for MySQL TINYINT, SMALLINT, INT, etc…

I have to thank Polo, my new PHP mentor, for sharing another bit of his knowledge with me :)

Shorter URL

Want to share or tweet this post? Please use this short URL: http://ozh.in/d1

Metastuff

This entry "PHP Bitwise Operators Example of Use" was posted on 12/05/2006 at 8:57 pm and is tagged with ,
Watch this discussion : Comments RSS 2.0.

One Reply

  1. Dx says:

    That's the sometimes-known binary thing that no one uses, but it's useful… Great post, you explained this easy

    No comments yet. You could get "First Post!" which would bring you fame, luxury cars and chicks.

    Where are them?

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
Gravatars: Curious about the little images next to each commenter's name ? Go to Gravatar and sign for a free account
Spam: Various spam plugins may be activated. I'll put pins in a Voodoo doll if you spam me.

Read more ?

 « PAPA