Kael Posted July 12, 2006 Report Share Posted July 12, 2006 Hi again everyone!I'm completely absorbed in a new web project, and it recently ocurred to me that i don't know the memory taxations of some of my techniques.What I've been doing is building the entire output into variables, then echoing it at the very end of the script. Example:<?php //include_me.php//add to existing variables from main.php, depending on passed valuesif ($_GET['page'] == 'something') {$body .= 'some gathered content from another place...';$left .= 'some menus';$title = 'document title';} elseif...?><?php //main.php//build initial variables - stuff that will appear on every page.body = 'Welcome to this site!<br />';left = 'Here is a menu that will be on every page.';@include 'include_me.php';//echo contentecho '<!DOCTYPE...<html> <head> <title>'.$title.'</title> </head> <body> <div id="left">'.$left.'</div> <div id='body">'.$body.'</div> </body></html>';?>I really like doing it this way because i'm able to do things out of order - which typically results in many less database calls - as well as being able to use the same output twice (which is rarely necessary, but does come up here and there). Also, I tend to have fewer pages this way, as i can put down as much other code as i want between sections (e.g., "left" and "body") without having to include multiple content files.However, what I've begun to wonder is if all that hanging on to stuff in variables is eating up memory. some of the pages are outputting as much as 28Kb, most of which is built to memory, then outputted via the aforementioned method.Now that I consider it, the capacity of PHP probably far excedes 28K... Anyway, if anyone has any comments, please do offer them.thanks tons!-love kael Quote Link to comment Share on other sites More sharing options...
yitzle Posted January 5, 2007 Report Share Posted January 5, 2007 This method should be perfectly fine.THe output and memory use (RAM) are unrelated. Quote Link to comment Share on other sites More sharing options...
ɹəuəllıʍ ʇɐb Posted January 5, 2007 Report Share Posted January 5, 2007 I do not know PHP, nor have I ever used it. But the principle is the same anywhere - when is the memory allocated (probably when the variables are defined), and when does the memory get released?The important part is the timing of the release. If memory usage build up and up, without ever releasing things - that will make things bad for the user.Naturally, allocated but unused memory gets paged out to the virtual memory file, but still if the user observes the browser's memory usage going up and up and up, this will at the end upset some users.Just make sure that allocated memory gets freed at some point, and all is well.P.S. avoiding repeated DB calls by storing data in variables (or tables) sounds like a very commendable thing to do! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.