strncasecmp() Function in PHP 8.2 With Example

Support PHP Version: PHP 7.1, PHP 7.2, PHP 7.3, PHP 7.4, PHP 8.0, PHP 8.1, PHP 8.2, PHP 8.3 With Latest All Version Support.

If you specifically need a case-insensitive comparison with a limited number of characters, you can achieve this by combining `strcasecmp()` with `substr()` to compare a substring of each string. Here’s an example:

<?php
// Define two strings
$string1 = "Hello World";
$string2 = "HELLO world";

// Compare the first 5 characters of each string in a case-insensitive manner
$result = strcasecmp(substr($string1, 0, 5), substr($string2, 0, 5));

// Output the result
if ($result == 0) {
echo "The first 5 characters of the strings are equal.\n";
} elseif ($result < 0) {
echo "The first 5 characters of the first string are less than the second string.\n";
} else {
echo "The first 5 characters of the first string are greater than the second string.\n";
}
?>

This code snippet compares the first 5 characters of two strings, “Hello World” and “HELLO world”, in a case-insensitive manner using `strcasecmp()` and `substr()`. It then prints a message based on the result of the comparison.