php - 比较给出不同结果的数组

标签 php arrays comparison

我有以下 PHP 代码:

$a = "5";
$b = array("position"=>1);
var_dump("5" <= $b);
var_dump("5" <= "Array");
var_dump("Z" <= $b);
var_dump("Z" <= "Array");

结果是:

boolean true
boolean true
boolean true
boolean false

我们知道数组被转换为字符串array。转换期间实际发生了什么?为什么以下情况的结果不同?

var_dump("Z" <= $b); // (in this case array convert to string "array")
var_dump("Z" <= "Array");  // (in this case string "Array" )

但结果不同。为什么?

最佳答案

如手册states :

Comparison with Various Types

array | anything | array is always greater

当两个操作数都是字符串时,应用字符串比较规则。

所以无论第一个操作数是什么,< array()永远正确

关于php - 比较给出不同结果的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20070522/

相关文章:

php - CodeIgniter 使用数据库中的后数组值验证表单

php - 将数组拆分为特定数量的 block

javascript - 循环两个对象数组,在 Id 上连接并合并它们

python - 递增的 float 彼此不相等

f# - 订购扩展的受歧视工会

php - 使用 PHP DOM 按类获取 div

php - 使用 md5 散列密码

javascript - 当扩展数组映射未定义时

f# - 如何比较深度嵌套的歧视工会?

php - 使用 DateInterval 计算两个日期之间的月份,而不在一年内换行