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;
    }
?>

when i use this code it will show me a date are identical even the value ex when i compare the value 12 jan to 12 march it will show me value are identical but the value is differ

0

4 Answers 4

6

You have used the wrong return values.

  • -1 and 1 mean that the strings are not identical (less than and greater than, respectively).
  • 0 means that the strings are identical.
Sign up to request clarification or add additional context in comments.

Comments

1

The function strcmp returns

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

Comments

1

strcmp "returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal". You're printing "date are identical" when $result == 1, which is incorrect.

Comments

1

strcmp returns:

  • a negative number if string1 is less than string2
  • zero if the two strings are equal
  • a positive number of string1 is greater than string2

It is incorrect to assume that strcmp will return only -1, 0, or 1.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.