简体   繁体   English

为什么我的PHP代码不再无缘无故了?

[英]Why does my PHP code doesn't work anymore for no reason?

I have a for loop in my code. 我的代码中有一个for循环。 I haven't changed anything on this part of code for about 5-6 days and I never had problems with it. 我在这部分代码上没有改变任何东西大约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数组的每个其他行,代码将再次运行。

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);

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];
    }

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. PHP和数组在速度方面不是最好的朋友,数组很慢。 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. 当你测试它时,你的阵列很可能是较小的(开始时不是最快的),但现在使用当前的数量它只是达到了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秒内完成它的工作,现在运行陷入困境

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM