What is the best way to check if a number is even or has a decimal point in PHP?
To check if a number is even or has a decimal point in PHP, you can use the modulus operator (%) to check if the number is divisible by 2 (even) and the floor function to check if the number has a decimal point. By combining these two checks, you can determine if a number is even or has a decimal point.
<?php
function checkNumber($num){
if($num % 2 == 0){
echo $num . " is even.";
} else {
echo $num . " is odd.";
}
if(floor($num) != $num){
echo " It has a decimal point.";
} else {
echo " It does not have a decimal point.";
}
}
checkNumber(10); // Output: 10 is even. It does not have a decimal point.
checkNumber(3.5); // Output: 3.5 is odd. It has a decimal point.
?>