PHP for循环需要很长时间

时间:2018-02-01 00:41:58

标签: php

请原谅我,我只是学习PHP和循环。这个for循环需要很长的时间来处理,我不知道为什么。我要列出您在表格中输入年龄并且年龄= $ age时所有年份的活动年份。

        //list the years you have been alive
        echo "<br>You know what? Just for fun I am going to list all"
        . " the year's you have been alive. Here you go:<br>";
        $year1 = date("Y");
        for ($y = $age + 1; $y < 100; $y--) {
            if ($y > 0) {
                $yearval = $year1 - $y;
                echo "$yearval<br>";
            }
        }

2 个答案:

答案 0 :(得分:2)

你有一个无限循环。如果循环开始时1,则此条件将始终为真,因为您每次通过循环从$y减去$y。当0到达$y > 0时,它会继续循环显示负值,但它不会打印任何时间。

您应该测试for ($y = $age + 1; $y > 0; $y--) { $yearval = $year1 - $y; echo "$yearval<br>"; }

"SELECT field1, field2 CASE WHEN REGEXP_CONTAINS(field3, r"^\".*\"$") THEN 'this' WHEN REGEXP_CONTAINS(field3, r"^\[.*]$") THEN 'that' WHEN field3 = '(not provided)' THEN NULL ELSE 'the_other' END AS better_field_3, field4 FROM `interpolated_table_name1` AS tbl LEFT JOIN `interpolated_table_name2` AS tbl2 ON blah = blah"

答案 1 :(得分:2)

问题是你的Consumer[Base]循环会无限期倒数。为防止这种情况发生,除检查for是否小于100外,还应检查y是否大于0

for ($y = $age + 1; $y < 100 && $y > 0; $y--)

如:

<?php

//list the years you have been alive
echo "<br>You know what? Just for fun I am going to list all the years you have been alive. Here you go:<br>";
$year1 = date("Y");
$age = 20; // Sample data
for ($y = $age + 1; $y < 100 && $y > 0; $y--) {
    $yearval = $year1 - $y;
    echo "$yearval<br>";
}

请注意,在执行此操作时,您不再需要if条件。

除此之外,years中不需要撇号。

可以看到 here

希望这有帮助! :)