Thursday, April 19, 2012

Is PHP"s include resource-expensive (particularly during iterations)?


Does PHP cache include requests? I was wondering how to clean up my code and I thought about using a bit more includes . Consider the following scheme.




[foreach answer] [include answer.tpl.php] [/foreach]



This would require to include answer.tpl.php hundreds of times.



Does it cache? Will it have a worth-considering affect on performance? Is that considered a good practice? Bad?



In response to @Aaron Murray answer



No, that won't work. The mere concept of _once is to prevent including the same file more than once. (to prevent errors caused by e.g. overwriting constant values)



Practical example would look like this:




# index.php
<?php
$array = array('a', 'b', 'c');

$output = '';

foreach($array as $e)
{
$output .= require_once 'test.php';
}

echo $output;

# test.php
<?php
return $e;


Source: Tips4all

3 comments:

  1. Does PHP cache include requests?


    As far as I know, PHP does not cache includes by default. But your underlying filesystem will likely do this. So accessing the same file over and over again as in your example should be quite fast after all.

    If you run into actual problems, you would first need to profile the application to find out where the actual bottleneck is. So unless you did not run into any problems yet, I would consider using the include not harmful.

    Regarding the good practice, I think this is fairly well explained in this article: When Flat PHP meets Symfony.

    Making your code a bit more re-useable

    This is no high design here, it's just to show the picture how you can start to make things more modular. You should be able to take the code 1:1 from your include file, just take care all needed template variables are passed into the function (don't use globals for that, it will stand in your way sooner or later):

    # in your answer.tpl.php

    function answer_template(array $vars) {
    extract($vars);
    ... your template code ...
    }

    # your script

    array = array('a', 'b', 'c');

    $output = '';

    require('answer.tpl.php');

    foreach($array as $e)
    {
    $output .= answer_template(compact('e'));
    }

    echo $output;

    ReplyDelete
  2. Have you considered:

    require_once('answer.tpl.php')
    or
    include_once('answer.tpl.php')


    Of course then you could include the 'required' files only in the scripts that you want them included in (only where they are really required).

    Edit: Revamped answer:

    index.php ->

    require_once('answer.php');
    echo answer(); // This function can be called from anywhere in the scripts.


    answer.php ->

    function answer() {
    return '<div>This is the answer</div>';
    }


    Also on a side note you could use output buffering in your function to capture HTML (slightly cleaner method for separating HTML and php) within your answer.php.

    In response to your example above:

    index.php

    <?php
    require_once('test.php');
    $array = array('a', 'b', 'c');
    $output = '';
    foreach($array as $e)
    {
    $output .= test($e);
    }
    echo $output;


    test.php

    <?php
    function test($param) {
    return $param;
    }

    ReplyDelete
  3. This topic came up before, so here are some potential duplicates (not endorsing any; mostly partial answers, but relevant read nevertheless):


    cost of "include" in PHP?
    Is it bad to include a lot of files in PHP like it is for file based Sessions?
    Will reducing number of includes/requires increase performance?
    Why is require_once so bad to use?
    Which is better performance in PHP?


    Well, none of those answers your question specifically. We had a little performance benchmark somewhere, but I can't find it.

    (Personally I often do orginize my code to merge the whole application into a single blob file, then strip whitespace, to avoid multiple file accesses, even if APC is there.)

    ReplyDelete