Forum Moderators: coopster
define('VAR1','Shipping and Ordering Information');
My question is, when you have short sections of HTML code (say like 7-15 lines) that are used repeatedly, for example:
<tr>
<th>Description</th>
<th align="center">Part Number</th>
<th>Quantity 1-9</th>
<th align="center">Qty 10 or more</th>
<th align="center">Shopping Cart</th>
</tr>
.... is it better store to it as a constant in one file with many constants OR in it's own separate include file? I would be including either method using PHP include. I guess what I am really saying is, what is the limit for what you want to hold in a constant?
Welcome to WebmasterWorld!
With a big file of verbose constants, PHP has to parse & load all of the text for every constant into memory regardless of whether the request uses zero, one or more of the constants. If you break them out into includes, then you can load them on demand, taking that hit only when you need to.
All that said, a file of big constants is probably not a very big deal, performance-wise. You're probably safe going with whichever one is easier to maintain.
I personally use functions to handle "text macro"-type insertions. Something like <?= qtyTableHeader()?>. In the function definition you're free to do an include, return literal text, or use whatever strategy works best for the performance you need. And if you change that strategy, you don't need to update it anywhere but the function definition. Everywhere that you're using <?= qtyTableHeader()?> will automatically pick up the benefits without any changes needed.
If I understand you correctly constants are always processed/parsed but functions are only called/loaded when they are needed.
Correct, ensure you use include rather than require (providing you're using PHP).
I have a site of approx 5000 pages which run on a total of 5 functions. The entire sites code is probably around 70 lines. With a bit of caching the site can take one hell of a hammering :)
As for require (as opposed to include), I definitely wouldn't want to use it and get a fatal error that stops my page from loading.
Bloke, for your 5000 page site do you go dynamic and just use one template file or static with a bunch of includes?
That just checks that 'latestwidgets' is actually a section on the site and serves up the correct page. If it doesn't exist then it serves up a 'proper' 404 page.