PHP include file and require Statements
In PHP, you can insert the content of one PHP file into another PHP file before the server executes it.
The include and require statements are used to insert useful codes written in other files, in the flow of execution.
Include and require are identical, except upon failure:
- require will produce a fatal error (E_COMPILE_ERROR) and stop the script
- include will only produce a warning (E_WARNING) and the script will continue
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.
PHP include and require Statement
Assume that you have a standard header file, called "header.php". To include the header file in a page, use include/require:
Assume we have a standard menu file that should be used on all pages.
All pages in the Web site should include this menu file. Here is how it can be done: