PHP - Using the include and require functions
From Global Programming Syntax
Comparing the functions
The require() and include() functions are almost identical in every way except for how they handle errors. If the included function includes a file that does not exist then usually a warning message will display then it will just skip the include and continue processing the rest of the script. If however the require function can't find a file, then a fatal error is displayed then the script may stop processing. Below is an example script you can try to see the difference:
include('file.does.not.exist');
echo '<hr>';
require('file.does.not.exist');
echo '<hr>This will display if previous error was not fatal.';
So in other words it is just the error reporting that is different where as the require function has better error reporting. So when deciding which function to use, generally it is best to use the include function on the finished result but in development the require function may come in handy to see if files are being included. The reason, normally on a finished result you don't want users to be able to see the errors displayed on your site. So since the include function has no or lower level error reporting depending on the configuration it would be better to use the include for public viewing.
Using include and require
There is practically no difference between the usage of the include() and require() functions. As mentioned earlier, it is only the error reporting that is different. So to use these functions simply just specify a filename in the first parameter field and it will include it. An example is as follows:
include('test.php');Then all of the code will be included and all of the html output will be included. Also it is possible to add return data from the included/required files. An example of what is meant by return data is expressed in the following example. First included.php
<?phpindex.php is as follows and will assign to the variable the word 'testing':
echo 'test';
return 'testing';
?>
<?php
$variable=include('included.php');
echo $variable;
?>
