PHP strncasecmp() Function
The PHP strncasecmp() function is used to perform binary safe case-insensitive string comparison of the first n characters of two strings.
This function is same as strcasecmp() function, except this function has one more parameter which is used to specify number of (upper limit of the) characters from each string to be used in the comparison.
Note: The strncasecmp() function is a binary-safe and a case-insensitive function.
Syntax
strncasecmp(str1, str2, length)
Parameters
str1 |
Required. Specify the first string to compare. |
str2 |
Required. Specify the second string to compare. |
length |
Required. Specify number of characters of each string to compare. |
Return Value
Returns the following value:
- Returns a number less than 0, when first string is less than second string.
- Returns 0, when both strings are equal.
- Returns a number greater than 0, when first string is greater than second string.
Example:
In the example below, strncasecmp() function is used to compare two strings and returns values based on the value of two strings.
<?php //returns 0 as first 5 characters //of both strings are same echo "result1: ".strncasecmp('HELLO Jo', 'hello Kim', 5)."\n"; //returns positive number as first unmatched character //of first string is greater than that of second string echo "result2: ".strncasecmp('World', 'Hello', 5)."\n"; //returns negative number as first unmatched character //of first string is less than that of second string echo "result3: ".strncasecmp('Hello', 'World', 5)."\n"; ?>
The output of the above code will be:
result1: 0 result2: 15 result3: -15
❮ PHP String Reference