The strcoll Function

The strcoll function compares two strings considering the current locale. The first parameter is the first string to compare, the second is the second string. The function returns 0 if the strings are identical, a number -1 if the first string is less than the second, and a number greater than 1 if the first string is greater than the second.

Syntax

phpstrcoll(string1, string2);

Example

Comparing strings with different locales:

php<?php
  setlocale(LC_COLLATE, 'en_US.UTF-8');
  $res1 = strcoll('apple', 'banana');
  
  setlocale(LC_COLLATE, 'de_DE.UTF-8');
  $res2 = strcoll('äpfel', 'zebra');
  
  echo $res1;
  echo $res2;
?>

Code execution result:

php-1
-1

Example

Comparing identical strings:

php<?php
  setlocale(LC_COLLATE, 'ru_RU.UTF-8');
  $res = strcoll('строка', 'строка');
  echo $res;
?>

Code execution result:

php0

See Also

  • the strcmp function,
    which compares strings without considering the locale
  • the strcasecmp function,
    which compares strings case-insensitively