Files are included based on the file path given or, if none is given, the include_path specified. If the file isn't found in the include_path, include() will finally check in the calling script's own directory and the current working directory before failing. vars.php <?php $color = 'green'; $fruit = 'apple'; ?> test.php <?php echo "A $color $fruit"; // A include 'vars.php'; echo "A $color $fruit"; // A green apple ?> The require() function is identical to include(), except that it handles errors differently. If an error occurs, the include() function generates a warning, but the script will continue execution. The require() generates a fatal error, and the script will stop. <?php // file and get, if you need to include a file.php?query_string. function pinclude($file, $type, $get = null) { $p = explode('/', $file); $file = end($p); $dir = ''; $n = count($p) - 1; for($i = 0; $i < $n; $i++) $dir .= $p[$i] . '/'; if($get !== null) { $tmp = $_GET; // back up $_GET = array(); $get = explode('&', $get); $n = count($get); for($i = 0; $i < $n; $i++) { if(strpos($get[$i], '=') === false) $_GET[$get[$i]] = 1; else { list($name, $val) = explode('=', $get[$i], 2); $_GET[$name] = $val; } } } ob_start(); chdir($dir); require $file; $out = ob_get_clean(); if($tmp) $_GET = $tmp; return $out; } $out = pinclude('./dir/yourfile.php', 'a=b&c=d&e'); echo $out; // i'm sorry but i forgot post requests... ?>
if the included/required file does not exist, require returns a fatal error while include returns a notice only. fatal error will stop the whole script while notices will still continue to execute the script.