I have the following code which should sort 'BMW' to the first position:
$cars=['Toyota','Volvo','BMW'];
usort($cars,function($v){return $v <=> 'BMW';});
var_export($cars);
The result with PHP7 (and smaller) is correct:
array ( 0 => 'BMW', 1 => 'Volvo', 2 => 'Toyota', )
As far as I understand, the result with PHP8 is wrong.
array (0 => 'Volvo', 1 => 'BMW', 2 => 'Toyota',)
Demo: https://3v4l.org/8SCXk
It's not up to the spaceship operator. This one seems to be working correctly. The cause must lie in the behavior of usort.
var_dump(
'Volvo' <=> 'BMW',
'BMW' <=> 'Volvo',
'BMW' <=> 'Toyota',
'Toyota' <=> 'BMW',
'BMW' <=> 'BMW'
);
Demo: https://3v4l.org/UaKrM
My question: Is this a nasty bug in PHP8 or do I need to change my code?