HTML and CSS Reference
In-Depth Information
Four include-related functions are built in to PHP: require , require_once , include , and
include_once . All these functions include an external file in the page being processed.
The difference between include and require is how PHP reacts when the file being
included isn't available. If include or include_once is used, the PHP page prints a
warning and continues on. If require or require_once is used, an unavailable include
file is treated as a fatal error and page processing stops.
If you use require_once or include_once to include a file that was already included on
the page, the function call will be ignored. If you use require or include , the file will
be included no matter what.
PHP includes are like HTML links in that you can use relative or absolute paths in your
includes. The difference is that absolute PHP paths start at the root of file system rather
than the web server's document root. So if you want to include a file using an absolute
path on a computer running Windows, you write the include like this:
require_once 'c:\stuff\myfile.php';
That's almost never a good idea. You should always use relative paths where possible. In
other words, if the included file is in the directory above the one where the including file
is located, you should use a path like this:
require_once “../myinclude.php”;
If the file being included is not stored with your other web documents, try to have that
directory added to your server's include path rather than using absolute paths to access it.
CAUTION
Never pass data entered by a user to any include function; it's a
big security risk. For example, this would be inappropriate:
require_once $_POST['file_to_include';
PHP includes can be useful even if you don't plan on doing any programming in PHP.
You can turn parts of your website that you use frequently into files to be included, sav-
ing you from having to edit the same content in multiple places when you're working on
your site. Using PHP includes this way can provide the same advantages that putting
your CSS and JavaScript into external files does. For example, you might create a file
called header.php that looks like this:
<!DOCTYPE html>
<html>
<head>
<title> <?= $title ?> </title>
<script src=”site.js”></script>
Search WWH ::




Custom Search