[Solved] Ages - PHP Task (all the values are inclusive)

  

3
Topic starter

Write a program that determines whether based on the given age a person is: baby, child, teenager, adult, elder.

The bounders are:

  • 0-2 – baby;
  • 3-13 – child;
  • 14-19 – teenager;
  • 20-65 – adult;
  • >=66 – elder;

All the values are inclusive.

Examples:

examples php task ages

2 Answers
3

Here is my solution with switch case:

<?php
 
echo 'Please input the age: ' . PHP_EOL;
$age = intval(readline());
 
switch (true) {
    case $age < 2:
        echo 'baby';
        break;
    case $age >= 3 && $age <= 13:
        echo 'child';
        break;
    case $age >= 14 && $age <= 19:
        echo 'teenager';
        break;
    case $age >= 20 && $age <= 65:
        echo 'adult';
        break;
    case $age >= 66:
        echo 'elder';
        break;
    default:
        echo 'Not a human...';
}
2

Mine is with if + elseif + else:

<?php
 
/*
•   0-2 – baby;
•   3-13 – child;
•   14-19 – teenager;
•   20-65 – adult;
•   >=66 – elder;
*/
 
echo 'Input the age: ' . PHP_EOL;
$age = intval(readline());
 
if ($age <= 2) {
    echo 'baby';
} elseif ($age <= 13) {
    echo 'child';
} elseif ($age <= 19) {
    echo 'teenager';
} elseif ($age <= 65) {
    echo 'adult';
} else {
    echo 'elder';
}
Share: