This RFC proposes SORT_STRICT, a new flag that enables strict, type-aware comparison for PHP's sorting and array functions.
<?php $values = [0, "", null, false, "0"]; var_dump(array_unique($values, SORT_STRICT)); // array(5) { // [0]=> int(0) // [1]=> string(0) "" // [2]=> NULL // [3]=> bool(false) // [4]=> string(1) "0" // } ?>
PHP was famously created as a loosely typed language, prioritizing flexibility and ease of use. However, the introduction of strict types in PHP 7 marked a massive paradigm shift for the language. Over the last decade, the ecosystem has matured significantly; major frameworks like Symfony and Laravel, along with modern coding standards like PER, have effectively pushed strict typing from an optional feature to a de facto standard for professional PHP development.
Despite this evolution, there is no native way to sort or filter arrays without type coercion. The sort() family defaults to SORT_REGULAR (loose equality), while array_unique() defaults to SORT_STRING (string casting), neither of which distinguishes between values of different types. This creates a friction point where modern application logic is strict, but utility functions remain loose. Currently, developers wishing to filter unique values strictly or sort mixed-type arrays deterministically must implement userspace closures using usort or specific loops. This is verbose, slower than internal C implementations, and prone to error.
Add a new constant SORT_STRICT that can be passed to the following functions:
sort(), rsort()asort(), arsort()ksort(), krsort()array_multisort()array_unique()
SORT_STRICT implements a two-step comparison:
Values are first compared by their type according to this hierarchy:
NULL < Bool < Int < Float < String < Array < Object < Resource
If types differ, the comparison is decided here. No type coercion occurs.
If types are identical, standard PHP comparison semantics apply:
| Type | Comparison Method |
|---|---|
| NULL | Always equal |
| Bool | false < true |
| Int | Numeric comparison |
| Float | Numeric comparison |
| String | Binary comparison (strcmp) |
| Array | Standard array comparison, with SORT_STRICT applied recursively to elements |
| Object | Standard object comparison, with SORT_STRICT applied recursively to properties |
| Resource | Resource ID comparison |
The key difference from SORT_REGULAR is that type coercion never occurs. An integer 1 and string “1” are never equal under SORT_STRICT because they differ at Step 1.
<?php $values = [0, "", null, false, "0"]; var_dump(array_unique($values, SORT_REGULAR)); // array(2) { // [0]=> int(0) // [1]=> string(0) "" // } var_dump(array_unique($values, SORT_STRICT)); // array(5) { // [0]=> int(0) // [1]=> string(0) "" // [2]=> NULL // [3]=> bool(false) // [4]=> string(1) "0" // } ?>
Compare arrays for identical values regardless of order:
Because SORT_STRICT groups values by type, sorted data has a useful property: if the first and last elements share the same type, the array is homogeneous.
None. This RFC adds a new constant and does not modify existing behavior.
Next PHP 8.x
IDEs, static analyzers, and language servers will need to recognize the new SORT_STRICT constant. No breaking changes are expected.
None.
None.
None currently.
strict_compare($a, $b) function exposing the comparison logic directlySORT_STRICT in additional array functions (e.g., array_diff(), array_intersect())Primary vote requiring a 2/3 majority:
TODO: Link to implementation PR.
TODO: After acceptance.
None yet.