Re: Boolean type forced on string assignment inside if statement
| From: | tamouse mailing lists | Date: | Fri, 04 Jan 2013 02:17:52 +0000 |
| Subject: | Re: Boolean type forced on string assignment inside if statement | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 12 13 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-320029@lists.php.net to get a copy of this message | ||
Bit operators & and | are NOT and should NEVER be confused with
Logical operators && and ||:
<?php
/**
* Bit operators in PHP
*/
$format = "Decimal: %2d Binary: %4b\n";
$a = 4;
$b = 6;
echo "Variable \$a:\n";
printf($format, $a, $a);
echo "Variable \$b:\n";
printf($format, $b, $b);
$c = $a | $b;
echo "Result of OR bit operator\n";
printf($format, $c, $c);
$c = $a & $b;
echo "Result of AND bit operator\n";
printf($format, $c, $c);
?>
OUTPUT:
-------
Variable $a:
Decimal: 4 Binary: 100
Variable $b:
Decimal: 6 Binary: 110
Result of OR bit operator
Decimal: 6 Binary: 110
Result of AND bit operator
Decimal: 4 Binary: 100
Bit operators are not comparing values, they're COMBINING values.