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?
are these menus are user dependence (different menus for different users)? if not you can just use to write the menu structure to plain txt file and regenerate it when you make some changes on your menu structure
Yeah well said pluswebdev. The problem with that approach Echilon is that the menu needs to be generated for each user. When in reality its probably better to generate it only once for all users. I dont particularly like storing information like this as a session variable. Try storing it to disk or better yet chuck it into memcached if you are able to install it on the server.
Each user will see a different menu. I could cache it to a file for each user, but that would probably generate more overhead than regenerating it on every page load.
What aspects of the menu will be different? If its something like a user name you can store the menu once on disk, and just the name in a session variable. That way when you cache the menu you can place the text #name# inside and replace that with the session variable everytime you load the page. I highly recommend memcached though, zero CPU load and minimal overhead. Good Luck