2

I have a website that I am building and I am planning to use php inlude for the header and footer.

Lets say the header is <html>.....</html

Footer is <html>......</html>

What happens with the beginning and ending of html tags on the same page?

Is there gonna be a problem?

Can there be two open and end tags on the same page?

header

footer

3
  • You really should strive for valid HTML. Also, there's no need to have those tags on your header and footer so I am unsure why you even have them there in the first place. Commented Oct 14, 2015 at 1:10
  • So John I should code: <html>php include (header)..... body.....php include(footer) </html>? Commented Oct 14, 2015 at 1:12
  • Yes. Your PHP includes should only generate the HTML you need to replace the PHP block with. For example, in <html><head><% include(header.php); %></head><body>Hello</body></html>, your header.php file would only need to output something like echo "<title>Hello!</title>";. It's been an awful long time since I did PHP so the syntax might not be right, but the idea is there! Commented Oct 14, 2015 at 2:42

2 Answers 2

3

If you are going to use "include", "require" or "require_once" on your page... the included file should contain ONLY valid markup. Think of your include files as what you would normally write between the <body> and </body> tags.

EXAMPLE:

index.php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<?php require'includes/header.php' ?>

my body content goes here

<?php require'includes/footer.php' ?>
</body>
</html>

header.php

<div id="header">
<div><img src="/where/my/image/is/filename.jpg" alt="my header image" title="my image title" /></div>
<div id="menu">this is where I will put my menu</div>
</div>

footer.php

<div id="footer">this is where I will put my footer content</div>

Notice that in the header.php and footer.php files the following lines have been removed...

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>

</body>
</html>

Multiple <head>, <html>, <body> tags are generally ignored by modern "smart" browsers. They will just "rewrite your code for you" based on what the browser "thinks" you meant to write. BUT will it be correct!?

Do not rely on browsers to "fix" your mistakes. Write good, clean, valid code.

Sign up to request clarification or add additional context in comments.

Comments

0

Your include header should start with

<!DOCTYPE html>
<html>
<head>
some meta tags, css and js
</head>

And you footer should end with

</html>

Here's an example of a small html page structure

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>

<h1>My First Heading</h1>
<p>My first paragraph.</p>

</body>
</html> 

Example Explanation:

The `DOCTYPE` declaration defines the document type to be HTML
The text between `<html>` and `</html>` describes an HTML document
The text between `<head>` and `</head>` provides information about the document
The text between `<title>` and `</title>` provides a title for the document
The text between `<body>` and `</body>` describes the visible page content
The text between `<h1>` and `</h1>` describes a heading
The text between `<p>` and `</p>` describes a paragraph

UPDATE

My questions is about having multiple opening and ending tags on a page because of php include

An HTML document can only have ONE html tag. If you just put several HTML together, it will be an invalid document, and the browsers may have problems displaying it.

1 Comment

Hi Pedro, I know about those descriptions. My questions is about having multiple opening and ending <html> </html> tags on a page because of php include.

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.