-5

I have the following question(project) and tried a million times, but just can't seem to crack this one

Conditional operator instead of if/else statement

age 12 - 19 should display 'You are a teenager' between 19 and 30 should display 'You are a young adult else 'You are neither a teenager nor a young adult'

Please assist

4 Answers4

1
echo  ($age >= 12 && $age <= 19)?'You are a teenager':'';
echo  ($age > 19 && $age <= 30)?'You are a young adult':'';
echo  ($age > 30 || $age < 12)?'You are neither a teenager nor a young adult':'';
Random
  • 3,158
  • 1
  • 15
  • 25
0

It is not that hard, and with braces you make it more clear.

echo ($age >= 12 && $age < 19 ? 'You are a teenager' : ($age >= 19 && $age < 30 ? 'You are a young adult' : 'You are neither a teenager not a young adult'));
Blaatpraat
  • 2,829
  • 11
  • 23
0
<?php

   $age = 13;

  echo $answer = ($age >= 12 && $age < 19 ? 'You are a teenager' : ($age >= 19 && $age < 30 ? 'You are a young adult' : 'You are neither a teenager not a young adult'));

?>
Hardy Mathew
  • 684
  • 1
  • 6
  • 22
0

you can acheive this terenary operator

   echo    if($age>12 && $age<20) ?"You are a teenager":'';
   echo    if($age>19 && $age<31) ?"You are an adult":'';
   echo    if($age>31 && $age<12) ?"You are not an adult nor a teenager":'';
Neelesh
  • 193
  • 1
  • 12