19

I know the basic usage of PHP require, require once, include and include once. But I am confused about when I should use them.

Example: I have 3 files, eg: settings.php, database.php, and index.php.

In database.php file, i wrote:

require_once 'settings.php';

and then in index.php, i wrote:

require_once 'settings.php';
require_once 'database.php';

so I load settings.php two times, is this okay? Any tips using these functions?

ChrisF
  • 134,786
  • 31
  • 255
  • 325
Vina
  • 759
  • 3
  • 10
  • 19

5 Answers5

24
  • include includes a file and throws a warning if the file was not found.

  • require includes a file and throws a fatal error if the file was not found.

  • include_once and require_once do the same thing, but only if the file was not already loaded.

However, the need for one of the _once functions is usually a sign of bad design. You should build your scripts in a way that clearly defines what gets included where.

Choose one place for settings.php to get included - probably index.php. There should be no need to additionally include it in database.php.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • 8
    +1. Though, nitpick/opinion: I don't think I agree with 'the need for one of the `_once` functions is usually a sign of bad design'. Autoloaders pretty much do the same thing, after all. I would say `_once` should always be used for files that contain only function/class/constant declarations, and the others for script snippets. In which case I would say *not* using `_once` is usually[!] a sign of bad design. `_once` (in absence of an autoloader), allows your code to be decently modular. (In this particular case, I would agree it should be included only once in the first place, though.) – pinkgothic Sep 02 '10 at 10:45
  • 1
    @pinkgothic I have to disagree. Autoloaders work on the same principle, *but* if they're worth their salt they do a `class_exists()` check before attempting an include. In my experience (read, also *own* experience!) `require_once()` *usually* means "I'm not sure whether this file has been included at some other point already, because I have no full grasp of my project's structure." as far as I can see, any other use of `_once` can be prevented by checking whether the resource I'm about to include is present already - be it a function, or a class. – Pekka Sep 02 '10 at 10:50
  • That said, I'm sure there are legitimate cases where doing a `_once` is simply the quicker and faster solution. – Pekka Sep 02 '10 at 10:51
  • Agreed, you could wrap your includes/requires into if-blocks, but that's doing the same work PHP does internally (well, all right, not the identical work, but the same principle) and becomes less readable in the process. Ultimately, it's a question of philosophy. Mine just happens to be the opposite of yours... probably largely because my experience has also been the opposite. At the end of the day, though, I'm sure we'd punch the same people. :)) – pinkgothic Sep 02 '10 at 10:59
  • @pinkgothic fair points! True, we probably aim for the same thing from (seemingly) different directions. :) – Pekka Sep 02 '10 at 11:03
3

You don't load settings.php two times, as per PHP documentation on require_once;

The require_once() statement is identical to require() except PHP will check if the file has already been included, and if so, not include (require) it again.

Daniel Sloof
  • 12,568
  • 14
  • 72
  • 106
2

The difference between include() and require() arises when the file being included cannot be found: include() will raise a warning (E_WARNING) and the script will continue, whereas require() will raise a fatal error (E_COMPILE_ERROR) and halt the script. If the file being included is critical to the rest of the script running correctly then you need to use require().

For more details : Difference between Include and Require in PHP

thomas
  • 785
  • 8
  • 7
1
  • require
    when the file is required by your application, e.g. an important message template or a file containing configuration variables without which the app would break.

  • require_once
    when the file contains content that would produce an error on subsequent inclusion, e.g. function important() { /* important code */} is definitely needed in your application but since functions cannot be redeclared should not be included again.

  • include when the file is not required and application flow should continue when not found, e.g.
    great for templates referencing variables from the current scope or something

  • include_once
    optional dependencies that would produce errors on subsequent loading or maybe remote file inclusion that you do not want to happen twice due to the HTTP overhead

Anand Huded
  • 641
  • 5
  • 12
0

Difference:-

include() Fetch data and load contain in current file and also load same file more than one time.

include_once() work same as include() but we use include_once() if a file has already been included, it will not be included again. if use same file as multiple time in Like:- include_once 'setting.php'; use second time include_once 'settting.php'; ignore them.

require() work same as include().

require_once() if file has already included, it will not be included again.

include() and include_once() produce warning and script will continue.

require() and require_once() produce fatel error and stop the script.

Better to use

require_once() in your script.
Vinit Dabhi
  • 993
  • 8
  • 8