Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.
It is possible to insert the content of one PHP file into another PHP file (before the server executes it), with the include or require statement.
If you want the execution to go on and show users the output, even if the include file is missing, use the include statement.
Including files saves a lot of work. This means that you can create a standard header, footer, or menu file for all your web pages. Then, when the header needs to be updated, you can only update the header include file.
Assume we have a standard footer file called "footer.php", that looks like this:
To include the footer file in a page, use the include statement:
The require statement is also used to include a file into the PHP code.
If we do the example using the require statement, the echo statement will not be executed because the script execution dies after the require statement returned a fatal error:
Use require when the file is required by the application.
Use include when the file is not required and application should continue when file is not found.