Skip to content

Latest commit

 

History

History
24 lines (20 loc) · 472 Bytes

spaceship_operator.md

File metadata and controls

24 lines (20 loc) · 472 Bytes

Spaceship operator

Spaceship operator is used for comparing

// Integers
echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1

// Floats
echo 1.5 <=> 1.5; // 0
echo 1.5 <=> 2.5; // -1
echo 2.5 <=> 1.5; // 1

// Strings
echo "a" <=> "a"; // 0
echo "a" <=> "b"; // -1
echo "b" <=> "a"; // 1

$games = ['Mario', 'Zelda', 'Fallout', 'Far Cry'];
usort($games, function ($a, $b) {
    return $a <=> $b; // it will sort them by name (by alphabetical order)
});