Content · Programming · PHP

PHP strcoll() Function: Usage and Examples

Compares strings according to the current LC_COLLATE locale.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

PHP strcoll() compares strings according to the current LC_COLLATE locale. The examples below show how the call behaves, where it is useful, and which limitations matter.

What Is strcoll() in PHP?


Compares strings according to the current LC_COLLATE locale.

Its call structure is:

strcoll($string1, $string2)

The arguments identify the values or resources passed to strcoll(). Read the examples with attention to the type of each supplied argument and to whether the function modifies its input or returns a separate result.

Example 1: Basic Usage


This example introduces strcoll with a direct call and a small, readable input.

<?php

echo strcoll("apple", "banana") < 0 ? "Earlier" : "Not earlier";

Compares strings according to the current LC_COLLATE locale. Sorting depends on installed locale data and process-level setlocale() state.

Example 2: Another Behavior or Input


Compare this input with the previous example: changing the data or option can affect how strcoll behaves.

<?php

var_dump(strcoll("a", "a") === 0);

The changed input illustrates why output may differ with the chosen values or environment. Sorting depends on installed locale data and process-level setlocale() state.

Practical Example: Using the Feature


The following example incorporates strcoll into a workflow. Values such as paths, connection handles and configuration options must come from trusted application settings.

<?php

$originalLocale = setlocale(LC_COLLATE, 0); if (setlocale(LC_COLLATE, "C") !== false) { echo strcoll("A", "B"); } setlocale(LC_COLLATE, $originalLocale);

In a real application, check each operation result, validate values before use and clean up acquired resources. Sorting depends on installed locale data and process-level setlocale() state.

Important Behavior and Edge Cases


  • Purpose: Compares strings according to the current LC_COLLATE locale.
  • Key limitation: Sorting depends on installed locale data and process-level setlocale() state.
  • Failure handling: Check return values and handle invalid inputs and unavailable operating-system or extension features where relevant.

Comparison With Related Functions


Function or ApproachBest Fit
strcoll()Compares strings according to the current LC_COLLATE locale.
Collator::compare()Uses ICU locale collation rules rather than the current process C locale.

Common Beginner Mistakes


MistakeCorrect Approach
Sorting depends on installed locale data and process-level setlocale() state.The correct approach depends on this behavior: Sorting depends on installed locale data and process-level setlocale() state.
Ignoring errors or cleanup after the operation.Validate inputs and check each result; close files, restore settings or release resources when the operation requires it.

Key Takeaways


  • Compares strings according to the current LC_COLLATE locale.
  • Sorting depends on installed locale data and process-level setlocale() state.
  • Use the examples as starting points, and adapt environmental paths, resources and permissions rather than assuming fixed results.

strcoll is useful when its specific behavior fits the problem. The safest implementations combine the correct API with input validation, error checking and resource cleanup.

Share this resourceSend this page to someone who may find it useful.