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:
Php Code
<?php
require('somefile.php');

//or

require 'somefile.php';
?>

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

Php Code
<?php
echo "today is:".date("Y-m-d");
?>
[ad type=”banner”]

and in another file that we have named wiki2.php

Php Code
<?php
require_once(‘wiki1.php');
require_once("wiki1.php");
?>

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

Php Code
<?php
echo "today is:".date("Y-m-d");
?>

And in another file that we have named test4.php

Php Code
<?php
include_once(‘wiki3.php');
//or
include_once("wiki3.php");
?>

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: