﻿ PHP Exercise: Test whether a number is greater than 30, 20 or 10 using ternary operator - w3resource

# 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.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

﻿

## PHP: Tips of the Day

PHP: How to generate a random, unique, alphanumeric string for use in a secret link?

Security Notice: This solution should not be used in situations where the quality of your randomness can affect the security of an application. In particular, rand() and uniqid() are not cryptographically secure random number generators. See Scott's answer for a secure alternative.

If you do not need it to be absolutely unique over time:

`md5(uniqid(rand(), true))`

`md5(uniqid(\$your_user_login, true))`