With a single statement in your PHP script, you can incorporate other PHP script. PHP code in this included files will be executed as a part of main PHP script i.e. you include the files & evaluate it .

For example, we can create a common menu file and can use it in all the pages. Instead of writing menu code in all pages, make a single file and include it in all the pages you need. No need to paste same code again and again. One more benefit is that you can add a new feature or find a bug/error easily without changing in all the files. This include statement saves you from such a chore.

There are two functions for including files in PHP.

  • include() Function
  • require() function

Put your reused code like header, footers, menu or functions in a file and include that file wherever you want that code. Write once and reuse it in multiple pages. Content of the included file will be display as text by default. For execution of PHP code in an included file , enclose it with PHP start tag and end tag.

 include() Function ::

This include() function takes one argument , a path/name of the specified file you want to include.

 You can return a value from an included file and can use include() function in loops and control structure. While using include() function , if files are missing or misnamed, include(0 will not stop the execution in that case.

Syntax :       include(File-name);
Example :  include (“header.php”)

 include_once() Function ::

When you are working on a large project having many included files and including same file twice can create result in unnecessary repetition and repeated declaration of classes & functions.

Include_once() function is same as include() function but include_once() takes path to the included file. One more thing about include_once() is that as it name suggests it allows a file to be included only once i.e. if a file is already included , then it will be ignored second time.

Syntax : include_once(File name along with path);
Example : include_once(“/user/freefeast/test.php”);

 require() Function ::

require() function performs the similar functionality as include() function. require() function stops the execution & gives fatal error if it founds any problem like missing files or misnamed files. While include() function continues to execute in case of missing/misnamed files .

require() a function cannot be used in loops or control structures and file included as the result of a require statement cannot return a value. Like include() function, this also takes one argument , a path/name of the specified file you want to include.

Syntax :       require(File-name);
Example :       require(“header.php”);

require_once() Function:

require_once() performs similar function as include_once(). It allows file to be included only once specified with file name and path i.e. if a file is already included , then it will be ignored second time            .

Syntax : require_once(File name along with path);
Example : require_once(“/user/freefeast/test.php”);