PHP strncasecmp() Function Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report The strncasecmp() function is a built-in function in PHP and is used to compare two given strings. It is case-insensitive. This function is similar to strcasecmp(), the only difference is the provision to specify the number of characters to be used from each string for the comparison. Syntax: strncasecmp($string1, $string2, $length) Parameters: This function accepts two parameters as shown in the above syntax and are described below: $string1, $string2: These parameters specifies the strings to be compared. $length: It specifies the number of characters from each string to be used in the comparison. This parameter is mandatory Return Value: This function returns an integer based on the conditions as described below: strncasecmp() returns 0 - if the two strings are equal. strncasecmp() returns <0 - if string1 is less than string2 strncasecmp() returns >0 - if string1 is greater than string2 Examples: Input : string1 = "Hello", string2 = "hEllo", length = 6 Output : 0 Input : string1 = "Geeks", string2 = "Gfg", length = 3 Output : -1 Input : string1 = "Nerd", string2 = "Geeks", length = 4 Output : 7 Below programs illustrate the strncasecmp() function in PHP: Program 1: When the two strings are identical: php <?php $str1 = "Geeks for Geeks "; $str2 = "Geeks for Geeks "; // Both the strings are equal $test=strncasecmp($str1, $str2, 16 ); echo "$test"; ?> Output : 0 Program 2 : When first string greater than the second string: php <?php // Input strings $str1 = "Geeks for Geeks "; $str2 = "Geeks for "; $test=strncasecmp($str1, $str2, 16 ); // In this case the second string is smaller echo "$test"; ?> Output: 6 Program 3: First string is smaller than the second string: php <?php // Input Strings $str1 = "Geeks for "; $str2 = "Geeks for Geeks "; $test=strncasecmp($str1, $str2, 16 ); // In this case the first string is smaller echo "$test"; ?> Output: -6 Program 4: This program illustrates the case-insensitivity of the function: php <?php // Input Strings $str1 = "GEEKS FOR GEEKS "; $str2 = "Geeks for Geeks "; // Both the strings are equal $test=strncasecmp($str1, $str2, 16 ); echo "$test"; ?> Output: 0 Program 5: Two strings are of equal length but contain a different character. In such a case the difference between ASCII value of the two characters is displayed. The function returns a positive value if the character in string1 has a greater ASCII value and negative if the character in string2 has a greater ASCII value. php <?php // Input Strings $str1 = "Good"; $str2 = "Goon"; $test1 = strncasecmp($str1, $str2, 4 ); // Second string has a character // with higher ASCII value echo "$test1"; echo "\n"; $test2 = strncasecmp($str2, $str1, 4 ); // First string has a character // with higher ASCII value echo "$test2"; ?> Output: -10 10 Reference: https://www.php.net/manual/en/function.strncasecmp.php Comment More infoAdvertise with us Next Article PHP addslashes() Function R RICHIK BHATTACHARJEE Follow Improve Article Tags : Misc Web Technologies PHP PHP-string Practice Tags : Misc Similar Reads PHP addcslashes() Function The addcslashes() function is a built-in function in PHP. The addcslashes() function is used to add backslashes before some specified characters in a given string. Syntax: string addcslashes($string, $characters) Parameters: This function accepts two parameters as shown in the above syntax and are d 2 min read PHP addslashes() Function The addslashes() function is an inbuilt function in PHP and it returns a string with backslashes in front of predefined characters. It does not take any specified characters in the parameter. The predefined characters are: single quote (')double quote (")backslash (\)NULL Note: The addslashes() func 2 min read PHP bin2hex() Function The bin2hex() function in PHP converts a string to hexadecimal values. The conversion is done byte-wise with the high-nibble first. Note: It is not for converting strings representing binary digits into hexadecimal. Syntax: bin2hex($string) Parameters: This function accepts a single parameter $strin 1 min read PHP chop() Function The chop() in PHP is used to remove white spaces or any other specified characters from the end of a string. Syntax: string chop($string, $character) Parameters: This function accepts two parameters as shown in the above syntax and are described below: $string : It is used to specify the string whic 2 min read PHP chr() Function The chr() function is a built-in function in PHP and is used to convert a ASCII value to a character. It accepts an ASCII value as a parameter and returns a string representing a character from the specified ASCII value. The ASCII value can be specified in decimal, octal, or hex values. Octal values 2 min read PHP chunk_split() Function The chunk_split() function is a built-in function in PHP. The chunk_split() function is used to split a string into smaller chunks of a specific length. Syntax: string chunk_split($string, $length, $end) Parameters: This function accepts three parameters as shown in the above syntax and are describe 2 min read PHP convert_uudecode() Function The convert_uudecode() is a built in function in PHP. This function decode a uuencoded string encoded using convert_uuencode() function. The uudecode() functions makes string into printable form. Syntax: string convert_uudecode(string) Parameters: The uuencoded string which will be decoded. Return T 1 min read PHP convert_uuencode() Function The convert_uuencode() is a built-in function in PHP. The convert_uuencode() function encodes a string using the uuencode algorithm. Uuencode encoding translates all strings (including binary data) into printable characters which makes them safe for network transmissions. Syntax: String convert_uuen 1 min read PHP count_chars() Function The count_chars() is an inbuilt function in PHP and is used to perform several operations related to string like the number of an ASCII character occurs in a string. Syntax : count_chars(string,return_mode); Parameters: The count_chars() function takes two parameters string and return_mode as explai 2 min read PHP crc32() Function The crc32() function helps us to calculate a 32-bit crc or cyclic redundancy checksum polynomial for a string. The function uses the CRC32 algorithm.This function can be used to validate data integrity. However, to ensure that we get the correct string representation from the crc32() function, we ne 2 min read Like