[prog] Using PHP to create the static part of HTML pages
Allie Micka
allie at pajunas.com
Wed May 11 00:00:51 EST 2005
I'm not exactly sure what you're asking, but I think the answer is "Use
PHP". If your question is basically along the lines of "should I name
my files .php or .html"?, my experience is that you're going to be much
happier with .php in the long run.
I tested this out once by taking a plain old html file and running
benchmarks on it. I then renamed it to .php so that it would invoke
the preprocessor and I ran benchmarks again. I saw a 3-5 millisecond
performance hit. This is *negligable* Your mileage may vary, but if
you have apache running somewhere you can use its included ab tool to
run the same tests.
Now, once you have access to the preprocessor on every page, you can
make use of php whenever you like, without ever having to rename
anything. I couldn't contemplate building a site without using some
form of templating to keep the design and navigation consistent. By
templating, I might even use something as basic as include files or a
full-on templating engine like Smarty.
If you're going to handle logins and sessions, you may wish to enforce
a timeout that logs users out after so many minutes of inactivity. In
order to do this, you'll need to check their session on each page view.
This means having php available on each page view.
The one performance hit you may find is that PHP by default disables
cache headers. Because each page is a program, the output may be
different on every request. So to play it safe, the preprocessor
disables HTTP caching and prevents local copies on proxy servers and
visitors' hard drives. Google around for HTTP caching and look into
the header() function if this bugs you.
So the other question you may have been asking is whether to switch in
and out of interpreter mode by using <? ?>'s, or whether you should use
echo statements. It's faster to close out of the interpreter with a
?>, output your html (e.g. a form) and then go back into your program
with a <?
So
<?
//do php stuff
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<!--form stuff-->
</form>
is better than
<?
//do php stuff
echo '<form action="'.$_SERVER['PHP_SELF'].'" method="post">';
echo '<!--form stuff-->';
echo '</form>';
?>
There are gray areas with this rule, change for
readability/maintainability at your discretion.
> Is there any performance hit for doing this?
>
> For example I could convert some of these pages back to plain HTML
> once I've finalized things, however its easier to maintain in PHP at
> the moment.
>
> For example the login page could be HTML, but as soon as its submitted
> it would have to run the PHP, and if the login failed I would have to
> generate that login page again anyway...
Allie Micka
pajunas interactive, inc.
http://www.pajunas.com/
scalable web hosting and open source solutions
More information about the Programming
mailing list