Lesson content
Read, practise, then check your understanding
if uses PHP boolean conversion. Guard clauses validate early and keep the main path shallow. Traditional switch uses loose comparison and can fall through; PHP 8 match is strict, returns a value, and never falls through.
Practical example
<?php
function grade(int $score): string {
if ($score < 0 || $score > 100) throw new InvalidArgumentException();
return match (true) { $score >= 80 => 'excellent', $score >= 50 => 'pass', default => 'retry' };
}
A match without a suitable arm/default throws. Avoid relying on surprising truthiness such as string '0'; transform boundary data into explicit domain types before decision logic.
Knowledge check
Answer every question correctly to complete this chapter.
0 of 10 checks passed
Your progress is saved on this device.