开发者

PHP inclusion behaving oddly with multiple includes

开发者 https://www.devze.com 2022-12-09 12:13 出处:网络
I have the following code: if (include_once(dirname(__FILE__).\'/file.php\') include_once(dirname(__FILE__).\'/local/file.php\')

I have the following code:

if (include_once(dirname(__FILE__).'/file.php')
    || include_once(dirname(__FILE__).'/local/file.php')
    )
{

This causes an error because PHP tries to include "1" (presumably dirname(__FILE__).'/file.php' || dirname(__FILE__).'/local/file.php')

Commenting out the second line makes this work as intended, other than the fact it won't use the second file. Do I really need to use elseif and code duplication here, or is there a way to get this working?

$ 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


Group the include statements:

if ( (include_once dirname(__FILE__).'/file.php')
      ||
     (include_once dirname(__FILE__).'/local/file.php')
   )

See example #4 in the manual page:

<?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';
}
?>


Try using parentheses to subdue the operator precedence.

if ( (include_once('xml.php')) || (include_once('xml.php')) ) {
    echo 'bah';
}
0

精彩评论

暂无评论...
验证码 换一张
取 消