This is slightly OT for SO, because I'm not trying to solve a specific problem, instead just to understand how something might be implemented. But I am after code, so let's see how it goes...
Let's say we had a checkbox for each day of the week, and we decided to store any combination of those checkboxes as a single number, such that:
0 = no days
1 = Monday
2 = Tuesday
4 = Wednesday
8 = Thursday
16 = Friday
32 = Saturday
64 = Sunday
127 = everyday
How might one go about implementing that logic in PHP so that if I submitted say, "13", PHP would know to tick only the Monday, Wednesday and Thursday checkboxes?
As to avoid code structure duplication (lots of similar
if
clauses) and introducing extra "magic" numbers (2
,7
), as shown Sammitch's working suggestions, I'd prefer the following.Bitwise
AND
s:edit
You can avoid the
if
s with something like:There's more than these two ways to skin this particular cat, but the 'best' way depends on what your result/output needs to be.