<?PHP $diff=strcmp("a","A"); echo "$diff"; //1 $diff=strcmp("a","b"); echo "$diff"; //-1 $diff=strcmp("a","a"); echo "$diff"; //0 $str1="PHP"; $str2="Javascript"; $diff=strcmp($str1,$str2); echo "$diff"; //1 ?>
If the two strings has the same 1st character, then compare the 2nd character and so on.
<?PHP $diff=strcmp("jhb","jha"); echo "$diff"; //1 $diff=strcmp("jha","jhb"); echo "$diff"; //-1 ?>
If the 1st string and the 2nd string has the same first character and followed characters, and the 1st string is shorter, return the length difference in negative; else if the 2nd string is shorter, return the length difference in positive.
<?PHP $diff=strcmp("j","jhg"); echo "$diff"; //-2 $diff=strcmp("jhg","j"); echo "$diff"; //2 $diff=strcmp("jhg","jh"); echo "$diff"; //1 $diff=strcmp("jh","jhabcde"); echo "$diff"; //-5 ?>
<?PHP $diff=strncmp("jhb","jha",1); echo "$diff"; //0 $diff=strncmp("jhb","jha",2); echo "$diff"; //0 $diff=strncmp("jhb","jha",3); echo "$diff"; //1 $diff=strncmp("jha","jhb",3); echo "$diff"; //-1 ?>
<?PHP $str1 = "perschon"; $str2 = "Perschon"; echo strcmp($str1,$str2); //1 echo strcasecmp($str1,$str2); //0 ?>