file inclusion


You can include the content of a PHP file into another PHP file before the server executes it. There are two PHP functions which can be used to included one PHP file into another PHP file.


  • require() function
  • include() function

This is a strong point of PHP which helps in creating functions, headers, footers, or elements that can be reused on multiple pages. This will help developers to make it easy to change the layout of complete website with minimal effort. If there is any change required then instead of changing thousand of files just change included file.


Include() function


The include() function takes all the text in a specified file and copies it into the file that uses the include function. If there is any problem in loading a file then the include() function generates a warning but the script will continue execution.


header.php to save this file extension


		  	<a href="http://www.studentsempire.com/index.php">Home</a> - 
			<a href="http://www.studentsempire.com/books.php">books</a> - 
		  

link.php to save this file extension


		  	<html>
			   <body>
			      <?php include("menu.php"); ?>
			      <p>This is an example to show how to include PHP file!</p>
			   </body>
			</html> 
		  


require() function


The require() function takes all the text in a specified file and copies it into the file that uses the include function. If there is any problem in loading a file then the require() function generates a fatal error and halt the execution of the script.


So there is no difference in require() and include() except they handle error conditions. It is recommended to use the require() function instead of include(), because scripts should not continue executing if files are missing or misnamed.


You can try using above example with require() function and it will generate same result. But if you will try following two examples where file does not exist then you will get different results.


		  	<html>
			   <body>
			      <?php include("header.php"); ?>
			      <p>This is an example to show how to include wrong PHP file!</p>
			   </body>
			</html> 
		  

Now lets try same example with require() function.


		  	<html>
			   <body>
			       <?php require("header.php"); ?>
			       <p>This is an example to show how to include wrong PHP file!</p>
			   </body>
			</html>
		  

This time file execution halts and nothing is displayed.


You may get plain warning messages or fatal error messages or nothing at all. This depends on your PHP Server configuration.




PHP Tutorials


Home

Introduction

Syntax

Variable

Comment

Echo and Print Statement

Data Types

Boolean

Integer

Number Convert

PHP String

php variable substitution in string

php string element

php string function

php addcslashes function

php addslashes function

php get magic quotes gpc function

php get meta tags function

php urlencode function

PHP $ and $$ Variables

PHP constant

PHP Magic Constants

PHP Static Variable

PHP Super Globals

PHP Globals

PHP Server

PHP reserved variable get

PHP reserved variable post

PHP reserved variable FILES

PHP reserved variable Cookies

PHP reserved variable Session

PHP reserved variable Request

PHP reserved variable environment

PHP Operator

PHP Arithmetic Operator

PHP Comparison Operator

PHP Incrementing and Decrementing Operators

PHP Identical Operators

PHP Logical Operators

PHP String Operators

PHP Ternary Operators

PHP Execution Operators

PHP Operator Precedence

PHP Array Operator

PHP Class

PHP Class Inheritance

PHP constructor and destructor

PHP abstract class

PHP class access control

PHP statement

PHP If statement

PHP else statement

PHP else if statement

PHP control structure statement

PHP while loop

PHP do while loop

PHP for loop

PHP for each loop

PHP break statement

PHP continue statement

PHP declare statement

PHP return statement

PHP require statement

PHP include

PHP require once

PHP include once

PHP goto statement

PHP function

PHP function parameter

PHP function scope

PHP array

PHP array operator

PHP array element loop

PHP array foreach loop

PHP array multidimensional

PHP html forms

PHP get and post method

form redirect

file inclusion

file i/o