PHP中最有用的函数:include

In most websites you have areas of content that are the same from one page to the next. These sections might include:

在大多数网站中,一页到下一页的内容区域相同。 这些部分可能包括:

The appearance of this content is commanded byCSS, which allows us to maintain rules for the consistent presentation of content across a site. However, the content itself is also often exactly the same: for example, you may always have the same code immediately after the opening body tag on every page:

此内容的外观是由CSS命令的,它使我们能够维护规则,以在整个站点上一致地呈现内容。 但是, 内容本身也常常是完全相同的:例如,您可能总是在每个页面的开始body标记后紧接着具有相同的代码:

<h1>Hannibal’s Bait &amp; Tackle Shop</h1>
<h3>Duluth, North Dakota</h3>

To make this content the same between all pages, most so-called web developers copy and paste the same code from one HTML page to the next. The approach works well so long as the content never changes. But if the site in our example decided to offer boat rental, and changed their business name to “Hannibal’s Bait, Boat & Tackle Store”, we would have a massive amount of editing to do site-wide. We can avoid this problem entirely – and speed up web development by 10 - 50% – by using PHP includes from the very beginning.

为了使所有页面之间的内容相同,大多数所谓的Web开发人员都将相同的代码从一个HTML页面复制并粘贴到下一个HTML页面。 只要内容永不更改,该方法就行之有效。 但是,如果在我们的示例中,该站点决定提供租船服务,并将其业务名称更改为“ Hannibal's Bait,Boat&Tackle Store”,我们将在整个站点进行大量编辑。 通过从一开始就使用PHP包含,我们可以完全避免此问题,并将Web开发速度提高10%到50%。

(It is possible to “shim” PHP includes into an already extant site, but doing so usually takes considerably more work: like most things, PHP includes are a technique that deliver their greatest value if you use them right the first time, when you are initially creating a site.)

(有可能“将” PHP包含项“填充”到一个已经存在的站点中,但是这样做通常会花费更多的工作:像大多数事情一样,PHP包含项是一种技术,如果您第一次正确使用它们,便可以发挥最大的价值。最初正在创建网站。)

Every server-side scripting language features some form of includes: you will also hear them referred to as server-side includes or SSI’s. In PHP, the process of using them goes something like this:

每种服务器端脚本语言均具有某种形式的包含:您还将听到它们称为服务器端包含或SSI。 在PHP中,使用它们的过程如下:

  1. Make sure that the page you want to use the include on has a .php extension. (Note that if a server is confronted with an index.html and index.php page in the same location, it will preferentially choose the first. Be very careful when substituting PHP pages for HTML ones.)

    确保要使用include的页面具有.php扩展名。 (请注意,如果服务器在同一位置遇到index.htmlindex.php页面,则它将优先选择第一个。在用PHP页面替换HTML页面时要格外小心。)

  2. Usually your content already exists on the page. Cut (CTRL-X) this content, taking note of its location in the code.

    通常,您的内容已经存在于页面上。 剪切( CTRL-X )此内容,并注意其在代码中的位置。

  3. Make a new HTML page.

    制作一个新HTML页面。
  4. Remove all code from the new page. An include takes everything from the include file and puts it into place on the main PHP page: we only want our snippet of code to be placed.

    从新页面中删除所有代码。 包含将包含文件中的所有内容放到PHP主页上:我们只希望放置代码片段。

  5. Paste (CTRL-V) the code you copied from the previous page.

    粘贴( CTRL-V )从上一页复制的代码。

  6. Save the HTML page with the snippet of code, with a filename that follows naming conventions: for example, header.html It is typical (but not required) to save the page in an includes sub-folder inside an assets folder.

    使用代码段保存HTML页面,并使用遵循命名约定的文件名:例如, header.html通常(但不是必需)将页面保存在Assets文件夹内的include子文件夹中。

  7. Return to the original PHP page. In the exact location where the HTML code was removed, write the following (assuming that the assets folder is directly beside the PHP page):

    返回原始PHP页面。 在删除HTML代码的确切位置,输入以下内容(假设assets文件夹直接位于PHP页面的旁边):

    <?php include('assets/includes/header.html'); ?>
  8. Make sure that both the PHP page and the include file, along with any directories, are correctly uploaded to the server, and test them in the browser.

    确保PHP页面和包含文件以及所有目录均正确上传到服务器,并在浏览器中对其进行测试。
  9. Make another PHP page that uses the same code to include the same file.

    制作另一个使用相同代码包含相同文件PHP页面。
    1. The functions differ the way in which they handle failure. If include fails to find the file, the rest of the script proceeds as normal, and a printed warning is added to the page location where the include was written. The rest of the page will appear normal.

      这些功能处理故障的方式不同。 如果include找不到文件,脚本的其余部分将照常进行,并将打印警告添加到写入include的页面位置。 页面的其余部分将显示正常。

      On the other hand, if require fails to find the file it is directed to, it causes a fatal error. All execution of the page stops, and the result is likely to be a fatal error message on an otherwise blank page.

      另一方面,如果require找不到指向的文件,则会导致致命错误。 该页面的所有执行都将停止,结果可能是致命的错误消息,否则显示为空白页面。

    2. include may be used as the action of a condition (i.e. inside an if statement.) However a file called by require will always appear on the page, even if it is placed inside a condition in which an argument is not met.

      include可以用作条件的操作(即,在if语句内部。)但是,即使将require调用的文件放在不满足参数的条件内,该文件也会始终出现在页面上。

    Note that a similar function to include exists in PHP, in the form of require. Both include and require work exactly the same way, with two exceptions:

    注意,PHP中include一个类似的功能,形式为require 。 两者都includerequire工作的方式完全相同,但有两个例外:

    For these reasons, I usually stick to include to do a server-side include on a PHP page.

    基于这些原因,我通常坚持include做服务器端包含一个PHP页面上。

    家庭作业 (Homework)

    Using your site, find areas which can be extracted as separate, centralized files, in the form of included content, such as navigation bars, logos, etc. Try to look for lines of continuous consistent content, rather than thinking necessarily of “header” or “footer”. Make this content includes in PHP pages. Remove the original HTML files from your site. The PHP pages should still validate when viewed in the browser.

    在您的网站上,找到可以提取为包含内容(如导航栏,徽标等)形式的单独的集中文件的区域。尝试查找连续一致的内容行 ,而不一定要考虑“标题”或“页脚”。 使此内容包含在PHP页面中。 从您的站点中删除原始HTML文件。 在浏览器中查看时,PHP页面仍应验证。

    太过分了 (Taking things too far)

    It is common for overly-ambitious coders to see the potential of include and go somewhat overboard, with the equivalent of the statement “Wow! All my pages down to the opening <body> tag are the same code, so I’ll just make all of that code one giant include!” In doing so, they are forgetting that the <title> should be different on every page, and includes, as we have used them here, don’t differentiate between pages they are used on. There are ways around this, but for now, think of consistent content in the body alone as being suitable for includes.

    雄心勃勃的编码人员通常会看到include和超出某些范围的潜力,这与“哇! 我的所有直到开始的<body>标签的页面都是相同的代码,因此,我将所有这些代码都include一个巨人!” 这样一来,他们就忘记了<title>在每个页面上都应该有所不同,并且由于我们在此处使用过它们,因此请不要在使用它们的页面之间进行区分。 可以通过多种方法解决此问题 ,但就目前而言, 仅将体内一致的内容认为是适合包含在内的即可。

    替代品包括 (Alternatives to includes)

    Many dynamic scripting languages feature their own alternatives to include, as do other server-side solutions and programs. , for example, uses what it refers to as “templates” to achieve the same ends. The problem with proprietary solutions is that you must use continue to use DreamWeaver (as an example) to edit template files from that point forward. And, in my experience, when proprietary solutions go bad, they go really bad. There can be compelling reasons to use a proprietary solution, but the fact that you can use a (or anything else) to change PHP remains a strong argument in its favour.

    许多动态脚本语言都具有自己的替代方案, include ,其他服务器端解决方案和程序也是如此。 例如, 使用它所谓的“模板”来达到相同的目的。 专有解决方案的问题在于,从那时起,您必须使用继续使用DreamWeaver(例如)来编辑模板文件。 而且,以我的经验,当专有解决方案变坏时,它们真的变坏了。 使用专有解决方案可能有令人信服的理由,但是您可以使用 (或其他任何方式)来更改PHP的事实仍然是一个强烈的理由。

    Long term, PHP includes are likely to replaced by HTML5 import and template, but support for those modules has only just started, as of this writing, and it will be many years before they can be relied upon.

    从长远来看,PHP包含的内容很可能会被HTML5 importtemplate所取代,但是截至撰写本文时,对这些模块的支持才刚刚开始,要依赖它们还需要很多年。

翻译自: https://thenewcode.com/177/The-most-useful-function-in-PHP-include

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值