PHP Bitvalue/Flags?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Vortex
Forum Newbie
Posts: 1
Joined: Mon Jun 12, 2006 3:39 am

PHP Bitvalue/Flags?

Post by Vortex »

mh.. hello everyone,

i was wondering if i can assign some sort of bitvalues to a var too, like its possible to do so in C. The way it works are the numbers of like 1, 2, 4, 8 all stand for something, so i can check on that, but also combine such values to make a flag for that var.. like a sort of options. Confusing?

1 Option 1
2 Option 2
4 Option 3
8 Option 4
16 Option 5
Now someone chooses opt 1 and 3 so his var gets saved as 1 + 4 = 5. Later i must be able to check on that, which options are enabled? So it can read the 5 into the only possible solution of 4 and 1. In C++ you can do it simply by something like this..

typedef enum {
F_SLEEP = 0,
F_PROTECT,
F_PUNISH,
F_FREEZE,
F_EMPOWER,
F_TERMINATOR,
F_SILENCE,
F_CHEAT,
F_CHATPROTECT,

F_EMPOWERSB,
F_NOSUICIDE,
F_NOTEAMSWITCH,
F_NOTOGGLE,
F_NORENAME
} modFlags_t;

Then the statement to check :: if (flags & (1 << F_SLEEP)) ;


Whats the PHP way to go for something like this?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

Look at the bitwise operators section of the online PHP manual. There are many user examples at the bottom of the page. The combination of define() and bitwise operators should do what you want.
(#10850)
Post Reply