Difference between include, require, include_once, require_once in PHP

A C programmer often uses include statement in PHP,  but when you read professional PHP scripts, you may find require/require_once is more common. What are the differences between include, require, include_once, require_once?

The first thing to note for C programmer is that these four statements are executed(interpreted) at run time, not handled at compiling time as in C, so if there is something wrong with the included file(such as missing included file), the statements before the include/require statement are executed normally.

The difference between include and require is that if the included file is missing, include returns a warning and the execution continues, while require returns a fatal error and the execution stops. In the following example, test1.php has an include statement which includes myinclude.php.

test1.php

<?php
incude "myinclude.php";
echo "say hi from test1.php<br>";
?>

 

myinclude.php

<?php
echo "say hi from myinclude.php<br/>";
?>

The execution of test1.php has the following output:

say hi from myinclude.php

say hi from test1.php

 

If myinclude.php is missing, the output is:

Warning: include(myinclude.php): failed to open stream: No such file or directory in C:\xampp\htdocs\myweb\test1.php on line 2

Warning: include(): Failed opening ‘myinclude.php’ for inclusion (include_path=’.;C:\xampp\php\PEAR’) in C:\xampp\htdocs\myweb\test1.php on line 2
say hi from test1.php

We can see despite of the missing file, the echo statement in test1.php is executed normally.

Now we replace the include in test1.php with require:

test1.php

<?php
require "myinclude.php";
echo "say hi from test1.php<br>";
?>

The output is:

Warning: require(myinclude.php): failed to open stream: No such file or directory in C:\xampp\htdocs\myweb\test1.php on line 2

Fatal error: require(): Failed opening required ‘myinclude.php’ (include_path=’.;C:\xampp\php\PEAR’) in C:\xampp\htdocs\myweb\test1.php on line 2

You can see require throws a fatal error, the execution stops and the echo statement in test1.php is not executed.

The difference between include and include_once, require and require_once is that xxx_once only reads in and executes the included file for once, at the next time xxx_once is executed and if the included file name(the parameter) is the same, xxx_once does nothing(won’t read/execute the included file again). See the following example:

test1.php

<?php
require "myinclude.php";
require  "myinclude.php";
echo "say hi from test1.php<br>";
?>

 

The output is:

say hi from myinclude.php
say hi from myinclude.php
say hi from test1.php

 

Note that there are two lines of “say hi from myinclude.php “.  Now change require to require_once:

<?php
require_once "myinclude.php";
require_once "myinclude.php";
echo "say hi from test1.php<br>";
?>

The output is:

say hi from myinclude.php
say hi from test1.php

There is only one line of “say hi from myinclude.php “.  This is because the second require_once statement in test1.php does not execute myinclude.php. So in order to avoid to redeclare a class or a function in the included file, you should use include_once or require_once. Of course if you only include the file for once, both xxx and xxx_once are ok, and there is no obvious difference as to the speed or performance between them.

If you search Internet for the difference between include and include_once(or between require and require_once), you may get a misunderstanding that xxx_once is executed only once in a loop structure. However this is not accurate. It is somewhat true only when the parameter(the file name)  of the xxx_once keeps the same during the loop execution. If the file name changes during a loop, xxx_once will read/execute the different included files. To clarify, let us see the following example:

<?php
for($i=0;$i<2;$i++)
{
require_once "myinclude.php";
}
echo "say hi from test1.php<br>";
?>

The output is:

say hi from myinclude.php
say hi from test1.php

We can see although require_once is executed twice in the loop, there is only one line of “say hi from myinclude.php”. However, in the following example:

<?php
for($i=0;$i<2;$i++)
{
require_once "myinclude$i.php";
}
echo "say hi from test1.php<br>";
?>

The output is:

say hi from myinclude.php
say hi from myinclude.php
say hi from test1.php

Note that we have created two copies of myinclude.php(myinclude0.php, myinclude1.php), and the require_once is executed twice, outputing one line of “say hi from myinclude.php” each time. This example also tells us we can use variable in include/require statements.

The last thing to mention about include/require is that the included file is interpreted as html(and interpreter is changed back to php mode after the include/require statement) , so if you want to use php statement in the included file, do not forget to add the PHP language tag(<?php ?>).

 

 

Posted in

Leave a Reply