Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing strings

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

like image 828
ritu Avatar asked Apr 21 '26 10:04

ritu


2 Answers

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.
like image 87
Delan Azabani Avatar answered Apr 24 '26 00:04

Delan Azabani


The function strcmp returns

< 0 if str1 is less than str2; 
> 0 if str1 is greater than str2, and 
0 if they are equal. 
like image 28
codaddict Avatar answered Apr 23 '26 22:04

codaddict



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!