Inspiration For Creative Minds
Home » Labs » How to include php headers on html site  

Filed under Labs

PHP is a powerful language for web development, it have many in built functions. Here is a post which explains how to include php headers on html site. Including the header dynamically is a good idea because with single file edit you can make changes on entire website. For example think there are hundreds of pages in a website and want to edit the menu, what will do? is it easy to edit all pages one by one, so such situation you can include a dynamic header on the site and the common header will call on each page, so if you want to edit the menu just edit the header template and it will reflect changes on entire website. We can understand it more clearly with below example.

Creating header.php

First you need to create a PHP page called header.php an add below html codes to it and save in root folder (You can save it wherever you want, in this example we assume its is saved on root folder of server.)

<nav role="navigation">
    <a href="index"><img src="images/logo.png" alt="logo" /></a>
    <ul>
        <li><a href="home.php">Home</a></li>
        <li><a href="about_us.php">About Us</a></li>
        <li><a href="services.php">Services</a></li>
        <li><a href="contact_us.php">Contact</a></li>
    </ul>
</nav>

PHP including method

To include you can use the ‘include’ function of PHP, it looks like below.

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

Below is the full html code which looks like after including the header template.

	<!doctype html>
	<html>
		<head>
		<!-- Add metadata and styles here -->
		</head>
		<body>
			<header>
				<?php include 'header.php'; // assuming its is in same folder, otherwise add exact path?>
			</header>
			<main role="main">
				<!-- Your main content -->
			</main>
			<footer>
				<!-- Your footer content -->
			</footer>
		</body>
	</html>
It's only fair to share...Share on Google+Tweet about this on TwitterShare on Facebook
Labs

Posts you may like: