问题描述
我有一个配置文件,其中包含常规配置(在git repo中),以及一个覆盖配置属性的本地配置文件(在repo中忽略)。现在,本地配置文件包含在配置文件的开头:
I have a config file with the general configuration (in a git repo), and a local config file that overwrites configuration properties (ignored in the repo). Right now the local config file is included at the beginning of the config file:
include_once 'local_config.php';
但我希望include是有条件的:只有在文件local_config.php实际存在时才这样做。我可以毫不费力地 ,但首先我需要检查文件是否存在。所以我尝试了get_include_path()但它返回了一个路径列表,我必须解析这个列表并检查每一个。
But I would like the include to be conditional: only do it if the file local_config.php actually exists. I can do a enter link description here without problems, but first I would need to check if the file exists. So I tried get_include_path() but it returns a list of paths, and I would have to parse this list and check for every one.
另一种选择就是调用include_once()并禁止警告,但它甚至更加混乱。是否有更简单的方法在PHP中执行真正的可选包含?
Another option would be to just call include_once() and suppress the warnings, but it is even messier. Is there a simpler way to do a real optional include in PHP?
推荐答案
使用 file_exists()
预定义的PHP函数如下:
Use the file_exists()
predefined PHP function like so:
// Test if the file exists
if(file_exists('local_config.php')){
// Include the file
include('local_config.php');
}else{
// Otherwise include the global config
include('global_config.php');
}
此处的文档:
这篇关于可选包含在PHP中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!