PHP Include

Using PHP in the simplest sense is to use includes. Includes call multiple files together to form one static HTML base document. PHP is a server side language and thereby the language is processed prior to leaving the sending the server. So by the time the file gets to your browser it’s already full HTML. When using PHP without a database and just for the includes and mathematical logic the page won’t be fully dynamically created but at lest it will decease reputation in the documents.

Easy example:
Index.php

<?php include('header.php'); ?>

<div id="content">
	<p></p>
</div>

<?php include('footer.php'); ?>

header.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>PHP Include</title>

<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
</head>

<body>
<div id="header">
	<ul id="navbar">
		<li><a href="#">link 1</a></li>
		<li><a href="#">link 2</a></li>
		<li><a href="#">link 3</a></li>
		<li><a href="#">link 4</a></li>
		<li><a href="#">link 5</a></li>
		<li><a href="#">link 6</a></li>
	<!--End ID="navbar"-->
	</ul>
<!--End ID="header"-->
</div>

footer.php

<div id="footer">
	<p></p>
<!--End ID="footer"-->
</div>

</body>
%lt;/html>

Upon being called by the user, the server reads the php and grabs the file in the include. Then places the information inside the file where the include was. Then the file leaves the server and is downloaded to the users browser.

In the end the browser reads this as one document and not 3 separate documents:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>PHP Include</title>

<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
</head>

<body>
<div id="header">
	<ul id="navbar">
		<li><a href="#">link 1</a></li>
		<li><a href="#">link 2</a></li>
		<li><a href="#">link 3</a></li>
		<li><a href="#">link 4</a></li>
		<li><a href="#">link 5</a></li>
		<li><a href="#">link 6</a></li>
	<!--End ID="navbar"-->
	</ul>
<!--End ID="header"-->
</div>

<div id="content">
	<p></p>
</div>

<div id="footer">
	<p></p>
<!--End ID="footer"-->
</div>

</body>
%lt;/html>

If you want to make sure the included file is add before anything following it, then use require(). Require works like include however if the file that it is looking for is not found the page render is stopped and nothing will follow, unlike the include which if not found just skips over it.

<html>
<body>

<?php
include("wrongFile.php");
echo "Continue Content";
?>

</body>
</html>

Will render:

Warning: include(wrongFile.php) [function.include]:
failed to open stream:
No such file or directory in http:\www.website.com\file.php on line 5

Warning: include() [function.include]:
Failed opening ‘wrongFile.php’ for inclusion
(include_path=’.;http:\php5\pear’)
in http:\www.website.com\file.php on line 5
Continue Content

rather then

<html>
<body>

<?php
require("wrongFile.php");
echo "continue content";
?>

</body>
</html>


Warning: require(wrongFile.php) [function.require]:
failed to open stream:
No such file or directory in http:\www.website.com\file.php on line 5

Warning: require() [function.include]:
Failed opening ‘wrongFile.php’ for inclusion
(require_path=’.;http:\php5\pear’)
in http:\www.website.com\file.php on line 5







Leave a Reply



blog