【问题标题】:symbol and decimal number true in phpphp中的符号和十进制数为真
【发布时间】:2014-05-20 00:04:20
【问题描述】:

我有这样的脚本

$number = range(0, 9);

当我有这种情况时

if (in_array('@', $number) === true) {
    echo "true";

}else "false";

和输出:

true

我的问题是为什么符号与数组 $number 中的任何数字相同? 我想要符号只是符号而不是数字。

我想要这样的例子

if (in_array('@', $number) === true) {
    echo "true";

}else "false";

输出:

false

【问题讨论】:

    标签: php arrays conditional-statements


    【解决方案1】:

    来自documentation for in_array():

    如果第三个参数strict设置为TRUE,那么in_array()函数也会检查大海捞针的类型。

    在 PHP 中,将任何不以数字开头的字符串转换为 0。0 存在于您的数组中,因此 in_array() 返回 true。如果您不希望发生这种情况,请将 in_array() 的第三个参数设置为 true,以便执行强比较(相当于 ===)并同时考虑类型。

    if (in_array('@', $number, true) === true) {
        echo "true";
    }
    else { 
        echo "false";
    }
    

    输出:

    false
    

    【讨论】:

    • 谢谢您的回答。如果我有这样的条件,因为在数组中 @number 有 0 。以及何时与任何符号进行比较。
    猜你喜欢
    • 2018-08-04
    • 2019-02-18
    • 1970-01-01
    • 2013-05-28
    • 1970-01-01
    • 1970-01-01
    • 2014-12-04
    • 2012-10-25
    • 1970-01-01
    相关资源
    最近更新 更多