Step 1 - Include files introduction
PHP 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
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
Step 2 - The php include function
PHP 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; ?>PHP F1
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/>"; ?>PHP F1
Output:Value of i before include is: --
Value of i after include is: -100-
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/>"; ?>PHP F1
Step 3 - Include file more than once
PHP 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/>"; ?>PHP F1
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'); echo "And again.<br/>"; include ('internal.php'); ?>PHP F1
Output:
-->internal.php<-- Multiple include test. -->internal.php<-- Now include it again. -->internal.php<-- And again. -->internal.php<--
Step 4 - The php require function
PHP 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'); echo "Finish."; ?>PHP F1
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' (include_path='D:Program FilesZendZendStudio-5.5.0binZendFrameworklibrary') in Z:WorkWebSitestestf1test.php on line 5
As you can see the include reports warnings but the executeion was continued and so you can see the output of the second echo function. However using the require to include the same file we got a fatal error and the script was stoped. You can't find the Finish message in the output.
Step 5 - Include files only once
PHP 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
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'); ?>PHP F1
Output:-->internal.php<-- Multiple include test. Now include it again. Multiple include test.
discuss this topic to forum
