Returning bool from comparison function is deprecated

Description

The comparison function should return 0, 1 or -1, and not just booleans. 1 means that $a > $b, -1 that $a < $b and 0 means that $a === $b. This is a richer information than boolean, and it also fits very well with the spaceship operator <=>.

Example

<?php

function mySort(int $a, int $b) : bool {
    return $a > $b;
}

$array = [1,2,3,4,5,6,7,8,9,10];
usort($array, mySort(...));

?>

Solutions

  • Set the returntype of the comparison function to int.

  • Use the spaceship operator as possible.

  • Cast the result to integer.

Changed Behavior

This error may appear following an evolution in behavior, in previous versions. See sortClosureReturnType.

Changed Behavior

This error may appear following an evolution in behavior, in previous versions. See sortClosureReturnType.