php中的符号和十进制数为真 [英] symbol and decimal number true in php

查看:37
本文介绍了php中的符号和十进制数为真的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的脚本

$number = range(0, 9);

当我有这种情况时

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

}else "false";

和输出:

true

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

and my question is why the symbols is the same whit any number in array $number?? I want symbols just symbols not number.

我想要这样的例子

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

}else "false";

输出:

false

推荐答案

来自 in_array() 的文档:

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

If the third parameter strict is set to TRUE then the in_array() function will also check the types of the needle in the haystack.

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

In PHP, casting any string that doesn't begin with a number evaluates to to 0. The 0 exists in your array, so in_array() returns true. If you don't want this to happen, set the third parameter for in_array() to true, so it performs a strong comparison (equivalent to ===) and consider the types, too.

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

输出:

false

这篇关于php中的符号和十进制数为真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆