PHP strcoll() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

In PHP, strcoll() compares two strings using the current locale's sorting rules.

It is like strcmp(), but the order can change when you call setlocale() with LC_COLLATE.

Syntax

Call strcoll() with the values you need. Start with this line.

PHP
$result = strcoll($a, $b);
CallWhat it does
strcoll("apple", "banana")Returns a negative number.
strcoll("a", "a")Returns 0.

Results depend on the locale installed on the server. In the default "C" locale, strcoll() behaves like strcmp().

Library declarations
PHP
strcoll(string $string1, string $string2): int

Parameters

ParameterDescription
string1Required. The first string.
string2Required. The second string.

Return Value

Return ValueDescription
intLess than 0 if string1 is less than string2, 0 if equal, greater than 0 if string1 is greater.

PHP Examples

Example 1 — Compare in the C Locale

Set the collation locale first, then compare.

PHP
<?php
setlocale(LC_COLLATE, "C");

if (strcoll("apple", "banana") < 0) {
    echo "apple comes first", PHP_EOL;
} else {
    echo "banana comes first", PHP_EOL;
}

How It Works

  1. Set the locale. LC_COLLATE controls string sorting.
  2. Compare two words. apple sorts before banana.
  3. Get a negative number. The first string is smaller.
  4. Print the answer. Test the sign, not an exact number.

Example 2 — Uppercase vs Lowercase in C

In the C locale, uppercase letters sort before lowercase ones.

PHP
<?php
setlocale(LC_COLLATE, "C");

if (strcoll("Z", "a") < 0) {
    echo "Z comes first", PHP_EOL;
} else {
    echo "a comes first", PHP_EOL;
}

How It Works

  1. Use the C locale. Sorting follows byte values.
  2. Compare Z and a. Z is 90, a is 97.
  3. Z is smaller. So it sorts first.
  4. Other locales differ. Many would place a before Z.

Example 3 — Sort with usort()

Use strcoll as the comparison function.

PHP
<?php
setlocale(LC_COLLATE, "C");

$items = ["b", "C", "a"];
usort($items, "strcoll");

echo implode(", ", $items), PHP_EOL;

How It Works

  1. Set the locale. This keeps the demo predictable.
  2. Pass "strcoll" to usort(). It compares each pair.
  3. Uppercase goes first. That is how the C locale works.
  4. Print the list. The order is C, a, b.

Frequently Asked Questions

strcoll() compares two strings using the sorting rules of the current locale. It returns a number you can use to sort strings.
strcoll(string $string1, string $string2): int. Call setlocale(LC_COLLATE, ...) before it to choose the rules.
strcmp() always compares raw bytes. strcoll() follows locale rules, so accented or mixed-case letters may sort differently.
Locales are installed per system. If a locale is missing, PHP falls back to another one. Always check what setlocale() returns.
Only if the system locale supports UTF-8. For reliable multibyte sorting, consider the Collator class from the intl extension.

Next: strcspn()

strcspn() counts how many characters at the start of a string do not match any character f.

strcspn() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful