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.
   int strcmp(str string 1, str string 2)

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

strncmp() compares two strings with specified length.

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

strcasecmp() is similar to strcmp(), but case insensitive.

<?PHP
	 $str1 = "perschon";
	 $str2 = "Perschon";
	 echo strcmp($str1,$str2);  //1
	 echo strcasecmp($str1,$str2);  //0
?>




:: PHP Tutorials Home ::
PHP String Functions
 • concatenation • echo
 • ereg • ereg_replace
 • explode • htmlspecialchars
 • preg_match • preg_replace
 • preg_split • print,sprintf
 • str_replace • strcmp
 • strpos • strrev
 • strrpos • strtr
 • substr • substr_replace
PHP Array Functions
 • array_diff • array_flip
 • array_intersect • array_key_exists
 • array_keys • array_merge
 • array_pop • array_push
 • array_rand • array_search
 • array_splice • array_unshift
 • array_values • asort & arsort
 • count • in_array
 • ksort • shuffle
 • sort
PHP Data Types
 • array • associative array
 • date & time • number
 • class, object • regular expression
 • string • variables
PHP Loop & Conditions
 • continue & break • for loop
 • foreach • if else
 • not equal • while
PHP File System Functions
 • copy • delete, unlink
 • dirname • download url
 • file_exists • is_file
 • mkdir • read file
 • scandir • write file
PHP Popular Topics
 • ajax • clone
 • comments • constants
 • cookie • database
 • defined • die
 • form validation • gd, draw images
 • global variables • header url
 • heredoc • mail
 • pass by reference • print
 • regular expression • sessions
 • threads • xml parse
PHP Math Functions
 • abs • cos
 • exp • floor & ceil
 • fmod • log
 • max & min • pow
 • round • sin
 • sqrt • tan
convert.idontcarewhatyouthink.net © 2021  Terms of Use | Home