问题描述
我正在使用一些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脚本死掉或继续前进,如果您的文件由于某种原因无法访问。
如果您对某些速度测试感到好奇,可以点击此链接查看。
我还在主题上发现了这一点。
$ b
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.
这篇关于包括外部的php文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!