CakeFest 2017 NYC, the Official CakePHP Conference


(PHP 4 >= 4.0.5, PHP 5, PHP 7)

strcollLocale based string comparison


int strcoll ( string $str1 , string $str2 )

Note that this comparison is case sensitive, and unlike strcmp() this function is not binary safe.

strcoll() uses the current locale for doing the comparisons. If the current locale is C or POSIX, this function is equivalent to strcmp().



The first string.


The second string.

Valorile întoarse

Returns < 0 if str1 is less than str2; > 0 if str1 is greater than str2, and 0 if they are equal.

Istoricul schimbărilor

Versiune Descriere
4.2.3 This function now works on win32.

A se vedea și

  • preg_match() - Perform a regular expression match
  • strcmp() - Binary safe string comparison
  • strcasecmp() - Binary safe case-insensitive string comparison
  • substr() - Return part of a string
  • stristr() - Case-insensitive strstr
  • strncasecmp() - Binary safe case-insensitive string comparison of the first n characters
  • strncmp() - Binary safe string comparison of the first n characters
  • strstr() - Find the first occurrence of a string
  • setlocale() - Set locale information

add a note add a note

User Contributed Notes 2 notes

14 years ago
Note that some platforms implement strcmp() and strcasecmp() according to the current locale when strings are not binary equal, so that strcmp() and strcoll() will return the same value! This depends on how the PHP strcmp() function is compiled (i.e. if it uses the platform specific strcmp() found in its standard library!).
In that case, the only difference between strcoll() and strcmp() is that strcoll() may return 0 for distinct strings(i.e. consider strings are equal) while strcmp() will differentiate them if they have distinct binary encoding! This typically occurs on Asian systems.
What you can be sure is that strcmp() will always differentiate strings that are encoded differently, but the relative order may still use the current locale setting for collation order!
sakkarinlaohawisut15 at hotmail dot com
13 years ago
strcoll()'s behavior is sometimes a little bit confusing. It depends on LC_COLLATE in your locale.


= 'a';
$b = 'A';

strcmp ($a, $b) . "\n"; // prints 1

setlocale (LC_COLLATE, 'C');
"C: " . strcoll ($a, $b) . "\n"; // prints 1

setlocale (LC_COLLATE, 'de_DE');
"de_DE: " . strcoll ($a, $b) . "\n"; // prints -2

setlocale (LC_COLLATE, 'de_CH');
"de_CH: " . strcoll ($a, $b) . "\n"; // prints -2

setlocale (LC_COLLATE, 'en_US');
"en_US: " . strcoll ($a, $b) . "\n"; // prints -2


This is useful e. g. if want to sort an array by using strcoll:


= array ('a', 'A', '', '', 'b', 'B');

setlocale (LC_COLLATE, 'C');
usort ($a, 'strcoll');
print_r ($a);


This is like sort($a):
    [0] => A
    [1] => B
  [2] => a
    [3] => b
    [4] =>
    [5] =>


(LC_COLLATE, 'de_DE');
usort ($a, 'strcoll');
print_r ($a)


This is completely different:
    [0] => a
    [1] => A
    [2] =>
    [3] =>
    [4] => b
  [5] => B
To Top