$year = readline('Type your year of birth: ');
$age = 2023 - $year;
switch ($age) {
case ($age < 0):
echo 'I don't see in the future.';
break;
case ($age >= 0) && ($age <= 3):
echo 'Congrats. A newborn capable of using the PC.';
break;
case ($age > 3):
echo 'Our system calculated that you are:' . ' ' . $age . ' ' . 'years old';
break;
}
So this is from my first lesson of PHP and the statement "echoes" the first case if I input 2023 but it should echo the second one. Any idea why this happens?
$age == 0falls into the case$age < 0it is due to loose comparisons between0andfalsewhich the switch statement employs.