Does using the include_once() command make page load time longer?

Discussion in 'PHP' started by Imozeb, Feb 12, 2010.

  1. #1
    I was thinking about using the PHP include_once() command for repetitive elements on my pages i.e. (header, navbar, sitemap) but I was wondering if using PHP's include_once() command will make my pages load time go up by alot compared to just using plain html.

    Does it make my page load time go up by alot or is it just a negligible increase so that the (changing just one file to make edits outweighes the load time increase)?

    And is using PHP include_once() command the best and easiest way to create just one file which can make edits to multiple pages?

    Thanks!

    ~imozeb :D
     
    Imozeb, Feb 12, 2010 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    The time is not going to be noticable unless you have a lot of different files being included. If you write your appliacations in a smart manner you should not ever need to use the _once sister functions of include and require
     
    JAY6390, Feb 12, 2010 IP
  3. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #3
    So what your saying is if I have only like 5 files each only about 1-2 kilobytes, there won't be much of a time difference?
     
    Imozeb, Feb 12, 2010 IP
  4. JoeWJ

    JoeWJ Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Well I have a PHP website that uses a lot of PHP include once commands and I don't find that it's any slower then a html website.
     
    JoeWJ, Feb 12, 2010 IP
  5. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #5
    It all adds up. If your site has any serious amount of traffic, then the miniscule delays from each small, avoidable non-best practise will start to compound and make the whole thing perceptibly slower.

    If you can use include() instead of include_once() you'll definitely be better off.
     
    SmallPotatoes, Feb 13, 2010 IP
  6. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    So what processes the PHP script? Is it the user's browser, or my mysql server, or is it the server that I put my PHP pages on?

    And what is the difference between include() and include_once(). I thought that the only difference was that include_once() only executes once per page which shouldn't be much different or slower than include() right??

    I'm so confused!!! :confused:
     
    Imozeb, Feb 13, 2010 IP
  7. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Your Apache server processes it. MySQL has nothing to do with the PHP processing. They are two separate entities. The processing occurs on YOUR server not the client browser, hence the preprocessor (processes before sending the output to the client)

    As for the difference, include will include a file more than once if needs be. For instance if I have a file called functions.php and I include it twice, then I could produce errors if there are functions in it as it will try to define a function twice, which isn't allowed

    functions.php
    <?php
    function foo() {
        // Code here
    }
    PHP:
    Then it's called in your index.php file

    index.php
    <?php
    include 'functions.php';
    include 'functions.php';
    PHP:
    This isn't normally how they get called twice (usually it's an included file including another file which has already or will be included) however it demonstrates the point. Using include_once would eliminate the possibility of this happening
     
    JAY6390, Feb 13, 2010 IP
    Imozeb likes this.
  8. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    So then how should I go about making one file (which contains table layout, PHP and Javascript functions) be able to be used across many pages so that I only have to change that one file to make site-wide edits?
     
    Imozeb, Feb 13, 2010 IP
  9. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Use include() instead of include_once() if at all possible. If you don't have cross-includes (A includes B, B includes C, C includes A) then include() should be fine.

    include_once() is in fact quite a bit slower. This is because it has to do some potentially complex and expensive checks (a lot of OS stat() calls, mostly) to determine whether the file being included is in fact the same as any other file that was already include_once()d. Due to symlinks and .. paths, it can't just compare the provided argument outright and be done with it.
     
    SmallPotatoes, Feb 13, 2010 IP
  10. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Thanks SmallPotatoes. And thanks to everyone else who answered my questions.

    ~imozeb :)
     
    Imozeb, Feb 13, 2010 IP
  11. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #11
    I mostly agree with the above.

    It is quicker, to use include, but not always. If there is a lot of sloppy coding, or just accidental including of the same file many times, include will be slower than include_once.
     
    jestep, Feb 15, 2010 IP