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>
2
  • Run this code, and you'll see that you have a syntax error. Also, what is not right? Commented Mar 26, 2019 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. Commented Mar 26, 2019 at 23:30

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments