PHP Fopen Speed

Dear Experts,

I am building a crm system for a client, and I am just doing some basic template management & design.

When including these templates I currently have a function that fopens the file (with read header) then fread's the contents, and replaces what it needs to replace, then outputs that to the requester.

Now this is fine, however some elements inside those pages require other template files to be included for example the search box appears everywhere so I use a template for it. Now my script currently seperately fopens the file every time that search box needs to be shown.

Is that an OK way of doing it, or am I just wasting I/O's and, should I just use a database and put all the much repeated sections in there?

Or should I, in fact use a file containing certain functions that would just display the output from a variable inside that very file?

Cheers for your responces.

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

If you're replacing with the same the same values over and over again, you'd be better off caching the final result. Make a temporary directory called "cache" (or something like that) and before you do a replacement, check to see if there's a cached version available. If not, create one after you do your replacements. If there are different ways of doing the replacements, then just name your cached files in such a way that you can identify which version each is.
You really should be using output buffers for this, you'll find it much easier to work with and allows you to 1) use a regular include and 2) use variables within the script your including.
include "file.php";
$contents = ob_get_clean();

Open in new window

A note to animecyc's suggestion - if you do include it, just be aware that you're actually RUNNING that PHP file and capturing the output. So if your PHP script does anything that might make changes to the database or has any sort of significant loading time, then using fopen would be better. Otherwise, if you don't mind the PHP executing, then output buffering would be a good way (caching would still be a good method, no matter which way you go)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.