简体   繁体   中英

PHP headers and footers

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"....?

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>

为了获得正确的语义,请包含<html><body>标记,并确保在footer.php文件中将其关闭

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.

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.

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.

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM