According to the documentation,bit(1)
是数字字段类型。那么为什么使用PHP 5.6我收到0x00和0x01字符?
数据库:
CREATE TABLE IF NOT EXISTS `demo` (
`id` bigint(20) unsigned NOT NULL,
`test` bit(1) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
INSERT INTO `demo` (`id`, `test`) VALUES (1, b'0'), (2, b'1');
测试代码:
$q = mysql_query("select * from demo");
while($row = mysql_fetch_array($q)) {
if ($row['test'] === chr(0x00)) {
echo '0x00' . PHP_EOL;
} elseif ($row['test'] === chr(0x01)) {
echo '0x01' . PHP_EOL;
}
if ((bool) $row['test'] === true) {
echo 'true' . PHP_EOL;
} else {
echo 'false' . PHP_EOL;
}
}
结果:
0x00
true
0x01
true
我正在使用PHP 5.6.9和MySQL 5.5.44。
答案 0 :(得分:2)
chr(0x00)是php中的奇数边缘情况:
chr(0x00) == true -> true
chr(0x00) === true -> false
chr(0x00) == false -> false
chr(0x00) === false -> false
empty(chr(0x00)) -> false
strlen(chr(0x00)) -> 1
(int)(chr(0x00)) -> 0
(bool)(chr(0x00)) -> true
var_dump(chr(0x00)) -> string(1) ""
它看起来应该是假的,但由于它不是一个空字符串而不是整数0 /布尔值假,你得到这个"意外"行为。
类似于0x01:
chr(0x01) == true -> true
chr(0x01) === true -> false
chr(0x01) == false -> false
chr(0x01) === false -> false
empty(chr(0x01)) -> false
strlen(chr(0x01)) -> 1
(int)(chr(0x01)) -> 0
(bool)(chr(0x01)) -> true
var_dump(chr(0x01)) -> string(1) ""