View Full Version : Will using a custom "template" (HTML Page) impact performance?

02-02-2010, 06:02 AM
By Performance, I mean speed + memory load.
Template denotes some sort of file that is to be eval'd


<div id="content">
<h2 class="title">|title|</h2>
<div class="post">|post|</div>

Which in turn would be be obtained via something like file_get_contents(), regular-expression-replaced to {$title} and such, and eval'd to output a basic HTML page.

So my question is, will this method be have a large performance impact on a server vs a normal <?php echo $title; ?> or not? In what circumstances will this cause the server's performance to drop (i.e. will lots of visitors cause it to decrease or what?)

02-02-2010, 01:19 PM
It will have an impact, but nothing major unless you are running it on a crappy shared host with thousands of users accessing it

02-02-2010, 01:58 PM
By regular-expression-replacing you mean preg_replace() ? I would advise to replace with str_replace() (http://php.net/str_replace). String functions are faster than regexp functions.

Of course the patterns to replace would need to be unique enough not to be met in the common page text by any chance (str_replace() will replace all strings it finds with the given replacement string).

Also you could submit arrays to str_replace() (http://php.net/str_replace) to replace many patterns with 1 function call.

Edit: Just in case: Also you said you would eval it to output a basic HTML page. I hope you would not use the function eval() ... There is no need for it - you could simply echo the resulting string after replacement. Also if eval() is used, it is necessary to be absolutely sure no one could attack the system by injecting some code. Normally I would use eval() only if I was sure it was absolutely safe and I was sure there was no way to avoid using this function.