PHP Include Function

Facebooktwitterredditpinterestlinkedinmailby feather

PHP Include( )

The PHP include() statement holds all the code/text that liesin the defined file and paste it into the file that uses the “include” statement.

With the help of include statement,we can insert the code/text of one PHP file into another PHP file(before server executes it).

Including file saves a lot of time or work. It means that we can create a standard header, footer, body, or menu file for all web pages. When we need to update a header, footer, or any web page, we can only update the included files.


  • We have a PHP file called “php”:

  • To include the “php” in a page, we use include statement.




  • We have a PHP file called “variable.php” with some defined variables:

  • We include the PHP file “variable.php”, in the calling file:

Difference between PHP include and require statements:

include require
1)  PHP include takes one argument, the filename that we want to include. PHP required() to perform the same process as include() does, takes one argument, the filename that we want to require.
2) Syntax: include ‘filename’; example: include ‘menu.php’; Syntax: require ‘filename’;

example:  require ‘menu.php’;

3)  PHP include() does not stop execution. PHP require() does stop execution once it gets a fatal error.
4) PHP include() gives the warning,and it continues to execute the script, in case if filename not found. If PHP require()found any problem like file not found or misnamed files and stops the execution once get a fatal error.
5) We can use PHP include() when the file is not required or not so important. When the script requires the file, it’s better to use required().




Warning: include(filenotexists.php): failed to open stream: No such file or directory in C:\xampp\htdocs\inc.php on line 5


In the above example, a file (filenotexists) is included with the include statement and PHP cannot find it, the PHP script will continue to execute.

Example 4:



Fatal error: require(): Failed opening required ‘filenotexists.php’ (include_path=’C:\xampp\php\PEAR’) in C:\xampp\htdocs\req.php on line 5

In the above example, the echo statement will not be executed because the script execution dies after the require statement returned the fatal error.

  • PHP include_once:

The file will be included just once. This statement is similar to the include statement, except only one difference that if the file has been already included, it will not beincluded again, it returns TRUE.

  • PHP require_once:

The require_once statement is similar to require statement except PHP file has already been included and if so, not require it again.

Facebooktwitterredditpinterestlinkedinmailby feather