douhuanglou1445 2014-11-13 11:18
浏览 89
已采纳

filter_var boolean返回false

This is weird

var_dump(filter_var(true, FILTER_VALIDATE_BOOLEAN));
var_dump(filter_var(false, FILTER_VALIDATE_BOOLEAN));

bool(true)
bool(false)

What an I missing here? Surely they should both be true? If not how can I validate a false boolean?

Edit: To clarify. I need to validate a false boolean. To ensure it's not a string, int, float or anything else. e.g.

$var = false; //true
$var = true; //true
$var = 'foo'; //false
$var = 1; //false

Perhaps I asked my question incorrectly or I've evolved it too much ad I should ask another question.

Solution I went for was:

$result = filter_var($bool, FILTER_VALIDATE_BOOLEAN) && ($bool===true||$bool===false);
  • 写回答

1条回答 默认 最新

  • dongli2000 2014-11-13 11:22
    关注

    From the manual:-

    If FILTER_NULL_ON_FAILURE is set, FALSE is returned only for "0", "false", "off", "no", and "", and NULL is returned for all non-boolean values.

    Set this and if null is returned, you don't have a bool (or boolish) value.

    For example:-

    var_dump(filter_var(true, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE));
    var_dump(filter_var(false, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE));
    var_dump(filter_var(2, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE));
    

    See it working

    However, it may be that is_bool() is more appropriate here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题