Example 1 — Compare in the C Locale
Set the collation locale first, then compare.
<?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
- Set the locale.
LC_COLLATEcontrols string sorting. - Compare two words.
applesorts beforebanana. - Get a negative number. The first string is smaller.
- Print the answer. Test the sign, not an exact number.

