简体   繁体   English

制作header.php文件

[英]Making a header.php file

I want to make a header file, but am not experienced with PHP in almost any aspect. 我想制作一个头文件,但是几乎没有任何方面的PHP经验 My uncle was telling me that I can use a header file with PHP, and that it was like a CSS with HTML. 我叔叔告诉我,我可以在PHP中使用头文件,就像在HTML中使用CSS。

The following is the HTML I want in my PHP: 以下是我在PHP中想要的HTML:

<center><nav>
<ul>
    <li><a href="/">Home</a></li>
    <li><a href="">Arcade</a>
        <ul>
            <li><a href="/arcade/action">Action</a></li>
            <li><a href="/arcade/arcade">Arcade</a></li>
            <li><a href="/arcade/puzzle">Puzzle</a></li>
            <li><a href="/arcade/vehicle">Vehicle</a></li>
            <li><a href="/arcade/violence">Violence</a></li>
            <li><a href="/arcade/defense">Defense</a></li>
            <li><a href="/arcade/rpg">RPG</a></li>
        </ul>
    </li>
    <li><a href="">Watch</a>
        <ul>
            <li><a href="/watch/tv">TV Shows</a></li>
            <li><a href="/watch/movies">Movies</a></li>
        </ul>
    </li>
    <li><a href="">Extras</a>
        <ul>
            <li><a href="/news">News</a></li>
            <li><a href="/updates">Updates</a></li>
        </ul>
    </li>
    <li><a href="/support">Support</a></li>
</ul>
</nav></center>

How could I incorperate that into a header file? 如何将其合并到头文件中?

I have looked at other websites such as W3Schools, but they don't seem to work. 我看过其他网站,例如W3Schools,但它们似乎不起作用。 I think my main issue is that I am not sure how to put the HTML into the PHP document. 我认为我的主要问题是我不确定如何将HTML放入PHP文档中。 When I do it, I try to type out the HTML as if it were header.html, only, ya know, it's not. 当我这样做时,我会尝试键入HTML,就像它是header.html一样,只知道,不是。

But anyways, if anybody could even just point me in the right direction of how to incorperate that HTML I included into a header.php file, and what to put in the html. 但是无论如何,如果有人能给我指出正确的方向,如何将我包含的HTML合并到header.php文件中,以及将哪些内容放入html中。

From what I have seen, the best scenario for including the PHP document in the HTML code is: 从我所看到的,将PHP文档包含在HTML代码中的最佳方案是:

<html>
<?php
    header('Location: http://www.example.com/');
?>

Right now, I'm putting that HTML in every single page. 现在,我将HTML放在每个页面中。 This is restricting me from making navigational tab changes! 这限制了我无法更改导航选项卡!

My website is http://www.gameshank.com/ 我的网站是http://www.gameshank.com/

Again, thanks in advanced, sooo much! 再次感谢您,谢谢!

Ah... the PHP header command is used to output a HTTP header, so I'm really not sure what you're hoping to achieve via its use. 啊... PHP 标头命令用于输出HTTP标头,因此我真的不确定您希望通过使用它来实现什么。

What you want to do is save your generic header HTML/PHP code into a new PHP file (perhaps called "header.php") and then include the contents of that file within each of your existing PHP pages via an include statement. 您要做的是将通用标头HTML / PHP代码保存到一个新的 PHP文件(可能称为“ header.php”)中,然后通过include语句将该文件的内容包含在您现有的每个PHP页面中。 For example: 例如:

<html>
<head><title>Sample HTML page</title></head>
<body>
    <?php include 'header.php'; ?>
    <h1>Page specific content</h1>
    <p>And stuff.</p>
</body>
</html>

By doing this, each page will automatically contain the contents of the header.php file (which will appear wherever you place the include statement) and any changes you require can simply be made to the header.php file. 这样,每个页面将自动包含header.php文件的内容(将在您放置include语句的任何位置出现),并且您需要的任何更改都可以简单地对header.php文件进行。

You definitely can! 你绝对可以!

On your main page (Lets say index.php), on the top you can put in 在您的主页上(让我们说index.php),在顶部您可以输入

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

(Or whatever your header file is called.). (或任何您的头文件被称为。)。

Put that HTML in header.php you want to include. 将HTML放入要包括的header.php中。

You have to use include like this 您必须使用这样的包含

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

or use require 或使用要求

<html>
<body>
<?php require("xxmenu.php"); ?>
<p>This is an example to show how to include PHP file!</p>
</body>
</html>

While you're at this, it's also a pretty good idea split the footer off of your page and place it in a seperate PHP file like you're doing with your header. 在这种情况下,将页脚从页面上分离出来并将其放置在单独的PHP文件中也是一个不错的主意,就像处理标头一样。

The point of this is it allows you to make changes to code that is featured in multiple pages throughout your site without having to go through every single page and make the same change. 这样做的目的是,您可以更改站点中多个页面上具有的代码,而不必遍历每个页面并进行相同的更改。

For instance if you have " Copyright (c) 2013 Blah Blah Blah " at the bottom of 50 pages on your site, when 2014 rolls around you don't want to edit 50 pages to change 2013 to 2014. You'd just make it once in "footer.php". 例如,如果您在网站上50页的底部具有“ Copyright(c)2013 Blah Blah Blah ”,则2014年临近时,您不想编辑50页以将2013年更改为2014年。一次在“ footer.php”中。

And like the others have said, simply include the file in your pages. 就像其他人所说的那样,只需将文件包含在页面中即可。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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