settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: Ages - PHP Task (Solved)

+4 votes

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

asked in PHP category by user richard8502

2 Answers

+3 votes

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...';
}

 

answered by user paulcabalit
+2 votes

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';
}
answered by user nikole
...