- PHP

How to include a file in PHP

There are a few different ways:

  • include() allows us to include the file even if it has some errors (it will generate warnings, but the script should continue its execution).
  • require() works pretty much the same, but will stop the rest of the script if included file generated some errors.
  • require_once() works like require(), but will include the specific file only one (it prevents us from including the same file many times).

All of them are useful, so it’s up to you which one will you choose in the specific scenario. From my experience, I use mostly require_once(), which is, in most cases, the safest one.

Examples:

<?php

include('path/to/file.php');

require('path/to/file.php');

require_once('path/to/file.php');

?>