For PHP code to execute, two things have to happen:
- The filename needs to end in
.php
- The PHP code needs to exist between an opening PHP tag
<?php
and, optionally, a closing PHP tag?>
Contents of index.html
:
<html>
<head>
<!-- header content here -->
</head>
<body>
<h1>Page name</h1>
<p>Page content</p>
<footer>
<p>Contact info here</p>
</footer>
</body>
</html>
To seperate this out, we should consider which code we want to duplicate across every page, and move those over to separate files. We can make a new directory to hold those.
index.html
index.php
templates/
- header.php
- footer.php
The contents of index.php
change to:
<?php
include("templates/header.php");
?>
<h1>Page name</h1>
<p>Page content</p>
<?php
include("templates/footer.php");
?>
New file: templates/header.php
<html>
<head>
<!-- header content here -->
</head>
<body>
New file: templates/footer.php
<footer>
<p>Contact info here</p>
</footer>
</body>
</html>