Recently I discovered the following strange behavior of 'include' on PHP 5.2 (Windows). According to the documentation and to my understanding 'include' is supposed to simply 'copy' the contents of the file into the calling one. So if we put another 'include' into the included file, the path should be relative to the root file. If you do 'echo getcwd();' from any of the files, the path is indeed relative to the root one. But I discovered that if PHP can not find the file, it searches by making the working directory the included file. Here is the directory structure I set up to test the things:
test/index.php {contains: include 'func/main.inc.php';}
test/func/main.inc.php {contains: include '1.inc.php'; include 'func/2.inc.php'; include '../func/1.inc.php';
test/func/1.inc.php {contains: echo '<h1>'.__FILE__.' - '.getcwd().'</h1>';
test/func/2.inc.php {contains: echo '<h1>'.__FILE__.' - '.getcwd().'</h1>';
test/func/func/2.inc.php {contains: echo '<h1>'.__FILE__.' - '.getcwd().'</h1>';
The results are:
the first include in main.inc.php has no problem finding 1.inc.php
the second include in main.inc.php includes test/func/2.inc.php NOT test/func/func/2.inc.php which means PHP is first trying to locate the file based on index.php's working directory
the third include prints warnings as it will include './2.inc.php'
Is this behavior documented? Can we rely on it?
martin