PHP for循环,中断

时间:2020-01-05 18:06:24

标签: php for-loop break

如果正在计算的值等于另一个变量,我试图在达到指定的重复次数之前让循环停止。我在 if 语句中有一个中断,但是它似乎没有任何作用,因为它一直循环直到达到指定的最终值。

echo "<h4>iteration Stages</h4>\n";
// looping the math for each iteration
for ($i=1; $i <= $iterations ; $i++) { 
    $estimate = 0.5 * ($estimate + $number / $estimate);

        if ($estimate == $real){
            echo round($estimate, 2);
            break;

        } else {
            echo round($estimate, 2);
            echo " -- \n";
    }
}

0 个答案:

没有答案