home

Php Include Tutorial


A common practice is to define a set of functions for an application and place them in a PHP file which is loaded by all other scripts in the system. External PHP files can be loaded using the include() function:

include('path/to/functions.php');

There are four functions in PHP to include an external file:

The difference between include and require is that the first will always attempt to continue processing the scripts, while the latter generates a fatal error in case of issues (e.g. cannot find the required file). You can use include_once() and require_once() to make sure the external file is not included more than once.

Here an example about how to include a file with useful functions:

functions.php:

<?php #functions.php

/**
* displayArrayContent()
* this function will format a print_r() output
* @param $arr the array to print out
*/

function displayArrayContent($arr){
echo "<pre>";
print_r($arr);
echo "</pre>";
}//end displayArrayContent

//it is good practice to omit the closing php tag (?>) at the end of included files;
//new lines or even spaces after the closing php tag may create issues when you send headers

anyFile.php:

<?php  #anyFile.php
//including an external script
include("functions.php");
?>

Any PHP code in the file being included must occur inside <?php and ?> tags. Normal text and HTML in the file will be output straight to the browser. This makes the include() function extremely useful for keeping repeated elements of web sites (such as the common navigation or header) in a single file.

Here an example about how to use include() to reproduce a template:

template.html:

<!--header section: header.php -->
<html>
<head>
<link rel="stylesheet" type="text/css" href="file.css"/>
<title>My website</title>
</head>
<body>

<!--navigation section: navigation.php-->
<div id="navigation">
<a href="index.html">homepage</a> |
<a href="news.html">news</a> |
<a href="gallery.html">gallery</a> |
<a href="contacts.html">contacts</a>
</div><!--/navigation-->

<!--main section: main.php-->
<div id="main">
<h1>this is my website</h1>
<p>this is my content:. . .</p>

<!--footer section: footer.php-->
</div><!--/main-->

<div id="footer">
cramerz.com &copy; 2012
</div><!--/footer-->

</body>
</html>

 

STEP 1: split the template into several php files to be included:

header.php:

<?php #header.php?>
<!--header section-->

<html>
<head>
<link rel="stylesheet" type="text/css" href="file.css"/>
<title>My website</title>
</head>
<body>

 

navigation.php:

<?php #navigation.php?>
<!--navigation section-->
<div id="navigation">
<a href="index.html">homepage</a> |
<a href="news.html">news</a> |
<a href="gallery.html">gallery</a> |
<a href="contacts.html">contacts</a>
</div><!--/navigation-->

 

main.php:

<?php #main.php?>
<!--main section-->

<div id="main">
<h1>this is my website</h1>
<p>
this is my content:
<?php

 if(isset($content)){
echo $content;
} else {
echo "NO CONTENT";
}
?>
</p>

 

footer.php:

<?php #footer.php?>
<!--footer section-->
</div><!--/main-->

<div id="footer">
cramerz.com &copy; 2012
</div><!--/footer-->

</body>
</html>

 

STEP 2: include the external php files:

index.php:

<?php #index.php
//declaring a content variable
$content = "HERE MY CONTENT FROM DB";

//including external files
include("header.php");
include("navigation.php");
include("main.php");
include("footer.php"); ?>

 


Content on this page requires a newer version of Adobe Flash Player.

Get Adobe Flash player

Copyright © 2014 by Cramerz.com.