Home > DeveloperSection > Blogs > File Inclusion in PHP

File Inclusion in PHP


PHP PHP  File 
Ratings:
0 Comment(s)
 138  View(s)
Rate this:

File Inclusion in PHP  


In PHP you can include the content of one file into another file. This will help in creating function, footers header or elements that can be used in several pages. If there is any changes needed in thousand of files just change included file. There are two PHP functions which are used to include the file:

·         The include() function

·         The require() function

·         The require_once function

·         The include_once function

Both these functions are used to include the file. Both are same the only difference between them is that if there is any problem in loading the file the require() function generates a fatal error and terminates the program.

The include() function

This function takes all the content into another file which has include() function. If there is any problem while loading a file than this function only generates the warning but not halt the program and continue with the execution.

Note: include will only produce a warning (E_WARNING) and continue with the execution.

Step 1: Create a file text.php which will be included.

XML = EXtensible Markup Language</br>

PHP = PHP Hypertext Preprocessor</br>

CSS = Cascading Style Sheets</br>

HTML = Hyper Text Markup Language</br>

SQL = Structured Query Language</br>

AJAX = Asynchronous JavaScript and XML</br>

SVG = Scalable Vector Graphics</br>

 

 

Step 2: Create a file include.php which will include the content of text.php

<?php

include("text.php");

echo '<b> file(text.php) is included in include.php file.</b>'

?>

 

Output:

XML = EXtensible Markup Language
PHP = PHP Hypertext Preprocessor
CSS = Cascading Style Sheets
HTML = Hyper Text Markup Language
SQL = Structured Query Language
AJAX = Asynchronous JavaScript and XML
SVG = Scalable Vector Graphics
file(text.php) is included in include.php file.

 

Note: The include_once() function: The include_once()  have the similar behavior to include() function, except that if the file is already included, it will not include the file again. As the name suggests, the file will be included just once.

The require() function

This function takes all the content into another file which has require() function. If there is any problem while loading a file than this function generates the fatal error and terminates the execution of program. It is recommended to use require() function instead of include() function because program should not continue its execution if there is a missing file.

Note: require() will produce a fatal error (E_COMPILE_ERROR) and stop the program.

You can try the above example using require() function it will generate the same result. Let’s try to include the file which does not exist.

<?php

include("t.php");

echo '<b> This example is used to show how include function had included the wrong PHP file which not even exist.</b>'

?>

 

Output:

Warning: include(t.php): failed to open stream: No such file or directory 

 

This example is used to show how include function had included the wrong PHP file which not even exist. // continue its execution

 

Using require() function

<?php

require ("t.php");

echo '<b> This example is used to show how include function had included the wrong PHP file which not even exist.</b>'

?>

 

Output:

It will show a fatal error and halt the program.

 

Note: The require_once() function: The require_once() have the similar behavior to require() function, except that if the file is already included, it will not include the file again. As the name suggests, the file will be included just once.


Don't want to miss updates? Please click the below button!

Follow MindStick