meme meme - 1 month ago 7
PHP Question

PHP if and else statement not working properly

im trying to create a script that generates a number and depending on the number generated it prints something specific but it's not working properly, Heres the code:

<?php
for($zz = 1; $zz <= 20; $zz++) {
$rangen = rand(1,100);
$a = (1 <= 0) && (0 <= 7);
$b = (8 <= 0) && (0 <= 17);
echo ("<br>".$rangen . "<br>");
if($a) {
echo "a";
} elseif ($b) {
echo "b";
} else {
echo "c";
}
}
?>


The error is that it keeps printing "c" no matter what the number is.

If anyone could help that would be great, thanks.

Answer

Your conditions are all wrong. Your comparing the same numbers and never using $rangen, this is why you obtain the same result each time.

1 <= 0 and 8 <= 0 will always return false which is why you always go to the else statement.

Comments