Matthew Scharley
Matthew Scharley

Reputation: 132274

PHP inclusion behaving oddly with multiple includes

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

Upvotes: 2

Views: 168

Answers (2)

Ionuț G. Stan
Ionuț G. Stan

Reputation: 179119

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

Upvotes: 4

meder omuraliev
meder omuraliev

Reputation: 186562

Try using parentheses to subdue the operator precedence.

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

Upvotes: 0

Related Questions