Jump to content

More PHP!


Kael
 Share

Recommended Posts

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 values
if ($_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 content
echo '
<!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

Link to comment
Share on other sites

  • 5 months later...

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!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. Privacy Policy