Login

Username:

Password: 

Join us Now |  Forgot Password? | Forgot UserName?

PHP

Learn step by step



PHP Include function

The include (or require) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement.
Including files is very useful when you want to include the same PHP, HTML, or text on multiple pages of a website.
Suppose you have a text file that you want to include in a web page that you've already got up and running. You could copy and paste the text from the file straight into you HTML. Or you could use the include( ) function

<HTML>
<HEAD>
<TITLE>Include files</TITLE>
</HEAD>
<BODY>
<H3>Normal text here </H3>
Normal text written in a HTML Editor
<H3>Include File here</H3>
<?PHP include "textfile.txt" ; ?>
</BODY>
</HTML>
Our PHP code is in red. Here it is:
<?PHP
include "textfile.txt";
?>

The Require() function

he require() function is very similar to the include() function with one major difference -- with the include() function, if a file that you try to include is not found, the rest of the script will be executed anyway, while with the require() function, if a file that you try to include is not found, the rest of the script will not be executed. The require() function requires files.

<html>
<head>
<title>Include file</title>
</head>
<body>
<?php
//make an error in the file name
//so that it is not found
include ("headerr.php");
print "File not found but this text will be
printed anyway if you use an include() function,
not if you use a require() function";
?>
</body>
</html>
Warning: main(headerr.php): failed to open stream: No such file or directory in /home/site/folder/ on line 9 Warning: main(): Failed opening 'headerr.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/site/folder/ on line 9
File not found but this text will be
printed anyway if you use an include() function,
not if you use a require() function

In the above example, the rest of the script was executed even though the file headerr.php was not found.
Using the require() function, things would be different:

<html>
<head>
<title>Include file</title>
</head>
<body>
<?php
//make an error in the file name
//so that it is not found
require ("headerr.php");
print "File not found but this text will be
printed anyway if you use an include() function,
not if you use a require() function";
?>
</body>
</html>

Warning: main(headerr.php): failed to open stream: No such file or directory in /home/site/folder/ on line 9 Warning: main(): Failed opening 'headerr.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/site/folder/ on line 9

In the above example, the rest of the script was not executed because the file headerr.php was not found.

The require_once() function

A third function used to include files is the require_once() function. The require_once() function behaves the same way as the require() function with one difference -- the require_once() function allows the contents of a file to be included only once.



Related Videos