start.php
<?php
if(file_exists('/config.php')) require_once('/config.php');
echo TEST;
?>
config.php
<?php
define('TEST','Hamsters');
?>
我有 Windows XP + XAMPP 和 PHP 版本 5.3.8。
如果我运行 start.php,它会给我这个错误:
Notice: Use of undefined constant TEST - assumed 'TEST' in C:\programs\xampp\htdocs\start.php on line 3
现在我将 start.php 修改为以下内容,他给了我我的 Hamsters
:
<?php
require_once('/config.php');
echo TEST;
?>
file_exists()
说文件不存在但没有条件仍然能够 require_once()
声称不存在的文件?
最佳答案
做一个要求(或包含)条件是你根本不应该做的事情:
if (file_exists('/config.php')) {
require_once('/config.php');
}
相反,如果不是必须,则采用 include
,如果必须,则采用 require
。另见:
将 include/require 包装到条件中会使这些变得复杂,并且由于 include 通常与程序流程相关,因此您确实希望保持简单。
此外,您可能希望稍后使用的一些优化无法通过条件包含实现。
在你的情况下,我想知道你为什么要检查文件是否存在。而不是 require_once
你最肯定的意思是 include_once
,if 是多余的:
include_once('/config.php');
关于php - 文件不存在但可以 require_once() 它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18423282/