require_once() is like require() but includes the file at most once. That is, the second time a file is tried to be included with require_once(), it has no effect.
The files to be included are searched in the include path (whose current value is returned by get_include_path()).
If require_once() does not find the file to be included, it raises E_COMPILE_ERROR which cannot be caught in a try … catch construct. Therefore, one might consider using include_ince() instead.
<!-- http://www.adp-gmbh.ch/php/include_require.html -->
<html>
<head><title>require_once</title></head>
<body>
<?php
// http://stackoverflow.com/a/2418744/180275
require_once('required_file.php');
$num_1 = 10;
$num_2 = 20;
print "The average of $num_1 and $num_2 is " . avg($num_1, $num_2);
?>
</body>
This is the file that is required by required_once.
<pre>
If the included or required file contains php code, the code must be between
<?php ... > even though the include/require command is already contained
within <?php ... >.
Text that is not within those tags will be sent to the browser!
</pre>
<?php
function avg($a, $b) {
return ($a+$b)/2;
}
?>