而for循环内部的循环并没有显示所有结果,只有1

时间:2013-07-06 09:27:38

标签: php loops

我有这两个代码应该做同样的事情,但回声完全不同,我无法弄清楚为什么。 第一个代码

<?php
    $runs = 10000;

    $start = microtime(true);
    for ($x = 0; $x < $runs; $x++) {
        for($i=1;$i<1001;$i++) echo $i%15?$i%5?$i%3?$i:'koukou':'roukou':'koukouroukou'," ";
    }
    $end = microtime(true);
    $elapsed = number_format($end - $start, 4);
    $one = number_format(($end - $start) / 500, 7);
    echo "$runs runs in $elapsed seconds, average of $one seconds per call";
?>

第二个代码

<?php
    $runs = 10000;

    $start = microtime(true);
    for ($x = 0; $x < $runs; $x++) {
        while(++$i<1001)echo$i%15?$i%5?$i%3?$i:'koukou':'roukou':'koukouroukou'," ";
    }
    $end = microtime(true);
    $elapsed = number_format($end - $start, 4);
    $one = number_format(($end - $start) / 500, 7);
    echo "$runs runs in $elapsed seconds, average of $one seconds per call";
?>

为什么我会得到不同的回声结果?

1 个答案:

答案 0 :(得分:0)

 for ($x = 0; $x < $runs; $x++) {
        while(++$i<1001)echo$i%15?$i%5?$i%3?$i:'koukou':'roukou':'koukouroukou'," ";
 }

应该是

 for ($x = 0; $x < $runs; $x++) {
    $i=0;
        while(++$i<1001)echo$i%15?$i%5?$i%3?$i:'koukou':'roukou':'koukouroukou'," ";
 }