包括外部php文件

栏目: PHP · 发布时间: 6年前

内容简介:翻译自:https://stackoverflow.com/questions/14079459/include-external-php-files

我正在使用一些 PHP 代码,如数据库连接,这对所有页面都很常见,所以我创建了一个包含php代码的php文件,然后我将这个文件包含在我的HTML代码中,

所以我想知道更好的方法来包含php文件,更好的替代include函数.

我的示例代码在这里

<?php
    include "check_timeout.php";
    include "connect_to_db.php";
?>
<html>
<head>
    <title>Example</title>
</head>
<body>
<?php
    mysql_close($con);
?>
</body>
</html>

先感谢您.

您有4个选项可供选择.

包括’yourfile.php’;

include_once’yourfile.php’;

要求’yourfile.php’;

require_once’yourfile.php’;

当然你也可以用“代替”.

他们都会做同样的事情,除了微小的差异.

如果yourfile.php不存在,则包含的将忽略该事实,并且您的php页面将继续 – 没有任何致命错误.

另一方面,需要的会产生致命的错误.

如果你知道那个文件在那里,你选择哪一个没什么区别.

至于带有_once后缀的选项,嗯,与它们的非_once后固定对应物相比,它们往往更慢.因为当你使用include_once或require_once时,PHP会做一些额外的工作来确保那些文件真正包含在ONCE中 – 保护你免受可能的双重包含情况,如果你不小心编码和许多文件使用许多包含你可能会遇到两次包含相同文件的情况.好吧,_once选项会阻止它.而且检查会带来一些处理成本.

我还注意到你已经使用了“而不是”用于文件分隔符.没有理由选择“over”,除非你在文件中引用变量名称,例如

$thefile =’yourfile.php;

包括“$thefile”;

那么从这4个中选出哪些?

这一切都取决于,如果你认为你确实需要强制_once问题,那么你选择include_once或require_once,如果你认为不需要,那么你选择include或require.至于include vs require,如果你的文件由于某种原因无法访问,那么你是否希望你的PHP脚本死掉或继续前进.

如果您对某些速度测试感到好奇,这里有一个链接供您查看.

http://php.net/manual/en/function.require-once.php

我也在主题上找到了这个.

Understanding the difference between require and include According to  the PHP manual, require and include “are identical in every way except  how they handle failure.” However, further reading of the manual  suggests another very subtle difference that impacts performance. When  you use the require keyword, the named file is read in, parsed, and  compiled when the file using the require keyword is compiled. When a  file containing the include keyword is compiled, the named file is not  read in, parsed, and compiled initially. Only when that line of code  is executed is the file read, parsed and compiled. Only use the  require keyword if you know you will always need that named file in  the current script. If you might use its functions, use include  instead. PHP opens up all files that are required, but only opens  included files as needed. Additionally, you should also consider using  require_once and include_once in place of require and include  respectively. In practice, it is more likely that you actually want  the functionality provided by the require_once and include_once  functions, even though it is much more common to use the require and  include keywords respectively. Refer to the following PHP manual pages  for more information: include, include_once
source:  07001

翻译自:https://stackoverflow.com/questions/14079459/include-external-php-files


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

可伸缩架构

可伸缩架构

【美】Lee Atchison / 张若飞、张现双 / 电子工业出版社 / 2017-7 / 65

随着互联网的发展越来越成熟,流量和数据量飞速增长,许多公司的关键应用程序都面临着伸缩性的问题,系统变得越来越复杂和脆弱,从而导致风险上升、可用性降低。《可伸缩架构:面向增长应用的高可用》是一本实践指南,让IT、DevOps和系统稳定性管理员能够了解到,如何避免应用程序在发展过程中变得缓慢、数据不一致或者彻底不可用等问题。规模增长并不只意味着处理更多的用户,还包括管理更多的风险和保证系统的可用性。作......一起来看看 《可伸缩架构》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

URL 编码/解码
URL 编码/解码

URL 编码/解码

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具