As a web developer, we most of the time come to scenarios where we do not want to keep repeating a piece of code that is doing the same thing in all the files that we need to create.
We look for a way where we can place the default code in a file that we can include in all the other files that we have created. This is why there is a need for PHP inbuilt functions that help us achieve the function of not repeating codes that are required in every page but do not change.
For example, while creating a website, you may realize that the navigation bar in the header and also the footer is always the same across all pages. There is no need of adding the codes for the header and footer in every file you create as there is an easier way of achieving this.
In this article, we shall focus on the use of require and include functions in PHP that will help you to minimize your coding work such that you will only create one file and you can easily use it in all the files you have.
The advantage of using require and include functions in PHP is that, when you want to update some piece of code, you only do it once and all the files will receive the update and also, it minimizes the size of your file as the number of lines added in a page will be reduced since some other static codes are in separate files.
PHP require function
PHP require function is an inbuilt PHP function that is used to put data into another file. When you use the required function and you specify the path of the file you want to include, the data in the separate file will act as it is part of the file it has been added to.
When using the PHP require function and the data you are adding happens to have an error, the require function will produce an error and the execution will stop.
The syntax for using the PHP require function is as below
An example will be;
Suppose you have the database connection code in a file called db_connect.php in a folder called required. To use the database connection code, you will need to add it as follows
require "required/db_connect.php ";
The above code snippet will ensure that the database connection is available for use in any file you require it.
PHP include function
PHP include function is an inbuilt PHP function that is also used to put data into another file. When you use the include function and you specify the path of the file you want to include, the data in the separate file will act as it is part of the file it has been added to.
When using the PHP include function and the data you are adding happens to have an error, the include function will produce a warning but the execution in the file will proceed. That is the major difference between include and require functions.
The syntax for using the PHP include function is as below
Just like above, to include a database connection file using include, you will need to use the following code.
include "required/db_connect.php ";
Please note that while using include and require functions, you should not just include the files anywhere. For example, if the script that you will include has data that is to be displayed in the browser, you should ensure that the position you call the file is where you want it to appear else if you include it at the top of the page and it’s a footer script it will appear before the header.
PHP require_once and include_once functions
PHP provides the use of require_once and include_once functions which are more the same as require and include functions expect that the require_once and include_once ensure that the file is only included once even if it is called more than one time. But all of them work the same without difference in syntax.