I am adding meta tags, titles, stylesheets and scripts to each page via PHP - but all separate from page to page (since i have a header.php that is universal) I am still on the fence though of what is faster for 20+ pages. I could easily store all my values and such in a multidimensional array and return the whole array, then distribute each value accordingly or I can query a database but really which would give me the fastest result? Would it really be that much slower to even matter? Would the user even notice? All questions I have no answers to what do you guys think?
-
8Pick whichever one is easiest to maintain and use. Until you are getting a huge number of hits I doubt either choice will affect your site. Remember, premature optimization is the root of all evil.GWW– GWW2011-06-29 04:25:46 +00:00Commented Jun 29, 2011 at 4:25
-
1@GWW I prefer to think it's only 97% evil ;-)user166390– user1663902011-06-29 04:29:41 +00:00Commented Jun 29, 2011 at 4:29
-
1Array, always. If you go with a database, you have to wait for it to load stuff into memory for you. A database is only really necessary when you have lots of data (over 100 MB or so), otherwise your memory would get too full.beatgammit– beatgammit2011-06-29 04:29:54 +00:00Commented Jun 29, 2011 at 4:29
2 Answers
I wouldn't bring a database into the mix for a couple dozen pages - not unless you've got a bunch of content you need to store in there. Rather, what I would do is build the array on each page, then include the template:
<?php
$page = array(
'title' => 'Index Page',
'styles' => array( 'reset.css', 'text.css', '960.css' ),
'scripts' => array( 'jquery.js' )
);
include('header.php');
?>
<p>Page content here<?p>
<?php
include('footer.php');
?>
Within your header.php file, have the following:
<title><?php echo isset($page['title']) ? $page['title'] : 'Default' ; ?></title>
<?php foreach ( $page['styles'] as $style ) : ?>
<link rel='stylesheet' href='<?php echo $style; ?>' />
<?php endforeach; ?>
This may be suitable for much of what you're doing, but when things start to get heavier - definitely put more long-term stock in a database solution.
1 Comment
Neither are nearly as fast as using the filesystem -
1) by separating content into named files identifiable by the client you can take advantage of HTTP's caching mechanisms
2) by not requiring the request to be routed via your application logic (or app logic + database) the I/O operation will have less overhead and therefore be faster.
(note Jonathan Sampson's answer uses a hybrid approach of storing the data in the filesystem but with option of selectively presenting different groups to the browser).
There are other times when you have to manipulate arbitrary data structures within your application where filesystem storage is not appropriate and where you might want to think about the performance of arrays vs databases. IME, PHP arrays are not always faster than DBs - and as the size of the data pool increases, a database becomes massively more efficient (effectively the database has greater bandwidth combined with greater latency). The only to know for sure which is faster / more scalable is to measure it - but if your dataset is less than, say, 100 items then the array will probably be faster.