Write a script that uses a conditional operator to determine whether a variable contains a number and whether the number is even. You need to use the is_numeric () function and the conditional operator. For floating-point number, you need to use the round () function to convert the value to the nearest whole number. Save the document as IsEven.php.
php code is as follows,
save the file name as IsEven.php :
<?php
//declare an assign a variable called num
$num = 11.60;
/*using is_numeric() function inside if conditional operator
to determine whether the variable num is number or not
*/
if (is_numeric($num)) {
/* using round() function to convert
the value to the nearest whole number*/
/* if conditional operator is used
to determine whether the variable num is even or not*/
if(round($num) % 2 == 0){
echo "Even number";
}
else{
echo "Odd number";
}
}
//enters else block if is_numeric returns false
else {
echo " is not numeric";
}
?>
output screenshots for different values of num variable:
Get Answers For Free
Most questions answered within 1 hours.