Skip to content

Instantly share code, notes, and snippets.

Created August 2, 2010 20:38
Show Gist options
  • Save anonymous/505272 to your computer and use it in GitHub Desktop.
Save anonymous/505272 to your computer and use it in GitHub Desktop.
<pre><?php
// Gisthub Git to support StackOverflow question http://stackoverflow.com/questions/3391193/notification-preferences-predicament/3391599#3391599
switch($_GET['x']) { // Was POST, but when playing around with the code GET makes more sense.
case 'Email': $x = pow(2,0); echo "X Email"; break; // 1
case 'Sms': $x = pow(2,1); echo "X Sms"; break; // 2
case 'Both': $x = pow(2,0) + pow(2,1); echo "X Both"; break;// 3, since 1 + 2 = 3.
default: $x = 0;
}
switch($_GET['y']) { // Was POST, but when playing around with the code GET makes more sense.
case 'Email': $y = pow(2,2); echo "Y Email"; break; // 4
case 'Sms': $y = pow(2,3); echo "Y SMS"; break; // 8
case 'Both': $y = pow(2,2) + pow(2,3); echo "Y Both"; break; // 12, since 4 + 8 = 12.
default: $y = 0;
}
$z = $x + $y;
var_dump($x);
var_dump($y);
var_dump($z);
// Now, we can detect Email or SMS using the bitwise values.
if($z & 1) echo "X Email";
if($z & 2) echo "X SMS";
if($z & 4) echo "Y Email";
if($z & 8) echo "Y SMS";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment