包括外部的php文件 [英] include external php files

查看:80
本文介绍了包括外部的php文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一些php代码,如所有页面常见的数据库连接,所以我创建了一个包含php代码的php文件,然后将该文件包含在我的HTML代码中,
所以我想要了解包含php文件的更好方法,更好地替代include函数。
我的示例代码在这里

 <?php 
includecheck_timeout.php;
包含connect_to_db.php;
?>
< html>
< head>
< title>示例< / title>
< / head>
< body>
<?php
mysql_close($ con);
?>
< / body>
< / html>

预先感谢您。

解决方案

您有4个选项可供选择。



include'yourfile.php';

include_once'yourfile。 php';

需要'yourfile.php';

require_once'yourfile.php';当然你也可以使用而不是。

他们都会做同样的事情,除了轻微的如果yourfile.php不存在,那么include文件将忽略这个事实,并且你的php页面将继续前进 - 没有任何致命错误。

$ / $> $ / $> $ / $> b
$ b

如果你知道那个文件在那里,那就没有区别了,因为b
$ b

你可以选择哪一个。



至于_once后缀的选项,那么它们往往会比它们后缀的无_once更慢。 include_once或require_once,PHP会做一些额外的工作,以确保这些文件真正包含在一次 - 如果您不小心编码并且许多文件使用很多包含,并且您可能遇到同一文件包含两次,那么,_once选项会阻止这种情况发生,并且这种检查将会通过e有一些处理成本。



我也注意到你已经使用了而不是作为你的文件分隔符,没有理由选择over,除非你指的是变量名您的文件,如



$ thefile ='yourfile.php;
包含$ thefile;

那么哪个挑选出这4个?
这一切都取决于,如果您认为您确实需要强制_once问题,那么您选择include_once或require_once,如果您认为不需要,那么您可以使用include或require。至于包含vs要求,这一切都归结为您希望您的PHP脚本死掉或继续前进,如果您的文件由于某种原因无法访问。


如果您对某些速度测试感到好奇,可以点击此链接查看。
http://php.net/manual/en/function.require-once.php



我还在主题上发现了这一点。
$ b


了解require和include之间的区别根据
,PHP手册require和include除了
他们如何处理失败。但是,进一步阅读手册
则表明另一个影响绩效的非常微妙的差异。当
使用require关键字时,指定的文件被读入,解析,编译时使用require关键字编译
。当编译包含include关键字的
文件时,指定的文件不是
,最初读入,解析和编译。只有当执行那行代码
时,才会读取,解析和编译文件。只有使用
require关键字时,如果您知道您始终需要
中的该命名文件作为当前脚本。如果您可能使用其功能,请使用include
代替。 PHP打开所有需要的文件,但只会根据需要打开
包含的文件。此外,您还应该考虑使用
require_once和include_once来代替require,并分别包含
。在实践中,实际上更希望
是由require_once和include_once
函数提供的功能,尽管分别使用require和
include关键字更为常见。有关更多信息,请参阅以下PHP手册页
:include,include_once

来源:
http://www.stevengould.org/portfolio/developerWorks/efficientPHP/wa-effphp/wa-effphp-a4.pdf



I am using some php code like database connection which is common for all the pages, so I created a php file which contains the php code and then I am including this file in my HTML code, So I want to know the better way to include php file, better alternative for include function. my example code is here

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

Thank you in advance.

解决方案

You have 4 options to choose from.

include 'yourfile.php';
include_once 'yourfile.php';
require 'yourfile.php';
require_once 'yourfile.php';

of course you can also use " instead of '.

they all will do the same thing except minor differences.

if yourfile.php does not exist, the include ones will ignore that fact and your php page will move on - without any fatal errors.

require ones on the other hand will create fatal error.

if you know that that file is there, it makes no difference as to which one you pick.

As to the options with the _once postfix, well, they tend to be slower compared to their none _once postfixed counterparts. Cause when you use the include_once or the require_once, PHP will do some extra work to make sure that those files are truly included ONCE - protecting you from a possible double include situation if you carelessly code and many files use many includes and you may run into situations where the same file gets included twice. Well, _once option will prevent that. And that checking will come with some processing cost.

I also noticed you've used " as opposed to ' for your file delimiters. There is no reason to choose " over ' unless you will be referring to variable names in your files such as

$thefile = 'yourfile.php; include "$thefile";

so which ones to pick out of these 4? it all depends, if you think you do need to force the _once issue, then you pick either the include_once or require_once, and if you think that's not needed, then you go with the include or require. As to include vs require, it all comes down to would you like your PHP script die or move on if yourfile is for some reason not accessible.

If you are curious about some speed tests, here is a link for you to check out. http://php.net/manual/en/function.require-once.php

I also found this on the subject matter.

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: http://www.stevengould.org/portfolio/developerWorks/efficientPHP/wa-effphp/wa-effphp-a4.pdf

这篇关于包括外部的php文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆