我有一个函数,它接受一个数字并返回一个与日期相对应的数组(该数字将在每周的每一天进行位掩码)。但是,数组会返回某些值的所有日期,而另一个值则返回一个空数组。 以下是功能
function get_days($days) {
$days_arr = array();
echo "days: " . decbin($days) . " - type: " . gettype($days) . "<br/>";
echo "type1: " . gettype($days & 0x01) . " - type2: " . gettype(0x01) . "<br/>";
echo "days & 0x01 = " . dechex($days & 0x01) . " = " . ($days & 0x01 == 0x01) . "<br/>";
echo "days & 0x02 = " . dechex($days & 0x02) . " = " . ($days & 0x02 == 0x02) . "<br/>";
echo "days & 0x04 = " . dechex($days & 0x04) . " = " . ($days & 0x04 == 0x04) . "<br/>";
echo "days & 0x08 = " . dechex($days & 0x08) . " = " . ($days & 0x08 == 0x08) . "<br/>";
echo "days & 0x10 = " . dechex($days & 0x10) . " = " . ($days & 0x10 == 0x10) . "<br/>";
if($days & 0x01 == 0x01)
$days_arr[] = 'M';
if($days & 0x02 == 0x02)
$days_arr[] = 'T';
if($days & 0x04 == 0x04)
$days_arr[] = 'W';
if($days & 0x08 == 0x08)
$days_arr[] = 'H';
if($days & 0x10 == 0x10)
$days_arr[] = 'F';
return $days_arr;
}
以下是回声的结果
days: 10101 - type: integer
type1: integer - type2: integer
days & 0x01 = 1 = 1
days & 0x02 = 0 = 1
days & 0x04 = 4 = 1
days & 0x08 = 0 = 1
days & 0x10 = 10 = 1
days: 1010 - type: integer
type1: integer - type2: integer
days & 0x01 = 0 = 0
days & 0x02 = 2 = 0
days & 0x04 = 0 = 0
days & 0x08 = 8 = 0
days & 0x10 = 0 = 0
我似乎无法找出问题背后的原因,我认为这应该有用。
答案 0 :(得分:2)
这是运营商优先问题。参见:
http://php.net/manual/en/language.operators.precedence.php
因此==
高于&
。你不应该这样做:
$days & 0x02 == 0x02
可是:
($days & 0x02) == 0x02