Tsallaka zuwa abun ciki
Justice Tech School

Ternary, Spaceship and Null Safe

The ternary ?: shortens if else. <=> returns -1, 0 or 1 for sorting. The null safe operator ?-> stops when an object is null.

Example
$age = 20;
echo $age >= 18 ? "adult" : "minor", "\n";
echo $age ?: "none", "\n";
echo 3 <=> 5, " ", 5 <=> 5, " ", 7 <=> 5, "\n";
$u = null;
var_dump($u?->name);

Result

adult
20
-1 0 1
NULL

Exercise

Compare for sorting.

$a  $b

Quiz Test

What does <=> return for 3 and 5?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.