• home
  • forum
  • my
  • kt
  • download
  • PHP include file tutorial

    Author: 2007-08-11 18:40:38 From:

    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
    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 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
    1. <?php
    2. $i = 100;
    3. ?>
    PHP F1
    After this we create an other file which is the main file and it will include the internal.php as follows:
    Code: test.php
    1. <?php
    2. echo "Value of i before include is: -$i-<br/>";
    3. include("internal.php");
    4. echo "Value of i after include is: -$i-<br/>";
    5. ?>
    PHP F1
    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:
    1. <?php
    2. echo "Value of i before include is: -$i-<br/>";
    3. $i=100;
    4. echo "Value of i after include is: -$i-<br/>";
    5. ?>
    PHP F1
    Of course in a real life application your files contains a bit more code :)

    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
    1. <?php
    2. echo "-->internal.php<--<br/>";
    3. ?>
    PHP F1

    And the main file is:
    Code:
    1. <?php
    2. include ('internal.php');
    3. echo "Multiple include test.<br/>";
    4. include ('internal.php');
    5. echo "Now include it again.<br/>";
    6. include ('internal.php');
    7. echo "And again.<br/>";
    8. include ('internal.php');
    9. ?>
    PHP F1
    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<-- 

    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:
    1. <?php
    2. echo "Try to include wrong file first with include.<br/>";
    3. include ('internalwrong.php');
    4. echo "Now try it with require.<br/>";
    5. require ('internalwrong.php');
    6. echo "Finish.";
    7. ?>
    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
    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:
    1. <?php
    2. include_once ('internal.php');
    3. echo "Multiple include test.<br/>";
    4. include_once ('internal.php');
    5. echo "Now include it again.<br/>";
    6. include_once ('internal.php');
    7. echo "Multiple include test.<br/>";
    8. include_once ('internal.php');
    9. ?>
    PHP F1
    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.  

    discuss this topic to forum

    relation tutorial

    No relevant information

    Category

      Ad Management (4)
      Calendars (3)
      Chat Systems (7)
      Content Management (6)
      Cookies and Sessions (8)
      Counters (8)
      Database Related (8)
      Date and Time (9)
      Development (6)
      Discussion Boards (7)
      E Commerce (6)
      Email Systems (9)
      Error Handling (5)
      File Manipulation (10)
      Flash and PHP (4)
      Form Processing (7)
      Guestbooks (8)
      Image Manipulation (3)
      Installing PHP (5)
      Introduction to PHP (9)
      Link Indexing (6)
      Mailing List Management (8)
      Miscellaneous (10)
      Networking (6)
      News Publishing (6)
      OOP (8)
      PEAR (6)
      PHP vs Other Languages (2)
      Polls and Voting (5)
      Postcards (0)
      Randomizing (8)
      Redirection (8)
      Searching (6)
      Security (6)
      Site Navigation (7)
      User Authentication (10)
      WAP and WML (7)
      Web Fetching (0)
      Web Traffic Analysis (11)
      XML and PHP (0)

    New

    Hot