codescracker


php

PHP File Inclusion



« Previous Tutorial Next Tutorial »


You are free to include any PHP file into another PHP file before the server executes it. There are two functions available in PHP helps in including the content of one PHP file into another PHP file:

Both the above function are used to include one PHP file into another.

Difference between include() and require() Function

As, both the function used to include one file into another in PHP, but there is one difference between these two function.

The difference is, when you use require() function to include any PHP file and if that file is not exist, then the script shouldn't continue executing further whereas in case of using include() function for the same purpose and again if the file is not exist, then the script should continue executing.

PHP Include File using include() Function

Let's create a file named main-menu.php and put the following content inside it.

<?php 
	echo "<ul>";
		echo "<li>PHP File Inclusion Tutorial</li>";
		echo "<li>PHP File Inclusion Examples</li>";
		echo "<li>PHP include() Function Tutorial</li>";
		echo "<li>PHP include() Function Example</li>";
		echo "<li>PHP require() Function Tutorial</li>";
		echo "<li>PHP require() Function Example</li>";
	echo "</ul>";
?>

And save this file in the same directory, that is, C:\xampp\htdocs\.

Now create another file named content.php and put the following content inside it.

<?php 
	echo "<p>This is the tutorial and example 
		of PHP file inclusion using include() 
		and require() function.</p>";
	echo "<p>Here you will learn about the 
		include() and require() function of 
		PHP used to include one file into another.</p>";
?>

And save this file in the same directory as provided above.

Now create your main PHP file where you are going to include the above two files. Create a file named codescracker.php and put the following content inside it.

<html>
<head>
	<title>How to include one PHP file into another</title>
</head>
<body>
<h2>Main Menu</h2>
<?php 
	include("main-menu.php");
?>
<h2>Content</h2>
<?php 
	include("content.php");
?>
</body>
</html>

Now save this file, and type localhost/codescracker.php in your browser, you will see the following output:

php file inclusion

Now replace the content of file codescracker.php with following:

<html>
<head>
	<title>How to include one PHP file into another</title>
</head>
<body>
<h2>Main Menu</h2>
<?php 
	include("main-menu.php");
?>
<h2>Content</h2>
<?php 
	include("unknown.php");
?>
<p>Ending...</p>
</body>
</html>

As the file unknown.php is not exist inside current directory, therefore the output will be:

php file include

You can see the paragraph with content Ending... in last of the web page after the error message.

The error message is displaying because i have included a file that is not exist inside the current directory.

If you don't want to see or display the error message then place the following code at beginning of the file codescracker.php.

<?php 
	error_reporting(1); 
?>

After adding the above code in the same program for unknown.php file inclusion, your whole program will looks like:

<?php 
	error_reporting(1); 
?>
<html>
<head>
	<title>How to include one PHP file into another</title>
</head>
<body>
<h2>Main Menu</h2>
<?php 
	include("main-menu.php");
?>
<h2>Content</h2>
<?php 
	include("unknown.php");
?>
<p>Ending...</p>
</body>
</html>

Now the sample output of the above file inclusion example code will be:

how to include one php file into another

PHP Include File using require() Function

As you have already create the two files named main-menu.php and content.php, therefore you don't have to create these two PHP files. But you have to replace the content of main PHP file with following content.

<?php 
	error_reporting(1); 
?>
<html>
<head>
	<title>How to include one PHP file into another</title>
</head>
<body>
<h2>Main Menu</h2>
<?php 
	require("main-menu.php");
?>
<h2>Content</h2>
<?php 
	require("content.php");
?>
<p>Ending...</p>
</body>
</html>

The output of above example of file inclusion using require() function in PHP is shown here:

php include function

Now replace the content of main PHP file with following content:

<?php 
	error_reporting(1); 
?>
<html>
<head>
	<title>How to include one PHP file into another</title>
</head>
<body>
<h2>Main Menu</h2>
<?php 
	require("main-menu.php");
?>
<h2>Content</h2>
<?php 
	require("unknown.php");
?>
<p>Ending...</p>
</body>
</html>

Now this time, you will not see the paragraph containing Ending... as you are including a non-existing file named unknown.php with require() function in PHP.

Here is the sample output produced by the above file inclusion example code in PHP:

php require function

« Previous Tutorial Next Tutorial »




Tools
Calculator

Quick Links
Signup - Login - Give Online Test