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:
So, if you want the execution to go on and show users the output, even if the include file is missing, use include. Otherwise, in case of FrameWork, CMS or a complex PHP application coding, always use require to include a key file to the flow of execution. This will help avoid compromising your application's security and integrity, just in-case one key file is accidentally missing.
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 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.
"menu.php":
All pages in the Web site should include this menu file. Here is how it can be done:
Assume we have an include file with some variables defined ("vars.php"):
Then the variables can be used in the calling file:
Your message has been sent to W3Schools.