Skip to content
WJunction - Webmaster Forum

PHP takes uber long, fatal error

Status
Not open for further replies.
Heres my error:
Fatal error: Maximum execution time of 60 seconds exceeded in C:\WAMP\www\Template\inc\templateClass.php on line 24

I'm assuming its because my php script takes too long, heres the relevant bit:

PHP:
    function parseIncludes(){
        $go = 1;
        while($go == 1){
            $result = preg_match('/{[^}]+}/', $this->template, $matches);
            if(isset($matches[0])){
                if($matches[0]){
                    $namear = explode('{', $matches[0]);
                    $name = substr_replace($namear[1] ,"",-1);
                    $file = 'templates/' . $name . '.tpl';
                    $includefile = file_get_contents($file);
                    $this->template = str_replace('{' . $namear[1], $includefile, $this->template);
                   
                }
                else{
                    $go = 0;
                }
            }
        }
    }
Any ideas?
 

9 comments

your checking for $matches 2 times, and the inner one is the one that sets $go to 0,

PHP:
function parseIncludes(){
        $go = true;
        while($go)
        {
            $result = preg_match('/{[^}]+}/', $this->template, $matches);
            if(isset($matches[0])){
               $namear = explode('{', $matches[0]);
               $name = substr_replace($namear[1] ,"",-1);
               $file = 'templates/' . $name . '.tpl';
               $includefile = file_get_contents($file);
               $this->template = str_replace('{' . $namear[1], $includefile, $this->template);
            }else
            {
                $go = false;
            }
        }
    }

its like

PHP:
while(true)
{
    if(false)
    {
        if(true)
        {
           //Code
        }
        else //if the first if is false, this if/else is never run.
        {
            break 2;
        } 
    }
}
 
the while is infinite, i cant say why, ill try look at it and maybe tell you later

EDIT:
litewarez is right :)
 
You should also be aware of the fact parsing your tags with regex's will make the scrip very resource intensive. Especially if you don't implement your own caching system. You might wanna build a small state machine and parse it per character.
 
Caching is pretty simple to be honest

After your compiler has finished compiling the source, you need to store the the contents in a cache directory with a name that can easily be read such as index_php_43hdj8347ytjd837yt.cache

then before your compiler runs for lets say index.php

you can just check to see if its cached and load it, with some other checks like clear files that are 1 hour old, so when the compiler tries again, it wont be cached so continue to compile and then cache again.

PHP:
$Template = new Tempalte('index');

if($Template->isCatched())
{
    $contents = $Template->GetCachedVersion();
}else
{
     $Template->assign("whatever","some_value");
     //Template
     $contents = $Template->CompileAndCache();
}

$Template->FlushCatch(); //Delete files older than 3600s / 1 hour

echo $contents;

You get the idea
 
Status
Not open for further replies.

About the author

T
Active Member · Joined
Picked up photoshop about half a year ago. Now i do graphics design :) Graphics/Web Design
885
Messages
0
Reactions
16
Points

Advertise on WJunction

Reach 1000's of webmasters, hosts & affiliates. Banner & sponsored-thread slots available.

Contact us
Back
Top Bottom