php教程

字符串与数字比较类型转换

我的站长站 2025-02-18 人阅读

都转换成字符串类型

$str1 = '0fff';
$str2 = 0;
 
if ((string)$str1 == (string)$str2) {
    echo 'yes';
} else {
    echo 'no';
}
 
// 预期结果是 'no'
// 实际结果是 'no'

PHP自带的字符串比较函数

$str1 = '0fff';
$str2 = 0;
 
// strcmp() 二进制安全字符串比较,区分大小写
// 如果 $str1 小于 $str2 返回 -1;
// 如果 $str1 大于 $str2 返回 1;
// 如果两者相等,返回 0。
if (strcmp($str1, $str2) === 0) {
    echo 'yes';
} else {
    echo 'no';
}
 
// 预期结果是 'no'
// 实际结果是 'no'


最新更新