strcoll(): locale-based string comparison

Compare strings using the current locale

strcoll() compares strings according to the active collation locale. Locale availability varies by server, so set and verify the locale when locale-aware ordering is important.

setlocale(LC_COLLATE, 'C');$result = strcoll('apple', 'banana');var_dump($result < 0); // true
The PHP strcoll() function is used to compare two strings based on the current locale settings. It performs a locale-aware comparison, which makes it more suitable for handling strings in different languages and regional settings compared to binary comparison functions like strcmp().

Syntax

int strcoll ( string $string1 , string $string2 )
$string1: The first string to be compared.
$string2: The second string to be compared.
Return Value: This function returns:
  • Less than 0 if $string1 is less than $string2.
  • Greater than 0 if $string1 is greater than $string2.
  • 0 if $string1 is equal to $string2.

Basic Example of strcoll()

In the following example, we compare two strings using the strcoll() function.
setlocale(LC_COLLATE, "C"); // Available on typical PHP installations.
$str1 = "apple";$str2 = "banana";var_dump(strcoll($str1, $str2) < 0);
Output:
bool(true)

Example with Equal Strings

Here, we compare two identical strings, and the function returns 0.
setlocale(LC_COLLATE, "C");$str1 = "apple";$str2 = "apple";var_dump(strcoll($str1, $str2) === 0);
Output:
bool(true)

Locale-Aware Example

Locale names and collation rules depend on the host operating system. This example checks whether each requested locale exists instead of assuming identical numeric results on every server.
$str1 = 'apple';$str2 = 'Apple';foreach (['en_US.UTF-8', 'fr_FR.UTF-8'] as $locale) {
    $chosen = setlocale(LC_COLLATE, $locale);
    if ($chosen === false) {
        echo "$locale is unavailable on this server.\n";
        continue;
    }
    $result = strcoll($str1, $str2);
    echo "$chosen: " . ($result <=> 0) . "\n";}
Output:
Output varies by installed locales and their collation rules. A value of -1, 0 or 1 indicates the comparison sign; unavailable locales are reported.

Comparing Numeric Strings

When comparing numeric strings, strcoll() treats them like strings based on the current locale, not as numbers.
setlocale(LC_COLLATE, "C");$str1 = "123";$str2 = "456";var_dump(strcoll($str1, $str2) < 0);
Output:
bool(true)

Case-Sensitive Comparison

Unlike strcasecmp(), the strcoll() function performs a case-sensitive comparison based on locale settings.
setlocale(LC_COLLATE, "C");$str1 = "Hello";$str2 = "hello";var_dump(strcoll($str1, $str2) < 0); // C locale compares byte values
Output:
bool(true)

Conclusion

The PHP strcoll() function is useful when you need locale-aware string comparison. It can follow installed locale collation rules, but results vary across systems. If predictable, multilingual sorting is required, consider the ICU Collator class from the PHP intl extension.
String Functions strcmp(): Binary comparison strcasecmp(): Ignore ASCII case


Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer