简体   繁体   English

PHP两个值的比较?

[英]PHP comparison of two values?

While comparing the values in the below array 比较下面数组中的值

array
  0 => 
    array
      0 => string '09:30' (length=5)
      1 => string '11:00' (length=7)
  1 => 
    array
      0 => string '11:01' (length=5)
      1 => string '18:00' (length=7)
  2 => 
    array
      0 => string '12:05' (length=5)
      1 => string '14:00' (length=7)
  3 => 
    array
      0 => string '14:00' (length=5)
      1 => string '20:20' (length=7)

If I am using if($a > $b) it is even giving true for equal values ($a = $b) // this is not comparision , while if($a - $b > 0) is giving accurate result. 如果我使用的是if($a > $b)甚至等于相等的值($a = $b) // this is not comparision ,而if($a - $b > 0)则给出准确的结果。

Why so ? 为什么这样 ?

EDIT: The code part where I am using cmp. 编辑:我正在使用cmp的代码部分。

This woks fine 这锅好

for($i=0; $i < $fr -1 ; $i++)
{
    if( $dtime[$i][1] - $dtime[$i+1][0] > 0 )
    {
        echo 'It is clashing';
        break;
    }
}

This does not works fine 这行不通

for($i=0; $i < $fr -1 ; $i++)
{
    if( $dtime[$i][1] > $dtime[$i+1][0] )
    {
        echo 'It is clashing';
        break;
    }
}

Though the way you're doing comparisons seems odd, I think you have a data cleaning problem. 尽管您进行比较的方式似乎很奇怪,但我认为您存在数据清理问题。 In the top part of your question it appears you're doing a var_dump() . 在问题的顶部,您似乎正在执行var_dump() Notice how '09:30' (length=5) is correct but '11:00' (length=7) is wrong because the length should be 5 also. 注意'09:30' (length=5)是正确的,但是'11:00' (length=7)是错误的,因为长度也应为5。 There are some additional hidden characters (maybe \\n or \\r) at the beginning or end of the data. 在数据的开头或结尾还有一些其他隐藏字符(可能是\\ n或\\ r)。

To fix, try running $value = trim($value) on each of your array elements before comparing. 要进行修复,请在比较之前尝试对每个数组元素运行$value = trim($value)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM