Interesting behavior comparing integer with array in PHP

…衆ロ難τιáo~ 提交于 2019-12-12 04:32:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!