问题
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?
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";
}
I've tried different values for $myInt
and different contents for $myArray
and it always prints this:
Int less than array
回答1:
Comparing an array to anything else result in array is always greater :
see php doc - section Comparison with Various Types
来源:https://stackoverflow.com/questions/39287235/interesting-behavior-comparing-integer-with-array-in-php