I have a script which uses a large amount of IF statements to check the contents of variables and generate HTML. These variables are loaded from a database, and don't change during the session. I'm wondering whether it would be considered bad practice to generate the HTML at the start of the session, then cache say 20Kb or so in a session variable.
It's actually for a dynamic navigation bar. The bar would only need to be generated once, then just echoed from the saved SESSION variable. Would this cause PHP to take a performance hit at all?
Session variables to cache HTML
Moderator: General Moderators
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
Sounds reasonable enough to me, though using templates is the typical solution to this kind of issue. have you considered either writing either a dead-simple template engine* or using one of the pre-built ones out there? Smarty is popular, though I've never used it personally.
*a session array & str_replace() could be considered a template engine, it doesn't have to be brain surgery.
*a session array & str_replace() could be considered a template engine, it doesn't have to be brain surgery.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Re: Session variables to cache HTML
PHP has to load the session data anyway. Go for it.Echilon wrote:Would this cause PHP to take a performance hit at all?