PHP inclusion behaving oddly with multiple includes

includePHP

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

Best Answer

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