PHP Include Files

You can include the content of a PHP file into another PHP file before the server executes it.The include and require statements are used to insert useful codes written in other files, in the flow of execution. There are two PHP functions which can be used to included one PHP file into another PHP file.

  • The include() Function
  • The require() Function

This is a strong point of PHP which helps in creating functions, headers, footers, or elements that can be reused on multiple pages. This will help developers to make it easy to change the layout of complete website with minimal effort. If there is any change required then instead of changing thousand of files just change included file.

Include and require are identical, except upon failure:

  • require will produce a fatal error (E_COMPILE_ERROR) and stop the script
  • include will only produce a warning (E_WARNING) and the script will continue

So, if you want the execution to go on and show users the output, even if the include file is missing, use include. Otherwise, in case of FrameWork, CMS or a complex PHP application coding, always use require to include a key file to the flow of execution. This will help avoid compromising your application's security and integrity, just in-case one key file is accidentally missing.

 

The include() Function

Syntax:

include 'filename';

Example:

Assume that you have a standard header file, called "header.php". To include the header file in a page, use include/require:

<html>
<body>

<?php include 'header.php'; ?>
<h1>Welcome to my home page!</h1>
<p>Some text.</p>

</body>
</html>

 

example 2:

Assume we have an include file with some variables defined ("vars.php"):

<?php
$color='red';
$car='BMW';
?>

Then the variables can be used in the calling file:

<html>
<body>

<h1>Welcome to my home page.</h1>
<?php include 'vars.php';
echo "I have a $color $car"; // I have a red BMW
?>

</body>
</html>

 

example 3:

Assume you want to create a common menu for your website. Then create a file menu.php with the following content.

<a href="http://www.mytoptutorials.com/">Home</a> –
<a href="http://www.mytoptutorials.com/html/">HTML</a> –
<a href="http://www.mytoptutorials.com/php/">PHP</a> – <br />

Now create as many pages as you like and include this file to create header. For example now your test.php file can have following content.

<html>
<body>
<?php include("menu.php"); ?>
<p>This is an example to show how to include PHP file!</p>
</body>
</html>

This will produce following result:

HomeHTMLPHP

This is an example to show how to include PHP file. You can include mean.php file in as many as files you like!

 

The require() Function

The require() function takes all the text in a specified file and copies it into the file that uses the include function. If there is any problem in loading a file then the require() function generates a fatal error and halt the execution of the script.

So there is no difference in require() and include() except they handle error conditions. It is recommended to use the require() function instead of include(), because scripts should not continue executing if files are missing or misnamed.

Syntax:

require 'filename';

You can try using above example with require() function and it will generate same result. But if you will try following two examples where file does not exist then you will get different results.

<html>
<body>
<?php include("xxmenu.php"); ?>
<p>This is an example to show how to include wrong PHP file!</p>
</body>
</html>

This will produce following result

This is an example to show how to include wrong PHP file!

Now lets try same example with require() function.

<html>
<body>
<?php require("xxmenu.php"); ?>
<p>This is an example to show how to include wrong PHP file!</p>
</body>
</html>

This time file execution halts and nothing is displayed.

NOTE: You may get plain warning messages or fatal error messages or nothing at all. This depends on your PHP Server configuration.

Both comments and pings are currently closed.

Comments are closed.

Designed for Mytoptutorials.com