Including PHP in HTML
From Wiki
Using include and require
You can add PHP to XHTML by putting it in a separate file and calling it by using PHP's include functions. There are four include functions:
-
include('/filepath/filename') -
require('/filepath/filename') -
include_once('/filepath/filename') -
require_once('/filepath/filename')
include() and include_once() will merely generate a warning on failure, while require() and require_once() will cause a fatal error and termination of the application execution.
As suggested by the names of the functions, include_once() and require_once() differ from simple include() and require() in that they will allow a file to be included only once per PHP script. This is extremely helpful when you are including files that contain PHP functions, because redeclaring functions results in an automatic fatal error. In larger PHP systems, it is quite common to include files which include other files which include other files-it can be difficult to remember whether you have included a particular function before, but with include_once() or require_once() you don't have to.
We need an example
Common headers and footers
One of the most common use of PHP's include capability is to add common headers and footers to all the Web pages on a site. For example, a simple header file (usually named header.inc) might look like this:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>A site title</title>
</head>
<body>
Similarly, a footer file called footer.inc might consist of:
<hr /> <p>Copyright 2005 - 2008</p> </body> </html>
They are called from a PHP page this way:
<?php require_once($_SERVER['DOCUMENT_ROOT'].'/header.inc'); ?> <p> This is some body text for this particular page. </p> <?php require_once($_SERVER['DOCUMENT_ROOT'].'/footer.inc'); ?>
This enhances the maintainability and scalability of an entire site. If you want a different look and feel or if you need to update the copyright notice, you can alter one file instead of identical lines in dozens of HTML pages.
PHP's include functions simply pass along the contents of the included file as text. Some people think that because an include function occurs inside PHP mode, the included file will also be in PHP mode. This is not true! Actually, the server escapes back into HTML mode at the beginning of each included file and returns to PHP mode at the end, just in time to catch the semicolon. As always, you need to say when you intend something to be PHP by using PHP opening and closing tags. Any part of an included file that needs to be executed as PHP should be enclosed in valid PHP tags. If the entire file is PHP (very common in files of functions), the entire file must be enclosed within PHP tags.
Note: For all PHP files included from other files, you must ensure that there are no empty new lines at the end of the file. Anything outside a PHP block is considered XHTML, even a blank line. Blank lines, or even blank spaces outside a closing PHP tag, will be interpreted as output.See also: PHP Templates

