PHP strncasecmp() Function
Last Updated :
22 Jun, 2023
Improve
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:
php
Output :
php
Output:
php
Output:
php
Output:
php
Output:
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
- 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
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 : 7Below programs illustrate the strncasecmp() function in PHP: Program 1: When the two strings are identical:
<?php
$str1 = "Geeks for Geeks ";
$str2 = "Geeks for Geeks ";
// Both the strings are equal
$test=strncasecmp($str1, $str2, 16 );
echo "$test";
?>
<?php
$str1 = "Geeks for Geeks ";
$str2 = "Geeks for Geeks ";
// Both the strings are equal
$test=strncasecmp($str1, $str2, 16 );
echo "$test";
?>
0Program 2 : When first string greater than the second string:
<?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";
?>
<?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";
?>
6Program 3: First string is smaller than the second string:
<?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";
?>
<?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";
?>
-6Program 4: This program illustrates the case-insensitivity of the function:
<?php
// Input Strings
$str1 = "GEEKS FOR GEEKS ";
$str2 = "Geeks for Geeks ";
// Both the strings are equal
$test=strncasecmp($str1, $str2, 16 );
echo "$test";
?>
<?php
// Input Strings
$str1 = "GEEKS FOR GEEKS ";
$str2 = "Geeks for Geeks ";
// Both the strings are equal
$test=strncasecmp($str1, $str2, 16 );
echo "$test";
?>
0Program 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
// 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";
?>
<?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";
?>
-10 10Reference: http://php.net/manual/en/function.strncasecmp.php