2011-03-02 186 views
2
<?php 
    $string1 = "12 jan"; 
    $string2 = "12 aprail, 13 march"; 
    $result = strcmp($string1, $string2); 

    switch ($result) { 
     case -1: print "date are not identical"; break; 
     case 0: print "date1"; break; 
     case 1: print "date are identical"; break; 
    } 
?> 

當我使用這個代碼,它會告訴我的日期是相同的連值ex 當我比較值的1月12日至3月12日 它會告訴我的價值是相同的 但值是不同的比較字符串

回答

6

您使用了錯誤的返回值。

  • -11表示字符串不相同(分別小於和大於)。
  • 0表示字符串是相同的。
1

功能strcmp返回

< 0 if str1 is less than str2; 
> 0 if str1 is greater than str2, and 
0 if they are equal. 
1

的strcmp 「如果str1小於STR2返回< 0;> 0如果str1大於str2,和0,如果它們相等」。當$ result == 1打印「日期完全相同」時,這是不正確的。

1

的strcmp返回:

  • 如果字符串2是一個負數少 比字符串2
  • 零如果兩個字符串 相等
  • 字符串1的正數大於字符串2

假設strcmp將僅返回-1,0或1是不正確的。