Jump to content

Recommended Posts

 

I have been working on a plugin system for a while now, and as I get farther along I am starting to notice WAY too many files being included (try 83) just to render a blog post. Now it is not that I have 83 different files that NEED to be included - but I have several that are included REPEATEDLY in order to render a page.

 

Files like "comments.php" that is included every time a comment is placed in the layout (imagine a post with 100 comments!). Here is an example of what is going on in my script:

 

<?php

    ...(CODE)...
    
    while ($row = mysql_fetch_assoc($result)) {
    
        //Include the file that will place the variables in the HTML correctly...
        include("comments.php");
        
    }

    ...(CODE)...

?>

 

 

and then comments.php looks like this:

 


<h2><?php print $row['title']; ?></h2>
<div><?php print $row['text']; ?></div>

 

 

So I want to know what you thing about maybe doing something like instead of including the same file over and over - just include it once and use str_replace() to enter the new values for each comment. But I am not sure how I would do that. ;)

 

Or is there another way I could handle this problem? The two most important things are speed and I would like to still be able to use PHP code in the files like "comments.php".

 

 

Link to comment
https://forums.phpfreaks.com/topic/71869-only-include-files-once-to-save-time/
Share on other sites

 

A friend just recommended that I try placing the comments.php code in a function that can be included once but called any number of times! So comments.php would look like this:

 


function place_data_in_comment($row) {
    <h2><?php print $row['title']; ?></h2>
    <div><?php print $row['text']; ?></div>
}

 

Does anyone know of any problems with this approach?

 

Its good to braking you code into smaller chunks but I really think your taking it to the extreme.

 

lol that is true  ;D

 

However, the examples shown are simplified versions of their real-life counterparts. Plus, I can't hard-code a file like 'comments.php' into a file, because not only is it a template file that designers need to edit - it is also a plugin that isn't a part of the system core.

 

But I get your drift.  ;)

 

 

 

 

Well, I have run some tests and I found that including a file once and then running a function to place the code is about 5x faster than including the file over and over.

 

If you want to run the test yourself I have attached a zipped version of the 3 files I used to test with - upload them to the same place a run "index.php".

 

 

 

[attachment deleted by admin]

You could make it a variable. The variable is set once and then referenced X times -

 

<?php

    ...(CODE)...
    
    while ($row = mysql_fetch_assoc($result)) {
    
        //Include the file that will place the variables in the HTML correctly...
        include("comments.php");
        $title =  $row['title']; 
    }

    ...(CODE)...

?>

 

 

Depending on what you have in $result (obviously has to include row title) and then comments.php looks like this:

 

<h2><?php '$title'; ?></h2>

 

Saves you from performing the same row query many times. Could make the comment var i++ for 'X' for id and get for str() to give each var a unique id. So -

 

$commentX = string

When coding in C you will often see this type of thing in header files.

 

Pretend this file is called header.h.

 

#ifndef HEADER_H
#define HEADER_H

// all the header code

#endif

 

This ensures that if a header is included more than once, the code only sees the light of day once.  You could do the same thing with PHP like so.

 

<?php

if(!defined(HEADER_INC_PHP))
{
  define('HEADER_INC_PHP', true);
  
  // all your header code

}
?>

<?php

    ...(CODE)...
    
    while ($row = mysql_fetch_assoc($result)) {
    
        //Include the file that will place the variables in the HTML correctly...
        include("comments.php");
        
    }

    ...(CODE)...

?>

 

Using includes like this is fundamentally wrong.  You want something like this.

 

<?php
include("comments.php");

    ...(CODE)...
    
    while ($row = mysql_fetch_assoc($result)) {
    
        //Include the file that will place the variables in the HTML correctly...
        this_is_a_function_defined_within_coments_php();

    }

    ...(CODE)...

?>

Also another important note, in the future remember that the server slows down when you import many files.  So your example was a 100 comments.  Well that is going to import comments.php 100 times as you know.  I would hate to imagine how many files you import on each execution of the code.  Speed and efficient code is so important especially when you systems become large.

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • 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.