Friday, 2010-07-30

Who's Online

We have 7 guests online


Breadcrumbs

Home Include Include


Include

                                PHP Include File

 

  • Introduction:

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(object oriented programing) each class has it's own file or you create a file for database 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 solution 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

  • Server Side Includes (SSI)


You can insert the content of one PHP file into another PHP file before the server executes it, with the include() or require() function.

The two functions are identical in every way, except how they handle errors:

  • include() generates a warning, but the script will continue execution
  • require() generates a fatal error, and the script will stop

These two functions are used to create functions, headers, footers, or elements that will be reused on multiple pages.

Server side includes saves a lot of work. This means that you can create a standard header, footer, or menu file for all your web pages. When the header needs to be updated, you can only update the include file, or when you add a new page to your site, you can simply change the menu file (instead of updating the links on all your web pages).


  • PHP include() Function

 

The include() function takes all the content in a specified file and includes it in the current file.

If an error occurs, the include() function generates a warning, but the script will continue execution.

 

Example 1:

Assume that you have a standard header file, called "header.php". To include the header file in a page, use the include() function:

Code:
                                                     <html>
                                                     <body>

                                                     <?php include("header.php"); ?>
                                                     <h1>Welcome to my home page!</h1>
                                                     <p>Some text.</p>

                                                     </body>
                                                     </html>

 

Example 2:

Assume we have a standard menu file, called "menu.php", that should be used on all pages:

Code:

                                                      <a href="/default.php">Home</a>
                                                      <a href="/tutorials.php">Tutorials</a>
                                                      <a href="/references.php">References</a>
                                                      <a href="/examples.php">Examples</a>
                                                      <a href="/about.php">About Us</a>
                                                      <a href="/contact.php">Contact Us</a>

 

All pages in the Web site should include this menu file. Here is how it can be done:

Code:

                                                      <html>
                                                      <body>

                                                      <div class="leftmenu">
                                                      <?php include("menu.php"); ?>
                                                      </div>

                                                      <h1>Welcome to my home page.</h1>
                                                      <p>Some text.</p>

                                                      </body>
                                                      </html>

 

If you look at the source code of the page above (in a browser), it will look like this:

Source code in browser:

                                                      <html>
                                                      <body>

                                                      <div class="leftmenu">
                                                      <a href="/default.php">Home</a>
                                                      <a href="/tutorials.php">Tutorials</a>
                                                      <a href="/references.php">References</a>
                                                      <a href="/examples.php">Examples</a>
                                                      <a href="/about.php">About Us</a>
                                                      <a href="/contact.php">Contact Us</a>
                                                      </div>

                                                      <h1>Welcome to my home page!</h1>
                                                      <p>Some text.</p>

                                                      </body>
                                                      </html>

 

The visitor would actually see all the HTML code as one long line of HTML code, because we have not inserted any new line characters. We did some formatting above to make it easier to read.

The include command simply takes all the text that exists in the specified file and copies it into the file that uses the include command. Include is quite useful when you want to include the same PHP, HTML, or text segment on multiple pages of a website. The include command is used widely by PHP web developers. Like PHP Echo, include is not a function, but a language construct.

                                

  • PHP require() Function


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.

 

Remember: The require() function is identical to include(), except that it handles errors differently. If an error occurs, the include() function generates a warning, but the script will continue execution. The require() generates a fatal error, and the script will stop.

 

Error Example include() Function:

Code:

                                                         <html>
                                                         <body>

                                                         <?php
                                                                   include("wrongFile.php");
                                                                   echo "Hello World!";
                                                         ?>

                                                         </body>
                                                         </html>

 

Output with Error message:

                                          Warning: include(wrongFile.php) [function.include]: failed to open stream:
                                          No such file or directory in C:\home\website\test.php on line 5
                                         

                                          Warning: include() [function.include]:
                                          Failed opening 'wrongFile.php' for inclusion (include_path='.;C:\php5\pear') in C:\home\website\test.php on line 5

                                          Hello World!

 

Notice that our echo statement is still executed, this is because a Warning does not prevent our PHP script from running. On the other hand, if we did the same example but used the require statement we would get something like the following example.

 

Error Example require() Function:

Now, let's run the same example with the require() function.

Code:

                                                            <html>
                                                            <body>

                                                            <?php
                                                                      require("wrongFile.php");
                                                                      echo "Hello World!";
                                                            ?>

                                                            </body>
                                                            </html>

 

Output Error message:

                                          Warning: include(wrongFile.php) [function.include]: failed to open stream:
                                          No such file or directory in C:\home\website\test.php on line 5
                                         

                                          Warning: include() [function.include]:
                                          Failed opening 'wrongFile.php' for inclusion (include_path='.;C:\php5\pear') in C:\home\website\test.php on line 5

 

The echo statement is not executed, because the script execution stopped after the fatal error.

It is recommended to use the require() function instead of include(), because scripts should not continue after an error.