Sunday, 6 April 2014

Including multiple PHP files.

Use of Including files.

  • Including multiple PHP files helps Maintainability
  • Huge blocks of code can be split into small useful blocks which can be included wherever necessary.
  • Re-usability of code.

How To 

  1. Using Require.
    • Produces a fatal-error (E_COMPILE_ERROR) and stops the script if the Code fails to load the Script file.
  2. Using Include
    • Produces a warning (E_WARNING) and Resumes the script upon failure. 
Based on Requirements choose whether to use Require or Include. 
 

 Syntax :  

<?php include 'script_name.php '  ?>
<?php require 'script_name.php'  ?>

Example


<!-- test.php --> 

echo " Hello World <br/>" ;
echo "This Message is Displayed  by the Included file test.php <br/> " ;



<!-- Main.php -->
<?php 
    echo "This is Displayed from main.php <br/>";
    include "test.php";
?> 

 Output

This is Displayed from main.php
Hello World
This message is Displayed from Test.php


For more Information refer
  1. http://www.w3schools.com/php/php_includes.asp
  2. http://www.php.net/manual/en/function.include.php
 

No comments:

Post a Comment