在PHP中比较整数和数组的有趣行为 [英] Interesting behavior comparing integer with array in PHP
问题描述
在PHP中,我必须比较一个整数变量是否小于数组的值,但是我在代码中出错并偶然发现了意外行为。
In PHP I had to compare if an integer variable was less than a value of an array, but I had an error in the code and I found by chance an unexpected behavior.
我正在将整数与数组本身进行比较,并且返回真。你知道为什么吗?
I was comparing the integer with the array itself and it was returning true. Do you know why?
我一直在StackOverflow和php.net中搜索此内容(PHP类型比较),但我没有找到具体的答案。
I've been searching about this in StackOverflow and php.net (PHP types comparisons) and I didn't found a specific answer.
这里有一些代码可以对此进行测试。
Here you have a little code to test this.
<?php
$myArray = array();
$myInt = 1;
if($myInt < $myArray){
echo "Int less than array\n";
}
if($myInt == $myArray){
echo "Int equal to array\n";
}
if($myInt > $myArray){
echo "Int greater than array\n";
}
我为 $ myInt
和 $ myArray
的不同内容,它总是打印以下内容:
I've tried different values for $myInt
and different contents for $myArray
and it always prints this:
Int less than array
推荐答案
数组对其他任何数组的结果总是大于数组:
Comparing an array to anything else result in array is always greater :
这篇关于在PHP中比较整数和数组的有趣行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!