Say you have multiple checkboxes in your html page. At some point you will need to add a few more checkboxes for some configuration options. Or they could be a whole bunch of boolean flags in your program that you need to keep track of. Usually you would have to create numerous separate variables for each boolean flag, an array for all of them, or if they're stored in a database - you'd have to add more columns to store each "bit". Why bother altering the table structure (and doing any kind of special memory allocation) just for some puny bits? We can combine them all into a single integer, store them all in one place, and have ability to easily read/alter them, as well as add more boolean flags, or reassign the meanings of the old ones.
For example, we might have a list of songs on a page, and we might want to give a user an ability to check which songs are to be played and which to be omitted. Say, we have 7 songs. Each one can be included (1 - checked) or excluded (0 - unchecked). Let's represent it in a single boolean number. If all songs were unchecked, it would look like this: 0000000. If we decide to check the second, third, fifth, and seventh songs, it would look like this: 0110101. Each bit stands for a song. Now if we drop the leading zero from the resulting number - it would look like 110101. Quick check on a conversion calculator would tell us that in a decimal representation the number is simply... 53. We can see now that number 53 means that we have 2nd, 3rd, 5th, and 7th songs checked. Ok, this is understandable, but how to extract these values and work with them?
Simple. Each song has its own number. When 7th song is checked, it looks like this 0000001. When 6th song is checked, it looks like this: 0000010, etc. 10 converted to decimals is 2. 1 is 1. Just to remind you how the boolean system works - 100 is 4, 1000 is 8, etc. Let's assign these values in PHP define(), to read them easier.
<?php
define ( SONG1, 1 ); // 0000001
define ( SONG2, 2 ); // 0000010
define ( SONG3, 4 ); // 0000100
define ( SONG4, 8 ); // 0001000
define ( SONG5, 16 ); // 0010000
define ( SONG6, 32 ); // 0100000
define ( SONG7, 64 ); // 1000000
?>
Now, let's say user has selected the checkboxes like I mentioned above: 0110101. This number is actually 53. The first thing to discuss is - how to use PHP and the information that we have to extract each individual checkbox from this single number. There is really nothing to it!
Let's simply write:
<?php
$checkboxes = 53; // we could get this value from the database, or wherever we store our checkboxes
if ( $checkboxes & SONG1 ) { // this means song 1 is checked }
if ( $checkboxes & SONG4 ) { // this means song 4 is checked }
?>
That's it! The "$checkboxes & SONG4" is actually "53 & 8". It returns either "8" if the song is checked, or zero if it's not. In our case (0110101) the Song 4 is not checked - so we'll get a zero for it.
It's also very easy to change each individual flags - set or remove the checks from the songs. Say we want to check and remove the Song 4. Here's what we do:
<?php
// adding Song 4
if ( !($checkboxes & SONG4) ) { // always have to check if it's already flagged
$checkboxes += SONG4; // flagging it
}
// removing Song 4
if ( $checkboxes & SONG4 ) {
$checkboxes -= SONG4;
}
?>
As simple as that.
Now it's obvious that if we need SONG8, SONG9, etc - we do not have to change the structure of anything - we can just define them as 128, 256, and keep on tracking them in that same integer.
There are many cases where this practice is beneficial. If you have tons of checkboxes, whose amount and meaning may vary, and you can't afford having so many columns in the database, and changing them constantly - this is the way to go. If you are afraid to mess with live-database too often - this way is better as well. This is simply a more agile and flexible approach to storing boolean values. Have fun!
Good, but...
Never use + and - to add/remove items. Use bit-wise operators instead.
<?php
// adding Song 4
$checkboxes |= SONG4; // flagging it
// removing Song 4
$checkboxes &= ~SONG4;
?>
Thanks - I thought something
Thanks - I thought something was awkward with that conditional approach, but couldn't put my finger on it.