require

  • The require statement is used to include a file into the PHP code.
  • require is identical to include except upon failure it will also produce a fatal E_COMPILE_ERROR level error.
  • In other words, it will halt the script whereas include only emits a warning (E_WARNING) which allows the script to continue.
  • when using require that it is a statement, not a function. It’s not necessary to write:
[pastacode lang=”php” manual=”%3C%3Fphp%0Arequire(‘somefile.php’)%3B%0A%0A%2F%2For%0A%0Arequire%20’somefile.php’%3B%0A%3F%3E” message=”Php Code” highlight=”” provider=”manual”/]

require_once

  • require_once() statement can be used to include a php file in another one, when you may need to include the called file more than once.
  • If it is found that the file has already been included, calling script is going to ignore further inclusions.
  • If a.php is a php script calling b.php with require_once() statement, and does not find b.php, a.php stops executes causing a fatal error.

Syntax:

require_once(‘name of the calling file with path’);

Example

The file name is wiki1.php

[pastacode lang=”php” manual=”%3C%3Fphp%0Aecho%20%22today%20is%3A%22.date(%22Y-m-d%22)%3B%0A%3F%3E” message=”Php Code” highlight=”” provider=”manual”/] [ad type=”banner”]

and in another file that we have named wiki2.php

[pastacode lang=”php” manual=”%3C%3Fphp%0Arequire_once(%E2%80%98wiki1.php’)%3B%0Arequire_once(%22wiki1.php%22)%3B%0A%3F%3E” message=”Php Code” highlight=”” provider=”manual”/]

If the test1 file twice but the file will include the wiki1 once and for calling at the second time this will be ignored.

And without halting will display the output a single time.

Include_once

  • The include_once statement includes and evaluates the specified file during the execution of the script.
  • This is a behavior similar to the include statement, with the only difference being that if the code from a file has already been included, it will not be included again, and include_once returns TRUE.
  • As the name suggests, the file will be included just once.

Syntax:

Include_once(‘name of the called file with path’);

Example

Here the file name is wiki3.php

[pastacode lang=”php” manual=”%3C%3Fphp%0Aecho%20%22today%20is%3A%22.date(%22Y-m-d%22)%3B%0A%3F%3E” message=”Php Code” highlight=”” provider=”manual”/]

And in another file that we have named test4.php

[pastacode lang=”markup” manual=”%3C%3Fphp%0Ainclude_once(%E2%80%98wiki3.php’)%3B%0A%2F%2For%0Ainclude_once(%22wiki3.php%22)%3B%0A%3F%3E” message=”Php Code” highlight=”” provider=”manual”/]

Here including the test3 file will include the file a single time but halt the further execution.

When should we can use require vs. include?

  • The require() function is identical to include(), except that it handles errors differently.
  • If an error occurs, the include() function generates a warning, but the script will continue execution.
  • The require() generates a fatal error, and the script will stop.

When should we can use require_once vs. require?

  • 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.

All these functions require, require_once, include and include_once are used to include the files in the php page but there is slight difference between these functions.

[ad type=”banner”]

Difference between require() and require_once():

  • require() includes and evaluates a specific file, while require_once() does that only if it has not been included before (on the same page).
  • Hence, require_once() is recommended to use when you want to include a file where you have a lot of functions for example. This means you make sure you don’t include the file more times and you will not get the “function re-declared” error.

Difference between require() and include()

  • Difference between require and include is that if the file you want to include is not found then include function give you warning and executes the remaining code in of php page where you write the include function.
  • While require gives you fatal error if the file you want to include is not found and the remaining code of the php page will not execute.

Categorized in: