On a real website, the header and the footer are identical on every page. Rather than copying them everywhere, you put them in a separate file that you include.

The inclusion statements

  • include: inserts a file; if it is missing, it shows a warning but carries on.
  • require: the same, but it stops the script if the file is missing.
  • include_once / require_once: only include the file once.

A concrete example

  1. Create header.php with the site menu.
  2. At the top of every page: include “header.php”;.
  3. Change the menu once: every page is up to date.

For a vital file (the database connection, for instance), prefer require: it is better to stop the script than to carry on without it.

Key point: include and require prevent repetition. Use require for files you cannot do without.