PHP strcmp


strcmp() function compares two strings. It first compares the first character, if the ASCII value of the 1st character is smaller than the 2nd, returns -1, , else return 1, and regardless of the string length. If the two strings are the same, return 0. strcmp() is case sensitive.

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