简体   繁体   中英

function exceeds maximum execution time

I have this function and most of the time when I run this function the following error occurs:

Fatal error: Maximum execution time of 30 seconds exceeded

The location (line) of the error is always different.

And this is the function:

            function selectAdsY(){
                //Put all the ads in the arrays.    

                global $ads;
                global $adsX;
                global $adsY;
                global $Min;
                global $ImpIfriends;
                global $ImpLivejasmin;
                global $ImpExo;
                global $ImpND;
                global $ImpAmazon;

                // Change these 
                $ImpIfriends = 20;
                $ImpLivejasmin = 10;
                $ImpExo = 0;
                $ImpND = 20;
                $ImpAmazon = 20;

                $i = 0;
                //ifriends
                    global $ifriends;
                    global $ifriendsX;
                    global $ifriendsY;
                    $iframeCountFriends = count($ifriends) - 1;
                    $a = rand(0, $iframeCountFriends);

                $ads[$i] = $ifriends[$a];
                    $adsX[$i] = $ifriendsX[$a];
                    $adsY[$i] = $ifriendsY[$a];
                if($ImpIfriends != 0){
                    $Min[$i] = min($ifriendsY);
                }

                //livejasmin
                    global $livejasmin;
                    global $livejasminX;
                    global $livejasminY;
                    $iframeCountLivejasmin = count($livejasmin) - 1;
                    $a = rand(0, $iframeCountLivejasmin);
                $i++;
                $ads[$i] = $livejasmin[$a];
                    $adsX[$i] = $livejasminX[$a];
                    $adsY[$i] = $livejasminY[$a];
                if($ImpLivejasmin != 0){
                    $Min[$i] = min($livejasminY);
                }
                //exo   
                    global $exo;
                    global $exoX;
                    global $exoY;
                    $iframeCountExo = count($exo) - 1;
                    $a = rand(0, $iframeCountExo);
                $i++;   
                $ads[$i] = $exo[$a];
                    $adsX[$i] = $exoX[$a];
                    $adsY[$i] = $exoY[$a];
                if($ImpExo != 0){
                    $Min[$i] = min($exoY);
                }

                //nasty dollars
                    global $ND;
                    global $NDX;
                    global $NDY;
                    $iframeCountND = count($ND) - 1;
                    $a = rand(0, $iframeCountND);
                $i++;   
                $ads[$i] = $ND[$a];
                    $adsX[$i] = $NDX[$a];
                    $adsY[$i] = $NDY[$a];
                if($ImpND != 0){
                    $Min[$i] = min($NDY);
                }

                //amazon.com
                    global $amazon;
                    global $amazonX;
                    global $amazonY;
                    $iframeCountAmazon = count($amazon) - 1;
                    $a = rand(0, $iframeCountAmazon);
                $i++;   
                $ads[$i] = $amazon[$a];
                    $adsX[$i] = $amazonX[$a];
                    $adsY[$i] = $amazonY[$a];
                if($ImpAmazon != 0){
                    $Min[$i] = min($amazonY);
                }
            }

I really have no clue what could be wrong...

EDIT: extending the time limit is not a solution in this case, this code can and should always be executed within a few miliseconds.

Increase the time limit of your PHP Script

<?php
set_time_limit(0);

You may have no error but the error mean you reached the default max_execution_time.

You can update this value.

Use this:

 ini_set('max_execution_time', 300); //300 seconds = 5 minutes

Give time in second, whatever you whant

Or

set_time_limit(0); //no limit

0 - Means no limit

Example:

<?php

set_time_limit(20);

while ($i<=10)
{
        echo "i=$i ";
        $i++;
}

?>

To limit the maximum execution time use set_time_limit($seconds) . If it is set $seconds to zero, no time limit is imposed on it . So just try to add set_time_limit(0) at the beginning of your script and script will work till the end. But if user's browser disconnects due to browser timeout your script could be halted, so you need to add ignore_user_abort(true) at the beginning of the script to ignore it and work exactly till the end of the script

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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