• Have Any Question?
  • contact@eitworld.com

PHP Include File





  • There are some statements in php which can include the content of other files in the current php files. The included file may be another php file or an html file or a text file etc.

Advantage

Including a file saves a lot of time-

  • If there is some part which is in many of your web pages, you can write this part in a separate file and then include it to all the web pages. Thus you don’t have to write it again and again.
  • If you want to change a code that is common to many web pages, it will take a lot of time if you have to change it in each of the web page. On other hand, if you have created a separated file for that code and included it to all the web pages, you just have to change it in that file and the change will be in effect in all the web pages that include this file.
  • Including files is very useful when you have to make a header, footer or a menu common to many web pages.

How to include files

There are two statements in php which are used to include files-

  • include
  • require

Syntax


include 'filename';
or

include(‘filename’);

require ‘filename’;

or

require(‘filename’);

After compilation, these statements are completely replaced by the content of the file that is included.

Difference Between include and require

  • If the file including is executed successfully, there is no difference between include and require. The difference comes in sight when file including is failed.
  • If file including gets failed then:
    • include gives a warning and compiles rest of the script whereas
    • require gives a fatal error and stops the script immediately.
  • If you want to continue the script even if a file that is to be included is missing, use include statement.
  • If you are dealing with frameworks or some secured page, use require statement to immediate stop the script if a file is missing.

Examples

Including HTML file

    • First write the php file in which the other file will be included. Suppose it is saved by the name sample1.php

<!DOCTYPE html>
<html>

<body>

<?php

include ‘sample.html’;

echo “Above heading has come from included file.”;

?>

</body>

</html>

    • It has two statements in its php code. First one is an include statement which will try to include sample.html file. The second statement is a simple echo statement.
    • Now create the sample.html file in same directory where the above php file (i.e. sample1.php) is located.
    • <h1>This heading is in html file</h1>
    • On running the sample1.php file, following output will be obtained.

Outputinclude

  • Thus the include statement has been replaced by the content of the html file.

Including PHP file

    • First write the php file in which the other file will be included. Suppose it is saved by the name sample1.php.

<!DOCTYPE html>
<html>

<body>

<?php

echo $my_var;

?>

</body>

</html>

    • In echo statement there is a variable $my_var, which has not been given any value. Thus on running the file, following error will be displayed:The second statement is a simple echo statement.

include

    • Now we include a file named sample2.php before the echo statement.

<!DOCTYPE html>
<html>

<body>

<?php

include ‘sample2.php;

echo $my_var;

?>

</body>

</html>

    • Now define the variable $my_var in sample2.php (the file that is included).
    • On running the sample1.php file, following output will be obtained.

Output

5
  • Thus the variable that is defined in sample1.php file, is now defined in sample1.php.


Real Time Web Analytics