1) require() – include()

  • Same: insert file to php file. Both display errors when file has errors.
  • Different: when file insert by using require() , if the file has errors, compiler will stop and display the errors. On the other side, the file which insert by include() has errors, compiler continue to the end of file and then display the warning that file is errors

Example 1:

<?php
 required(‘b.php’);
 echo “hello”;
?>

Suppose that “ b.php ” doesn’t exist, when this code is running, the “hello” will not be displayed.

Example 2:

<?php
include(‘b.php’);
echo “hello”;
?>

This time, the “hello” will be displayed even file “b.php” not exist.

1) require_once() – include_once()

  • It’s another ways of require() and include() but it will check if the file has been inserted before, it won’t insert again
Example 1: suppose file a.php has content “hello world -“
<?php
include “a.php”;
include “a.php”;
?>

The result will be “hello world – hello world -”

Example 2:

<?php
include “a.php”;
include_once“a.php”;
?>

Or

<?php
include_once “a.php”;
include_once “a.php”;
?>

The result will be only “hello world -”