This tutorial will show you how to divide your
code into more files and how to include various files into your actual
PHP code. Maintain organization and ease of updates using file includes.
PHP include file
Step 1 - Include files introductionPHP include file
If
you want to make a project then it is not a good idea to put all of the
code into one file. Even a quite simple task can result a huge amount
of code. To maintain and reuse a code from a single file is quite hard.
If you download and check the source code of any PHP project
you will find a lot of files. Each file contains only a well defined
task implementation. For example in OOP each class has it's own file or
you create a file for DB management an other one for mail sending and
an other to display menu system.
A real example could be a project with the following files:
- index.php
- header.php
- footer.php
- menu.php
- content.php
The final page is constructed by using all of the above mentioned
files. The benfit of this structure is that if you need to change meta
information you only need to edit the header.php file and so you
probably make less error.
However
to make this sollution to work you need to learn how to include a file
in an other one. There are more ways to do this in PHP. You can use the
following functions:
- include
- require
- include_once
- require_once
In the next steps I will explain how to use them.
Step 2 - The php include functionPHP include file
The first way to include a file is using the include function. This is a PHP built in code and the synatx is the following:
include ( filename )
This
code tries to include the content of the file defined by filename. If
the file doesn't exists the include function will report a warning but
the script will continue.
Let's see an example. First we create a file which will be included later and it only contains a variable initialisation:
Code: internal.php
-
<?php
-
$i = 100;
-
?>
After this we create an other file which is the main file and it will include the internal.php as follows:
Code: test.php
-
<?php
-
echo "Value of i before include is: -$i-<br/>";
-
include("internal.php");
-
echo "Value of i after include is: -$i-<br/>";
-
?>
If you run the test.php script you will get an output similar to this:
Output:
Value of i before include is: --
Value of i after include is: -100-
As you can see the variable $i was initialised in the internal.php file
and so the first echo couldn't display it. However after we included
the file it become part of our code so the second echo function
displays it correctly.
Without the include function the code would be something like this:
Code:
-
<?php
-
echo "Value of i before include is: -$i-<br/>";
-
$i=100;
-
echo "Value of i after include is: -$i-<br/>";
-
?>
Of course in a real life application your files contains a bit more code :)
Step 3 - Include file more than oncePHP include file
Sometimes
it can happen that you want to include a file in your script more than
once. There is no problem with it. You can do this by calling include
more times. Altough in such case maybe a well organized loop can be
better.
So to demonstarte more include calls I have created the following example code:
Code: internal.php
-
<?php
-
echo "-->internal.php<--<br/>";
-
?>
And the main file is:
Code:
-
<?php
-
include ('internal.php');
-
echo "Multiple include test.<br/>";
-
include ('internal.php');
-
echo "Now include it again.<br/>";
-
include ('internal.php');
-
-
include ('internal.php');
-
?>
This code will result the following output:
Output:
-->internal.php<-- Multiple include test. -->internal.php
<-- Now include it again. -->internal.php<-- And again. -->internal.php<--
google_protectAndRun("ads_core.google_render_ad", google_handleError, google_render_ad);
Step 4 - The php require functionPHP include file
In
a lot of cases it is very important to have the file really included in
our code. For example if the include of a database management code
fails and the script continue it's run then it will cause a lot of
unwanted error in the later steps. To avoid this you can say if the
include was not success then stop all the further activity. For this
purpose was the function require() introduced.
The
usage if php require function is quite similar to the include however
if the file can not be found require will report a fatal error and the
script will die.
Let's see an example:
Code:
-
<?php
-
echo "Try to include wrong file first with include.<br/>";
-
include ('internalwrong.php');
-
echo "Now try it with require.<br/>";
-
require ('internalwrong.php');
-
-
?>
And the output will be:
Output:
Try to include wrong file first with include.
Warning: include(internalwrong.php) [function.include]: failed to open stream: No such file or directory in Z:WorkWebSitestestf1test.php on line 3
Warning: include() [function.include]: Failed opening 'internalwrong.php' for inclusion (include_path='D:Program FilesZendZendStudio-5.5.0binZendFrameworklibrary') in Z:WorkWebSitestestf1test.php on line 3
Now try it with require.
Warning: require(internalwrong.php) [function.require]: failed to open stream: No such file or directory in Z:WorkWebSitestestf1test.php on line 5
Fatal error: require() [function.require]: Failed opening required 'internalwrong.php'
Step 5 - Include files only oncePHP include file
In
a big and complaex project it can happen that you don't know whether a
file was included yet or not. For example suppose the following
situation:
- A.php includes B.php
- B.php includes C.php only if a condition is true in B.php
In this case you can not be sure that C.php is surely included in A.php
besides this you want to avoid duplicated includes. To solve this
problem you can use the include_once or the require_once functions.
These
functions first checks whether a file was included or not. After this
check they include the file only if it was not included before. To
demonstarte this let's go back to an older example and now use
include_once instead of include as follows:
Code:
-
<?php
-
include_once ('internal.php');
-
echo "Multiple include test.<br/>";
-
include_once ('internal.php');
-
echo "Now include it again.<br/>";
-
include_once ('internal.php');
-
echo "Multiple include test.<br/>";
-
include_once ('internal.php');
-
?>
So the code is very similar but the output is rather different:
Output:
-->internal.php<-- Multiple include test. Now include it again. Multiple include test
|