1 min read

PHP Include and Require

The include and require statements allow you to insert the content of one PHP file into another PHP file (before the server executes it).


1. Difference between include and require

  • require: Use when the file is required by the application. If the file is not found, a fatal error is thrown and the script stops.
  • include: Use when the file is not required. If the file is not found, a warning is shown but the script continues.

2. Syntax

include 'filename.php';
require 'filename.php';

3. include_once and require_once

These behave like include and require, but if the file has already been included, it will not be included again. This prevents problems like redefining functions or classes.

require_once 'config.php';

4. Use Cases

  • Headers and Footers: Create a standard header/footer and include it on every page.
  • Configuration: Store database settings in a separate file.
  • Functions: Keep common functions in a library file.

programming/php/php