PHP Include Tutorial
>
What is a PHP include file:
Basically a php include file allows you to separate your content out, for example at www.beginner-tutorials.com our side menu is an include file. This allows us to easily update our content when we have a new set of tutorials. Some examples of include files could be
- header.php
- footer.php
- menu.php
- content.php
In PHP the include takes a file name and simply inserts that file's contents into the script that issued the include command.
Ok so let have a look at the code required to use the include function
The first way to include a file is using the include function. This is a PHP built in code and the syntax is the following:
include ( filename )
Lets have a look at a real world example like used in www.beginner-tutorials.com where our side menu contains a list of the following tutorials
- Home
- Learn HTML
- Learn PHP
- Learn JAVA
- Learn Photoshop
- Learn C++
- Learn Perl
- Learn JavaScript
- Learn Unix
In this example we created a common menu file that all our web pages use, this file is called menu.php. In this file we added the following code.
| <ul class="sidemenu"> <li><a href="index.php">Home</a></li> <li><a href="html-tutorials.php">Learn HTML</a></li> <li><a href="php-tutorials.php">Learn PHP</a></li> <li><a href="java-tutorials.php">Learn JAVA</a></li> <li><a href="photoshop-tutorials.php">Learn Photoshop</a></li> <li><a href="c++-tutorials.php">Learn C++</a></li> <li><a href="perl-tutorials.php">Learn Perl</a></li> <li><a href="javascript-tutorials.php">Learn JavaScript</a></li> <li><a href="unix-tutorials.php">Learn Unix</a></li> </ul> |
To include the file in all our php pages we need to add the following code to all our php pages
<?php include("menu.php"); ?>
For example in our index.php we add the following line of code
<html><head><title>example of include</title></head>
<body>
<?php include("menu.php"); ?>
</body>
</html>
Result after uploading both the files test2.php and menu.php to your server.
Thats It.
![]() |
