Basic Return Types
function getAge(): int { return 25; } function isAdult(int $age): bool { return $age >= 18; } echo getAge(); echo isAdult(11)
Nullable Return Type
function findUsername(int $id): ?string { if ($id === 1) { return "JohnDoe"; } return null; // It's valid because of the ? before string } echo findUsername(2);
Return Type of void
function logMessage(string $message): void { echo $message; } logMessage("Hello");
Union Return Types
function sum($num1,$num2):int|string{ return $num1+$num2; } echo sum("33","20");