0

I want to use php to create a consistent header and footer across my site using the php incluse tags. When creating the header file, do I need the html and body tags or can I just start with the div id="header"....?

6 Answers 6

2

What you should worry about is the final outcome of the markup of the site, after you've included everything.

Example:

header.php

<div id="header"></div>

footer.php

<div id="footer"></div>

index.php

<html>
<head></head>
<body>
    <?php include('header.php'); ?>
    <div id="content"></div>
    <?php include('footer.php'); ?>
</body>
</html>
Sign up to request clarification or add additional context in comments.

2 Comments

Why not include html and body in header.php and footer.php rather than have the same code duplicated in all pages?
It's just an example. I like to do it that way, but the main worry is the final outcome.
1

For correct semantics, include the <html> and <body> tags, making sure to close them in the footer.php file

Comments

1

Yes you should. It's a good idea, though, to use variables within the include to set such things as <title>...

<?php 
$pagetitle="My page";
include('header.php');
?>
Content here
<?php include('footer.php'); ?>

where header.php is

<html>
<head>
<title><?php echo $pagetitle; ?></title>
<!-- your meta tags etc -->
</head>
<body>

and footer is

<script>/* your javascript includes */</script>
</body>
</html>

Comments

0

You can have a section of HTML in a file like this.

<footer> This is my global footer! </footer>

Then in PHP you can use include() to include that html file. It will render the contents of the file to the output where you include it.

include 'globalFooter.html';

Do I need the html and body tags or can I just start with the div?

No you do not.

Comments

0

Your question is not completely clear, but you seem to be asking whether you need any special tags in the included file. The short answer is no—the included file is inserted into the including file verbatim, so it would contain whatever tags are required to render the header (or footer) you had in mind.

Comments

0

Whatever you do, the result should be valid HTML.

So if your index.php starts with <?php include "header.php" ?> then your header.php file should start with <!DOCTYPE html><html>... Same goes for the end.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.