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
int strcoll ( string $string1 , string $string2 )
$string1: The first string to be compared.setlocale(LC_COLLATE, "C"); // Available on typical PHP installations.
$str1 = "apple";$str2 = "banana";var_dump(strcoll($str1, $str2) < 0);
Output:
bool(true)
setlocale(LC_COLLATE, "C");$str1 = "apple";$str2 = "apple";var_dump(strcoll($str1, $str2) === 0);
Output:
bool(true)
$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.
setlocale(LC_COLLATE, "C");$str1 = "123";$str2 = "456";var_dump(strcoll($str1, $str2) < 0);
Output:
bool(true)
setlocale(LC_COLLATE, "C");$str1 = "Hello";$str2 = "hello";var_dump(strcoll($str1, $str2) < 0); // C locale compares byte values
Output:
bool(true)
Collator class from the PHP intl extension.
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.