2

I want a random integer to be generated in the range from 1 to 3 until 2 will be generated.

Please review the code below - What am I doing wrong?

Thank you!

<?php
    $min = 1;
    $max = 3;
    $number = rand($min,$max);
    while($number !== 2) {
    echo ($number);
    }
?>

1 Answer 1

3

your rand() is not in the while loop, so the rand() will execute one time.

If the $number is not 2, the while loop will execute without stoping.

If the $number is 2, the while loop will not executed.

while(($number = rand($min, $max)) != 2){echo $number;}
Sign up to request clarification or add additional context in comments.

1 Comment

so i just ran this forgetting to set a min and max :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.