PHP Include / Require

PHP includes and requires are mechanisms that allow you to include the content of one PHP file into another PHP file.

This can be useful in situations where you have common code that you want to reuse across multiple pages, or when you want to break up a large PHP file into smaller, more manageable pieces.

 

There are two main ways to include files in PHP:

1) include()

2) require()

The main difference between these two functions is how they handle errors. If the file being included cannot be found by the include() function, a warning is issued and the script continues to execute. If the file being required cannot be found by the require() function, a fatal error is issued and the script stops executing.

 

Here is an example of how to use the include() function:

<?php

  include 'navbar.php';

  // Other page content goes here

  include 'footer.php';

?>

In this example, the content of the "navbar.php" and "footer.php" files will be included in the current PHP file.

 

Here is an example of how to use the require() function:

<?php

  require 'navbar.php';

  // Other page content goes here

  require 'footer.php';

?>

In this example, the "navbar.php" file and the "footer.php" file are required by the current PHP file. If either of these files cannot be found, a fatal error will be issued and the script will stop executing.