PHP Exercises : Test whether a number is greater than 30, 20 or 10 using ternary operator
PHP : Exercise-21 with Solution
Write a PHP function to test whether a number is greater than 30, 20 or 10 using ternary operator.
Sample Solution: -
PHP Code:
<?php
function trinary_Test($n){
$r = $n > 30
? "greater than 30"
: ($n > 20
? "greater than 20"
: ($n >10
? "greater than 10"
: "Input a number atleast greater than 10!"));
echo $n." : ".$r."\n";
}
trinary_Test(32);
trinary_Test(21);
trinary_Test(12);
trinary_Test(4);
?>
Sample Output:
32 : greater than 30 21 : greater than 20 12 : greater than 10 4 : Input a number atleast greater than 10!
Flowchart:

PHP Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a PHP script to get the last occurred error.
Next: Write a PHP script to get the full URL.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
PHP: Tips of the Day
Mutates the original array to filter out the values specified
Example:
<?php function tips_pull(&$items, ...$params) { $items = array_values(array_diff($items, $params)); return $items; } $items = ['x', 'y', 'z', 'x', 'y', 'z']; print_r(tips_pull($items, 'y', 'z')); ?>
Output:
Array ( [0] => x [1] => x )
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises