SitePoint Sponsor

User Tag List

Results 1 to 14 of 14

Thread: including files

  1. #1
    SitePoint Evangelist Alchemist's Avatar
    Join Date
    Aug 2006
    Posts
    412
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    including files

    Hello,

    wanted to refresh a couple of things regarding including files with php

    PHP Code:

    switch($c)
    {
    case 
    '1': include('test1.php'); break;
    case 
    '2': include('test2.php'); break;
    case 
    '3': include('test3.php'); break;

    if $c = 2, test1.php and test3.php will not be processed at all, correct? what about if you use require instead?

    thanks

  2. #2
    masquerading Nick's Avatar
    Join Date
    Jun 2003
    Location
    East Coast
    Posts
    2,215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Correct.

    If you use require, test1 and test3 will still not be processed. The difference between include and require is that if the script is looking for test2.php and can't find it, include will throw a warning, while require will kill the script.

    Also consider using include_once or require_once - that way, if for some reason the file has already been included previously, php won't include it again (which may, depending on the contents of the file, be a bad thing.)

    And if those files are necessary for your script to run, you should probably use require.
    Nick . all that we see or seem, is but a dream within a dream
    Show someone you care, send them a virtual flower.
    Good deals on men's watches

  3. #3
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Correct.
    Require() is the same as Include(), with the exception that require(), as it's name suggests, requires the file to load to continue.

    This means that a fatal error in the require()d file stops the main file from continueing, same if it doesn't exist.

    Edit:

    Alas, I have been beaten to it!
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  4. #4
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Location
    Bristol, England
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Possibility View Post
    Also consider using include_once or require_once - that way, if for some reason the file has already been included previously, php won't include it again (which may, depending on the contents of the file, be a bad thing.)
    include/require_once has huge overheads compared to simply including or requiring.

  5. #5
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yeah - it's best to never allow yourself into a situation where you're require() a file more than once - if you have a class-based file-system is the best way out of it.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  6. #6
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Alchemist View Post
    Hello,

    wanted to refresh a couple of things regarding including files with php

    PHP Code:

    switch($c)
    {
    case 
    '1': include('test1.php'); break;
    case 
    '2': include('test2.php'); break;
    case 
    '3': include('test3.php'); break;

    if $c = 2, test1.php and test3.php will not be processed at all, correct? what about if you use require instead?

    thanks
    The break keyword ensures that if a case evaluates to true, that the remaining cases in the switch statement will not be executed or evaluated. The rest of the comments here on how include and require operate are correct, but the reason that there is no "fall through" in that code is due to the break keyword.
    Visit my blog
    PHP && Life
    for technology articles and musings.

  7. #7
    SitePoint Evangelist Alchemist's Avatar
    Join Date
    Aug 2006
    Posts
    412
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cool. thank you very much guys

  8. #8
    masquerading Nick's Avatar
    Join Date
    Jun 2003
    Location
    East Coast
    Posts
    2,215
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Christopher Hill View Post
    include/require_once has huge overheads compared to simply including or requiring.
    Oh? I had never really thought of that...how much overhead are we talking about here?
    Nick . all that we see or seem, is but a dream within a dream
    Show someone you care, send them a virtual flower.
    Good deals on men's watches

  9. #9
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Christopher Hill View Post
    include/require_once has huge overheads compared to simply including or requiring.
    My benchmarks:

    require of a very small file with 1 empty class definition on win2000, Pentium 2GHz, PHP 5.2.0 with eAccellerator:

    0.19 ms

    require_once of the same file:

    0.33 ms

    2nd execution of require_once of the same file in the same script:

    0.021 ms

    To me it looks like the overhead is negligible, you probably don't require_once hundreds of files for it to make any practical difference. *Huge* is not the word I would use here

  10. #10
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Well, it's almost double the processing time. Whether or not the second running is lower is irrelevent - you don't need to multiple-run require().
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  11. #11
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Location
    Bristol, England
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Lemon Juice View Post
    My benchmarks:

    require of a very small file with 1 empty class definition on win2000, Pentium 2GHz, PHP 5.2.0 with eAccellerator:

    0.19 ms

    require_once of the same file:

    0.33 ms

    2nd execution of require_once of the same file in the same script:

    0.021 ms

    To me it looks like the overhead is negligible, you probably don't require_once hundreds of files for it to make any practical difference. *Huge* is not the word I would use here
    It's also the memory consumption. PHP needs to remember all the files included at execution. Times that by 1,000 users on a site and sudenly it becomes a bigger issue.

  12. #12
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Christopher Hill View Post
    It's also the memory consumption. PHP needs to remember all the files included at execution. Times that by 1,000 users on a site and sudenly it becomes a bigger issue.
    You are right but it also depends on how you approach this. require_once is used for convenience and not speed and if you compare the actual overhead caused by using require_once instead of require you will find it's almost nothing compared to the overhead of a php framework or some other object-oriented solutions. A framework can give you an overhead of more than 2000ms in a script that displays an empty page not to mention memory consumption. If you have for example 20 require_once instead of require you save 2.8ms - you see the difference. It's up to the developer to decide if it's worth the trouble.

  13. #13
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    If you have for example 20 require_once instead of require you save 2.8ms
    Irrelevent. Using require you only do it once anyway, with 20 require_once()s you're wasting alot more processing power.

    IMHO included and required files should not run any code or output anyway. They should contain a class or some functions to do that, and then you include it automatically then run the function when you want it.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  14. #14
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Location
    Bristol, England
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In the words of Rasmus:

    If you know what you are including and when, you shouldn't ever need to use include_once/require_once[...]Why take a performance hit when none is needed. If the include files get moved around, a require_once won't fix that anyway. Always using require_once calls is just a bad habit as far as I am concerned. There may be a few common files that are prone to multiple inclusion in a complex application, but most shouldn't be included multiple times and if you are including them multiple times you'd probably want to know about it and not just ignore it the way the _once functions do.
    http://toys.lerdorf.com/archives/34-Flickr-API-Fun.html


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •