Press enter to see results or esc to cancel.

PHP file inclusion function include() vs require() vs include_once() vs require_once()
PHP
  

PHP file inclusion function include() vs require() vs include_once() vs require_once()


In PHP, insert the same PHP, HTML, or text on multiple pages using include(), include_once(), require() and require_once(). In this article we are going to see real differences between them with an example.


At first create a sample menu page.

menu.php
<?PHP
echo '<a href="home.php">Home</a> - <a href="about.php">About</a> - <a href="contact.php">Contact</a>';
?>

include function

When you want to include a file within the current execution by using include function. The code inside the included file will then run when the include function  called. Example as follows.

<?PHP
include('menu.php');
?>
<div class="content">Hello world!</div>

Output

Home - About - Contact 

Hello world!

require function

The require function exactly the same as the include function.

<?PHP
require('menu.php');
?>
<div class="content">Hello world!</div>

Output

Home - About - Contact 

Hello world!

Difference Between include() vs require()

In include(), file that you include is not found than PHP interpreter generated a warning and execution continues without that file.

<?PHP
include('menu.php');
include('menu1.php');
?>
<div class="content">Hello world!</div>

Output

Home - About  - Contact

Warning: include() [function.include]: Failed opening 'menu1.php'

Hello world!


In require(), file that you include is not found than PHP interpreter generated a Fatal Error and execution stops there.

<?PHP
require('menu.php');
require('menu1.php');
?>
<div class="content">Hello world!</div>

Output

Home - About - Contact 

Fatal error: require() [function.require]: Failed opening required 'menu1.php'

include_once function

The include_once function also just like the include function.

The difference between include() and include_once() are easily perceived by its names. The include() are allow files to insert multiple times in current process. The include_once() are allow files to inserted only once in the current process.

If you define any functions in the included file to avoid redefinition of a function you should use include_once.

<?PHP
include_once('menu.php');
include_once('menu.php');
?>
<div class="content">Hello world!</div>

Output

Home - About - Contact

Hello world!

In this I've include menu.php at two times. The output displayed only once.

require_once function

The require_once function is combination of the require and include_once function. It will make sure the file exists if not there it will throw a fatal error. Also it will make sure that the file can only be used once on the page. This function is strict out of the above functions. I've use this when constructing the page.   

<?PHP
require_once('menu.php');
require_once('menu.php');
?>
<div class="content">Hello world!</div>

Output

Home - About - Contact

Hello world!