note 38019 added to language.operators.bitwise
| From: | krang at krang dot org dot uk | Date: | Thu, 04 Dec 2003 19:30:08 +0000 |
| Subject: | note 38019 added to language.operators.bitwise | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-61418@lists.php.net to get a copy of this message | ||
Hopefully this may help someone understand the fun of Bitwise Operators....
The purpose of this function is to return a value from the GPC (Get, Post and Cookie) and do some
basic formatting to it depending on the $VALIDATION value:
<?PHP
function RETURN_SUBMITTED_VALUE ($VARIABLE, $METHOD, $VALIDATION) {
//-------------------------------
// Get the value from the
// relevant submit method...
if ($METHOD == 'POST') {
if (!isset($_POST[$VARIABLE])) $_POST[$VARIABLE] = '';
$VALUE = $_POST[$VARIABLE];
} elseif ($METHOD == 'COOKIE') {
if (!isset($_COOKIE[$VARIABLE])) $_COOKIE[$VARIABLE] = '';
$VALUE = $_COOKIE[$VARIABLE];
} else {
if (!isset($_GET[$VARIABLE])) $_GET[$VARIABLE] = '';
$VALUE = $_GET[$VARIABLE];
}
//-------------------------------
// If necessary strip the slashes.
// the "GPC" means - GET, POST
// COOKIE.
if (ini_get ('magic_quotes_gpc') == true) {
$VALUE = stripslashes($VALUE);
}
//-------------------------------
// Now for the different types
// of VALIDATION's
if (($VALIDATION & 8) == 8) {
$VALUE = (int)$VALUE;
}
if (($VALIDATION & 4) == 4) {
$VALUE = strtolower($VALUE);
}
if (($VALIDATION & 2) == 2) {
$VALUE = strip_tags($VALUE);
}
if (($VALIDATION & 1) == 1) {
$VALUE = trim($VALUE);
}
//-------------------------------
// Finally return the value
return $VALUE;
}
echo RETURN_SUBMITTED_VALUE ('ID', 'GET', 8) . '<br />';
// Convert to an Integer
echo RETURN_SUBMITTED_VALUE ('NAME', 'GET', 3) . '<br />';
// Trim Whitespace and Strip HTML tags
echo RETURN_SUBMITTED_VALUE ('GENDER', 'GET', 6) . '<br />';
// Strip HTML tags and convert to lower case
//-----------------------------------------------
// If this script was loaded under the URL
// index.php?ID=19a&NAME=Krang&GENDER=MalE
// it would print
//
// 19
// Krang
// male
//-----------------------------------------------
?>
For those that dont understand binary, the numbers you see are not random, they double each time
(1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024...) which allows you to mix and match the different
function, eg...
1 + 2 = 3 (Trim Whitespace + Strip HTML)
2 + 4 = 6 (Strip HTML + Convert to lower case)
----
Manual Page -- http://www.php.net/manual/en/language.operators.bitwise.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+38019
Delete -- http://master.php.net/manage/user-notes.php?action=delete+38019&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+38019&report=yes
Search -- http://master.php.net/manage/user-notes.php