<?php
// Declare two arrays
$arr1 = array(1, 2, 3);
$arr2 = array(2, 3, 4);
// Sort using spaceship operator
usort($arr1, function($a, $b) {
return $a <=> $b;
});
echo "Sorted array using spaceship operator: ";
print_r($arr1);
// Compare using spaceship operator
echo "Comparison using spaceship operator: ";
echo $arr1 <=> $arr2;
?>
Sorted array using spaceship operator: Array ( [0] => 1 [1] => 2 [2] => 3 )
Comparison using spaceship operator: -1
$arr1
` and `$arr2
` and assigned them the values `(1, 2, 3)
` and `(2, 3, 4)
`, respectively. <=>
` to sort `$arr1
` in ascending order and compare it to `$arr2
`. print_r
` and `echo
` statements.