Type Declarations
Add types to parameters and returns. ?int is nullable and int|string is a union. declare(strict_types=1) makes checks strict.
Example
function add(int $a, int $b): int { return $a + $b; }
echo add(1, 2), "\n";
function f(?string $s): string { return $s ?? "none"; }
echo f(null);Result
3 none
Exercise
Return type.
function f(): { return 1; }
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.