write a php function to test whether a number is greater than 30, 20 or 10 using ternary operator in PHP

PHP
function test($num) {
return ($num > 30 ? 'greater than 30' : ($num > 20 ? 'greater than 20' : ($num > 10 ? 'greater than 10' : 'Input a number at least greater than 10!')));
}
echo test(40);
🤖 Code Explanation
The first thing this code does is define a test function that takes a single parameter. This function will return a string that says either "greater than 30", "greater than 20", or "greater than 10". If the number inputted is less than 10, it will return the string "Input a number at least greater than 10!".
Next, the code calls the test function with the input 40. Since 40 is greater than 30, the function will return the string "greater than 30".

More problems solved in PHP




















