How would you like to be able to record hundreds, or thousands of different yes/no values in a single number? This is just one of the many applications where base_convert can come in handy.

Remember basic math classes in high school, where you learned about binary numbers, and hexadecimal, and other number systems that use a different base than our standard base 10?  Remember reading about computers, and how they are ultimately just binary number crunchers? Well let’s put that fund of knowledge to work in our PHP programs.

Suppose you have an on-line questionnaire, with hundreds of questions to which people can reply with a simple yes/no response. You need to record the tens of thousands of results for later analysis. You can do it with a simple integer. In fact, if you have fewer than 64,000 questions (and let’s hope for your subject’s sake that there aren’t that many!) you can use a small integer (SMALLINT) in your mysql database.

How? Well, simply convert each answer into zeros and ones, to create a binary number. Let’s say for example the first five questions were answered ‘yes’ and the next five ‘no’ by a particular subject. You save that as 1111100000. Now use base_convert(1111100000,2,10) and PHP converts the base two number into base 10 for easy storage in your database. Retrieve the number from the database, and convert it back into base two using base_convert(#,10,2). Simple and quick, it saves tons of storage space.