0

Having trouble, can anyone tell me how I properly set my variable between 2 numbers? I gave it a shot, but this is not right.

<!DOCTYPE html>
<html lang="en">
<head>
    <title> if/else </title>
</head>

    <body>

    <?php
        $randinterger = rand(10,100));

if ($randinterger <= 25) {
    echo ($randinterger);
    echo ("This is less that 25");
} elseif (26 < $randinterger <= 50) {
    echo ($randinterger);
    echo ("This is between 25 and 50");
} elseif (50 < $randinterger <= 75) {
    echo ($randinterger);
    echo ("This is between 50 and 75");
} else (75 < $randinterger <= 100) {
    echo ($randinterger);
    echo ("This is between 75 and 100");
}

    ?>

</body>
</html>
  • Run this code, and you'll see that you have a syntax error. Also, what is not right? – Ibu Mar 26 '19 at 23:29
  • I think want you want is to compare and check if a variable's value is between 2 number. `26 < $randinterger <= 5` -> should be come `$randinterget > 26 && $randinteger <=5`. And same for other condition. –  Mar 26 '19 at 23:30

1 Answers1

0

You cannot compare numbers like that. See corrected code below.

<?php
        $randinterger = rand(10,100));

if ($randinterger <= 25) {
    echo ($randinterger);
    echo ("This is less that 25");
} elseif (26 < $randinterger && $randinterger <= 50) {
    echo ($randinterger);
    echo ("This is between 25 and 50");
} elseif (50 < $randinterger && $randinterger <= 75) {
    echo ($randinterger);
    echo ("This is between 50 and 75");
} else (75 < $randinterger && $randinterger <= 100) {
    echo ($randinterger);
    echo ("This is between 75 and 100");
}

    ?>

you have to split the conditions. What you want to know if is randinteger is lower than x AND greater than Y.

kristyna
  • 1,360
  • 2
  • 24
  • 41