Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder sitNsmile's Avatar
    Join Date
    Dec 2009
    Location
    Charlotte, NC
    Posts
    358
    Thanks
    19
    Thanked 2 Times in 2 Posts

    Exclamation require_once or include "" ?

    What are the real differences in just using

    Yeah I know it says only "once" but how does that matter much in when I want to include file through out the site.
    PHP Code:
    require_once("functions.php"); 
    or if I just added instead (I usually use this one)
    PHP Code:
    include "functions.php"

    Thanks

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    once prevents a re-import of variables and functions. If you include and later include again, any variables that have been changed will be replaced and any functions already declared will toss a fatal error (killing you're code of course). There is rarely a need to go without the *_once option. Back in the day, it was hard to control this in comparison to nowadays (we didn't have the *_once functions about 8 years ago).

    Require and include are completely different though. Include will continue to process regardless of if it can find the file, require will terminate script execution. Include is useful for something like configurations that have been pre-declared, while require is useful for breaking you're code into functions or classes.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    Regular Coder
    Join Date
    Jun 2007
    Posts
    126
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I'm also unsure when to use this.

    What would i use for adding db.php files, and files with config and variables?

    And what would i use for including files that contain HTML elements such as my side bars and stuff?

  • #4
    Regular Coder
    Join Date
    Mar 2006
    Posts
    238
    Thanks
    3
    Thanked 37 Times in 37 Posts
    Personally I prefer require_once() in any case where I have a doubt.

    As Fou-Lu correctly said, include() or include_once() would allow to continue the script execution even if the inclusion failed. If you know for sure why you need this behavior in the script, you could use them of course.

    Normally I prefer to stop a script execution and get a fatal error if such a serious thing as inability to include a file occurs.

    Though this is my personal opinion only which could be wrong of course.

  • #5
    Regular Coder
    Join Date
    Jun 2007
    Posts
    126
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I have 2 side bars for left and right.

    i used:

    <? require_once('left.php'); ?>

    which works fine.

    then i add:

    <? require_once('right.php'); ?>

    then the left side bar disappears. why is this?

    I also have:

    <? require_once('home/index.php'); ?>

    for the main content. Am i using it in the correct way?

    what would i use to include /header.php, /footer.php, /db.php and /nav.php (nav holds an array of links which is then included into the side bar)

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by forcerhpool View Post
    I have 2 side bars for left and right.

    i used:

    <? require_once('left.php'); ?>

    which works fine.

    then i add:

    <? require_once('right.php'); ?>

    then the left side bar disappears. why is this?

    I also have:

    <? require_once('home/index.php'); ?>

    for the main content. Am i using it in the correct way?

    what would i use to include /header.php, /footer.php, /db.php and /nav.php (nav holds an array of links which is then included into the side bar)
    Assuming you're php contains processing instructions, you'd use either a require or require_once call. If you're importing both left.php and right.php and the display changes, you're either overwriting the variables or you're html code is incorrect.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #7
    Regular Coder sitNsmile's Avatar
    Join Date
    Dec 2009
    Location
    Charlotte, NC
    Posts
    358
    Thanks
    19
    Thanked 2 Times in 2 Posts
    Okay I think I some what get the differences, but not in a complete way.

    If I wanted to include the database information, I should use

    PHP Code:
    <? require_once('database.php'); ?>
    and if I was to include other files like "functions"

    I could just use
    PHP Code:
     <? include "functions.php"?>
    but really wouldnt matter if I used include_once or require?
    Is it more personal preference or is there a real meaning to using the correct include?

    Thanks

  • #8
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    include loads the file, but script processing carries on if it cannot, for some reason, actually load the file. require behaves the same except that the script will abort/exit/die if the file cannot be loaded for any reason. include_once and require_once behave the same except for the simple fact that they won't reload the file if it has been loaded previously, whereas require and include will attempt to do so.

    Personally, I always use require. If something is missing, I can't see any reason why the script should be allowed to continue. It needs sorting. Same with not using the _once version of require. If you're trying to load a file twice, your script needs sorting.

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Quote Originally Posted by MattF View Post
    include loads the file, but script processing carries on if it cannot, for some reason, actually load the file. require behaves the same except that the script will abort/exit/die if the file cannot be loaded for any reason. include_once and require_once behave the same except for the simple fact that they won't reload the file if it has been loaded previously, whereas require and include will attempt to do so.

    Personally, I always use require. If something is missing, I can't see any reason why the script should be allowed to continue. It needs sorting. Same with not using the _once version of require. If you're trying to load a file twice, your script needs sorting.
    I also only require now. Though I do use a lot of require_once calls; this is because of the extreme OO approach I've now taken. Using require calls to load classes is a pain; half the concept with the classes is to create the reusability, so for the most part I cannot be 100% certain that a class has already been loaded, and therefore require the definition to be reloaded. The once will prevent it from trying to reload the same class definitions.
    Though I now use the spl_autoload call so I can import at the same time I issue a 'use' command.

    The only time I'd ignore inclusions is if it won't cause damage to the scripts. Perhaps you want to allow users to have say a custom format for their date/time numbers which you store in an ini file or whatever. Since I will already have a default for these formats, if I cannot load the file I will just continue and issue a warning instead. Conceptually, if you need something to be there for you - use a require. If you can survive without it, or its something asthetic, include it.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #10
    Regular Coder sitNsmile's Avatar
    Join Date
    Dec 2009
    Location
    Charlotte, NC
    Posts
    358
    Thanks
    19
    Thanked 2 Times in 2 Posts
    Thank you both. that was more help. now I understand on using the whole require and include much more then I did yesterday. I think I will starting calling using require instead of the include, cause most of the programming in the functions and database I'd rather kill the site instead of continuing with problems on the code.

  • #11
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by Fou-Lu View Post
    Perhaps you want to allow users to have say a custom format for their date/time numbers which you store in an ini file or whatever. Since I will already have a default for these formats, if I cannot load the file I will just continue and issue a warning instead.
    That's a good point. I hadn't considered that type of requirement. Users actually being allowed to do something... I must just be draconian.


  •  

    Posting Permissions

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