为什么我的PHP代码没有任何原因?

时间:2021-03-17 21:05:02

I have a for loop in my code. I haven't changed anything on this part of code for about 5-6 days and I never had problems with it.

我的代码中有一个for循环。在这部分代码中,我已经有5-6天没有做任何修改了,而且我从来没有遇到过问题。

Since yesterday I tried to reload my code and it allways gives me this error:

从昨天开始我尝试重新加载我的代码,它总是给我这个错误:

Maximum execution time of 30 seconds exceeded - in LogController.php line 270

Well I can't explain why but maybe someone of you could look over it.

我不能解释为什么,也许你们中有人可以去看看。

This is my code around line 270.

这是我在270行的代码。

$topten_sites = [];
for ($i = 0; $i <= count($sites_array); $i++) {
    if ($i < 10) { // this is 270
        $topten_sites[] = $sites_array[$i];
    }
}
$topten_sites = collect($topten_sites)->sortByDesc('number')->all();

As I said, it worked perfectly, so why it gives me an error? If I uncomment these lines and every other line that contains the $topten_sites array, the code workes again.

就像我说的,它工作得很好,为什么它会给我一个错误呢?如果我取消这些行和包含$topten_sites数组的其他行的注释,代码将再次运行。

4 个解决方案

#1


15  

This looks wrong:

这看起来是错误的:

    for ($i = 0; $i <= $sites_array; $i++) {
        if ($i < 10) { // this is 270
            $topten_sites[] = $sites_array[$i];
        }
    }

If $sites_array is an array, it makes no sense to compare it to an integer so you probably have a never-ending loop.

如果$sites_array是一个数组,那么将它与一个整数进行比较是没有意义的,因此您可能有一个永无止境的循环。

If you just need the first 10 elements in another array, you can replace your loop with:

如果您只需要在另一个数组中使用前10个元素,您可以将循环替换为:

$topten_sites = array_slice($sites_array, 0, 10);

#2


2  

Why would You iterate entire array if You only want first 10 results?

如果只想要前10个结果,为什么要迭代整个数组?

    for ($i = 0; $i < 10; $i++) {
            $topten_sites[] = $sites_array[$i];
    }

#3


1  

To answer the actual answer; code never stops working "for no reason". Code works or it doesn't, both for a reason. If it stops working something changed compared to your previous tests. "Sometimes it works, sometimes it doesn't" falls in the same logic. Code will always behave exactly the same every time, just some of the parameters have changed, you have to find which one.

回答实际回答;代码永远不会“无缘无故地”停止工作。代码可以工作,也可以不工作,这都是有原因的。如果它停止工作,与之前的测试相比,有些东西会发生变化。“有时行得通,有时行不通”,都属于同一逻辑。每次代码的行为都会完全相同,只是一些参数发生了变化,你必须找到哪个参数。

In your case, i'm guessing the entries in your array have increased. PHP and arrays aren't best friends when it comes to speed, arrays are slow. It could very well be that your array was smaller when you tested it (wasn't probally the fastest to begin with), but now with the current amount it just hit the threshold of 30 seconds.

在你的例子中,我猜你数组中的条目增加了。当谈到速度时,PHP和数组不是最好的朋友,数组是慢的。很有可能你的数组在测试时更小了(并不是最开始的速度),但是现在有了当前的数量,它就达到了30秒的阈值。

It could also be that a part of code before this bit of code takes a lot of time (say suddenly 28 seconds instead of 20), and your loop (which never changed) does it's job in the regular 3seconds it always does, now runs into problems

也可能是代码的一部分在这段代码之前花费了大量的时间(比如突然28秒而不是20秒),而你的循环(它从未改变)在通常的3秒内完成了它的工作,现在遇到了问题

#4


0  

Use it like this:

使用它是这样的:

    $topten_sites = [];
    for ($i = 0; $i <= 10; $i++) {
        $topten_sites[] = $sites_array[$i];
    }
    $topten_sites = collect($topten_sites)->sortByDesc('number')->all();

#1


15  

This looks wrong:

这看起来是错误的:

    for ($i = 0; $i <= $sites_array; $i++) {
        if ($i < 10) { // this is 270
            $topten_sites[] = $sites_array[$i];
        }
    }

If $sites_array is an array, it makes no sense to compare it to an integer so you probably have a never-ending loop.

如果$sites_array是一个数组,那么将它与一个整数进行比较是没有意义的,因此您可能有一个永无止境的循环。

If you just need the first 10 elements in another array, you can replace your loop with:

如果您只需要在另一个数组中使用前10个元素,您可以将循环替换为:

$topten_sites = array_slice($sites_array, 0, 10);

#2


2  

Why would You iterate entire array if You only want first 10 results?

如果只想要前10个结果,为什么要迭代整个数组?

    for ($i = 0; $i < 10; $i++) {
            $topten_sites[] = $sites_array[$i];
    }

#3


1  

To answer the actual answer; code never stops working "for no reason". Code works or it doesn't, both for a reason. If it stops working something changed compared to your previous tests. "Sometimes it works, sometimes it doesn't" falls in the same logic. Code will always behave exactly the same every time, just some of the parameters have changed, you have to find which one.

回答实际回答;代码永远不会“无缘无故地”停止工作。代码可以工作,也可以不工作,这都是有原因的。如果它停止工作,与之前的测试相比,有些东西会发生变化。“有时行得通,有时行不通”,都属于同一逻辑。每次代码的行为都会完全相同,只是一些参数发生了变化,你必须找到哪个参数。

In your case, i'm guessing the entries in your array have increased. PHP and arrays aren't best friends when it comes to speed, arrays are slow. It could very well be that your array was smaller when you tested it (wasn't probally the fastest to begin with), but now with the current amount it just hit the threshold of 30 seconds.

在你的例子中,我猜你数组中的条目增加了。当谈到速度时,PHP和数组不是最好的朋友,数组是慢的。很有可能你的数组在测试时更小了(并不是最开始的速度),但是现在有了当前的数量,它就达到了30秒的阈值。

It could also be that a part of code before this bit of code takes a lot of time (say suddenly 28 seconds instead of 20), and your loop (which never changed) does it's job in the regular 3seconds it always does, now runs into problems

也可能是代码的一部分在这段代码之前花费了大量的时间(比如突然28秒而不是20秒),而你的循环(它从未改变)在通常的3秒内完成了它的工作,现在遇到了问题

#4


0  

Use it like this:

使用它是这样的:

    $topten_sites = [];
    for ($i = 0; $i <= 10; $i++) {
        $topten_sites[] = $sites_array[$i];
    }
    $topten_sites = collect($topten_sites)->sortByDesc('number')->all();