PHP include and require

I. Import files in PHP

To organize our code, we can split our file into multiple files and include them when we need.

In PHP, we can use these statements to import files:

  • include
  • include_once
  • require
  • require_once

II. Syntax

The syntax for include and require:

include 'file.php';
require 'file.php';
include_once 'file.php';
require_once 'file.php';

The include_once and require_once will only include the file one time.

III. Difference between include and require

The include and require statements are similar, except when there is an error:

  • include statement gives a warning (E_WARNING) and the script will continue.
  • require statement gives a fatal error (E_COMPILE_ERROR) and stop the script.