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:
- include()
- include_once()
- require()
- require_once()
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?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:
1 2 3 4 |
<?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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<!--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 © 2012 </div><!--/footer--> </body> </html> |
STEP 1: split the template into several php files to be included:
header.php:
1 2 3 4 5 6 7 |
<!--header section--> <html> <head> <link rel="stylesheet" type="text/css" href="file.css"/> <title>My website</title> </head> <body> |
navigation.php:
1 2 3 4 5 6 7 |
<!--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:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<!--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:
1 2 3 4 5 6 7 8 9 |
<!--footer section--> </div><!--/main--> <div id="footer"> cramerz.com © 2012 </div><!--/footer--> </body> </html> |
STEP 2: include the external php files:
index.php:
1 2 3 4 5 6 7 8 9 10 11 |
<?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"); ?> |