PHP 7 "for loop"if语句的错误.循环32.767次后停止工作


PHP 7 "for loop" bug with if statement. Stops working after 32.767 loops?

我一直在玩PHP,并试图找到这个"错误"修复,但无法找到任何关于它…

任务:找出找出一个随机选择的数字需要多少次尝试。

我在for循环外生成了一个随机数,并声明了一个变量,该变量将是for循环运行以尝试猜测该数字的最大次数。如果for循环内生成的随机数与循环外生成的随机数是"==",那么我就把这个随机数保存在一个变量中,这样就节省了正确处理随机数并跳出循环所需的时间……简单的东西。

"BUG":

一切都很顺利,直到我们在for循环中运行了32.767次。如果我们处理的是小的数字,没有什么问题,但是如果我们尝试一些大的随机数,比如大于50.000的数字,无论如何,for循环中的If语句将在第32.767次执行。这就像"=="突然变成了"="。

作为PHP的新手,我认为这可能是内存的事情,但我尝试了一些没有if语句的大循环,它们都工作…

代码如下:

<?php
$number = rand(1,50000); // Change this to something small, bellow 30.000, and it will start to work. make it the same as line 10.
$correctNumber;
$maximumLoops = 50000; // No matter how much loops you try, if the random number is big and we get to the 32.767th loop, the if statement will execute.
$numberOfLoops = 0;

for($i = 0; $i < $maximumLoops; $i++){
    $randNumber = rand(1,50000); // Change this to something small, bellow 30.000, and it will start to work. make it the same as line 3.
    if($randNumber == $number){
        $correctNumber = $number;
        $numberOfLoops = $i;
        break;
    }
}
echo "The Number was: $number<br>";
echo "Correct Number is: $correctNumber<br>";
echo "The maximum number of loops was: $maximumLoops<br>";
echo "Number of loops to get it right: $numberOfLoops";

任何帮助都将非常感激。由于

听起来你在Windows上运行的问题,最大随机数是32,767