PHP String strncasecmp() Function
The PHP String strncasecmp() function is used for string comparison of the first n characters. It is case insensitive. This function is similar to strcasecmp(); the only difference is that you can specify how many characters from each string will be used for comparison.
Syntax
Below is the syntax of the PHP String strncasecmp() function −
int strncasecmp ( string $string1, string $string2, int $length )
Parameters
Here are the parameters of the strncasecmp() function −
$string1 − (Required) It specifies first string to search.
$string2 − (Required) It specifies second string to search.
$length − (Required) It specifies number of characters from each string to be used in the comparison.
Return Value
The strncasecmp() function returns -1 if string1 is less than string2, 1 if string1 is greater than string2, and zero if they are equal.
PHP Version
First introduced in core PHP 4.0.2, the strncasecmp() function continues to function easily in PHP 5, PHP 7, and PHP 8.
Example 1
First we will show you the basic example of the PHP String strncasecmp() function to compare the two given strings.
<?php
echo strncasecmp("Example!","example!",6);
?>
Output
Here is the outcome of the following code −
0
Example 2
In the below PHP code we will use the strncasecmp() function and compares the first 3 characters of two strings.
<?php
// Define two strings
$string1 = "Hello";
$string2 = "heLLo";
// Compare the first 3 characters of both strings
$result = strncasecmp($string1, $string2, 3);
// Use a switch statement to output the result
switch (true) {
case ($result < 0):
echo "String 1 is less than String 2\n";
break;
case ($result > 0):
echo "String 1 is greater than String 2\n";
break;
default:
echo "Both strings are equal for the first 3 characters\n";
break;
}
?>
Output
This will generate the below output −
Both strings are equal for the first 3 characters
Example 3
Now the below code retrieves specific information from strncasecmp(), and prints it.
<?php
// Define two strings
$string1 = "Goodbye";
$string2 = "Good";
// Compare the first 5 characters of both strings
$result = strncasecmp($string1, $string2, 5);
// Output the result
if ($result < 0) {
echo "$string1 is less than $string2\n";
} elseif ($result > 0) {
echo "$string1 is greater than $string2\n";
} else {
echo "Both strings are equal for the first 5 characters\n";
}
?>
Output
This will create the below output −
Goodbye is greater than Good
Example 4
This example uses strncasecmp() function to compare two case-insensitive strings over their whole length.
<?php
// Define two strings
$string1 = "Programming";
$string2 = "programming";
// Compare the full length of both strings
$result = strncasecmp($string1, $string2, strlen($string1));
// Output the result
if ($result < 0) {
echo "$string1 is less than $string2\n";
} elseif ($result > 0) {
echo "$string1 is greater than $string2\n";
} else {
echo "Both strings are equal for the first 5 characters\n";
}
?>
Output
Following is the output of the above code −
Both strings are equal for the first 5 characters