PHP strcoll() Function
The PHP strcoll() function is used to perform case-sensitive string comparison of two strings.
Unlike strcmp() function, this function is not binary safe. This function uses the current locale for doing the comparisons. If the current locale is C or POSIX, this function is equivalent to strcmp() function.
Note: The strcoll() function is a case-sensitive but not a binary-safe function.
Syntax
strcoll(str1, str2)
Parameters
str1 |
Required. Specify the first string to compare |
str2 |
Required. Specify the second 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, strcoll() function is used to compare two strings and returns values based on the value of two strings.
<?php //returns 0 as both strings are same echo "result1: ".strcoll('Hello', 'Hello')."\n"; //returns positive number as first unmatched character //of first string is greater than that of second string echo "result2: ".strcoll('World', 'Hello')."\n"; //returns negative number as first unmatched character //of first string is less than that of second string echo "result3: ".strcoll('Hello', 'World')."\n"; ?>
The output of the above code will be:
result1: 0 result2: 15 result3: -15
❮ PHP String Reference