i

PHP Tutorial

Using Include() And Require()

Include function helps you to handle files on multiple pages in a proper way. It gives you the ability to organize the php files in a better way. Using include function you can use all the files in a single php file when the files are present at multiple locations. Imagine you have created a separate file for header and a separate file for the footer. To get all the multiple files in the homepage or main php file, you can use include function. Include statement includes and evaluates the specific file mentioned after include keyword.

Syntax of include function is include(filename) e.g. include(header.php) 

Require function also helps you to handle files on multiple pages and adds the file present at a different location. The filename is given with require() function to get the content of that file in the main page. Required file has to be present to run the php code where require function is used.

Syntax of require function is require(filename) e.g. require(header.php)

The difference between include() and require() is that when a file is not present mentioned after include() keyword it gives error for that file but executes the remaining code but when the file mentioned with require() function is not present, it gives a fatal error and doesn’t run the remaining code.