One of the common question amoung the beginers or freshers is what is the difference between include and require function.
In this article describe what are the difference between these two. If you want to include the PHP file into another PHP file, before the server is execute.
This will help web developers to make it easy to modify the layout of complete website with minimal effort. If there is any change required then instead of changing thousands of files just change included file.
There are two PHP functions which can be used to included one PHP file into another PHP file.
- The include() Function
- The require() Function
There are four ways to include the files into your web pages. They are include(), include_once(), require() and require_once().
PHP include() function
The include() function is used in PHP when you want to include a file within the current process. The include() function takes all the text or content in a specified file and copies to current process. Include() function generates a warning when any problem in loading a file but the script will continue execution.
The include() function helps to include the files into our pages. If you want to include a file into ur current process.
When the include function is called, the current page(main page not include file) is run or execute.
If the contents are in all page then we specify the repeating content in one file and include the file into a specified file like header and footer in template. Header at the top of the page and footer at the bottom of the page. The header and footer are going to be the same on all pages. You will put this code inside it’s own file and use the include function to add the file to the page.
PHP require() function
The require() function is also used to include a file into the PHP code, require() function will produce a fatal error or E_COMPILE_ERROR and stop the script.
PHP include vs. require
The main difference between include and require; when a file is included with the include statement and PHP cannot find it, the script will continue to execute
The require will be a fatal error E_COMPILE_ERROR which will stop the application continuing, where the include function will just return a warning but will continue with the application
If anyone has doubts on this topic then please do let me know by leaving comments or send me an email.