我有以下代码:
if (include_once(dirname(__FILE__).'/file.php')
|| include_once(dirname(__FILE__).'/local/file.php')
)
{
这会导致错误,因为 PHP 尝试包含 "1"
(大概是 dirname(__FILE__).'/file.php' || dirname(__FILE__).'/local/file .php'
)
注释掉第二行可以使其按预期工作,但它不会使用第二个文件。我真的需要在这里使用 elseif
和代码重复吗?还是有办法让它工作?
$ php --version
PHP 5.2.6-3ubuntu4.2 with Suhosin-Patch 0.9.6.2 (cli) (built: Aug 21 2009 19:14:44)
Copyright (c) 1997-2008 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2008 Zend Technologies
最佳答案
对 include
语句进行分组:
if ( (include_once dirname(__FILE__).'/file.php')
||
(include_once dirname(__FILE__).'/local/file.php')
)
参见example #4在手册页中:
<?php
// won't work, evaluated as include(('vars.php') == 'OK'), i.e. include('')
if (include('vars.php') == 'OK') {
echo 'OK';
}
// works
if ((include 'vars.php') == 'OK') {
echo 'OK';
}
?>
关于PHP 包含在多个包含时表现得很奇怪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1592258/