PHP的is_bool如何区分INT和BOOL之间的区别?

时间:2016-11-28 22:53:51

标签: php pdo

我问,因为我在向PDO传递布尔值时遇到问题。

这个函数给我真/假:

?

我正在使用如下所示的行将数据绑定到函数内的参数:

public function getBoolVal($var){
    if (!is_string($var)) return (bool) $var;
    switch (strtolower($var)) {
        case '1':
        case 'true':
        case 'on':
        case 'yes':
        case 'y':
            return true;
        default:
            return false;
    }
}

并且$this->db->bind('active', (bool)$this->getBoolVal($pa['active'])); 函数内部是:

bind

当绑定值时,当输入值foreach ($this->parameters as $param => $value) { $type = PDO::PARAM_STR; switch ($value[1]) { case is_int($value[1]): $type = PDO::PARAM_INT; break; case is_bool($value[1]): $type = PDO::PARAM_BOOL; break; case is_null($value[1]): $type = PDO::PARAM_NULL; break; } // Add type when binding the values to the column $this->sQuery->bindValue($value[0], $value[1], $type); } 为1时,我将PDO::PARAM_BOOL作为类型,但如果输入为0,我总是得到$pa['active'] ...

所以给定1或0,地球上的PDO::PARAM_INT如何知道我想要什么?或者我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您可以将代码更改为

if ($value[1] === true or $value[1] === false) {
    $type = PDO::PARAM_BOOL;
}
else {
    switch ($value[1]) {
        case is_int($value[1]):
            $type = PDO::PARAM_INT;
            break;
        case is_float($value[1]):
            $value[1] = str_replace(',', '.', $value[1]);//already string, pdo does not support float values
            break;
        case is_null($value[1]):
            $type = PDO::PARAM_NULL;
            break;
    }
}