PHP include file



We can include other files also in our PHP programs. Its for inserting the codes in other files in the current PHP program. Its a good practice to do make individual files with a piece of code which we need to use in several other pages of our project. Instead of writing the same codes in all pages we can include that file with the same code. Database connection code, reusable function and some global declarations , common header or footer or sidebar etc can be saved in a separate file , and we need to only include that file in the required page.

Here we are going to learn about various statements using for include a file.
There are mainly four statements are there for this in PHP

  • include
  • require
  • include_ once
  • require_ once

include” and “require” statements will simply add the contents of the includes file.Its usage is like include ‘filename’; or require “filename”;( dont forget to provide the exact path to the file)

“include_ once” and “require_ once” statements will do the same job as include and require execpt it will include the file only once. Means even if we have tried to include the file same file more than once in a page using any of these statements it will include only once.

Now we can look at the different between these statements

Difference between include and require

Both will do the same job when including a file. Difference is occurring when the file is missing. include statement will produce only a warning (E_WARNING) and continue the execution of script . But require statement will produce a fatal level error (E_COMPILE_ERROR) and stops the execution of program.