Say I have files

mainprogram.php
searchtheweb.php
searchgoogle.php

then in searchtheweb.php I have

include_once ("searchgoogle.php");

And in mainprogram.php I have

include_once ("search/searchtheweb.php")

Sound reasonable right?

Now say I want to move both searchtheweb.php and searchgoogle.php to a new folder called search

now in mainprogram.php I will have

include_once ("searchtheweb.php")

Will I need to change

include_once ("searchgoogle.php");

mainprogram.php

    I think I understand lol:
    If the mainprogram.php is in the root or whatever you would need:

    include_once ("search/searchtheweb.php");

    Then if searchtheweb.php is in the same folder as searchgoogle.php you would just have:

    include_once ("searchgoogle.php"); 

      All relative paths will be relative to the main script, not relative to any file included by the main script even if subsequent includes are called by that included file. (When you include a file, it's as if its code is inserted directly into the including file at that point.)

      All this points to the advantages of setting up an include_path and organizing your includes so that they can be found by that path. Then you do not have to worry as much about what directory you are in. A secondary approach is to use absolute paths instead of relative paths, e.g.:

      include $_SERVER['DOCUMENT_ROOT'].'/path/to/file.php';
      
        NogDog wrote:

        All relative paths will be relative to the main script, not relative to any file included by the main script even if subsequent includes are called by that included file. (When you include a file, it's as if its code is inserted directly into the including file at that point.)

        Actually, that's not the case. I thought the exact same thing, but my tests confirmed otherwise:

        ./test.php

        <?php
        require_once 'testme/test1.php';

        ./testme/test1.php

        <?php
        require_once 'test2.php';

        ./testme/test2.php

        <?php
        echo "Hello world!";

        Output of php -f ./test.php

        Hello world!

        EDIT: Tested on the Windows build of PHP 5.3.2 (cli).

          That's interesting. The thing is I like to build a lot of module and may want to keep all those modules on their own directory. If Dexter and Brad is correct, then it will be possible to do so. But the documentation seems to suggest that Nogdog is correct.

          The experiment by Brad shows that Dexter is correct. So can we sort of update the documentation or explain to me why the documentation is correct?

            Yeah I would do it NogDog's way, I dont like includes inside includes inside includes

              Brad way seems better if a module will be used by many different programs.

                Hmm...it's weird. As Brad suggests, this works:

                www/test/test.php:

                </php
                include 'files/include.php';
                

                www/test/files/include.php:

                include 'hello.php';
                

                hello.php:

                <p>Hello, World.</p>
                

                Output:

                Hello, World.

                On the other hand, this generates a file-not-found error:

                www/test/test.php:

                </php
                include './files/include.php';
                

                www/test/files/include.php:

                include './hello.php';
                

                hello.php:

                <p>Hello, World.</p>
                

                Output:

                Warning: include(./hello.php) [function.include]: failed to open stream: No such file or directory in C:\wamp\www\test\files\functions.php on line 2

                Warning: include() [function.include]: Failed opening './hello.php' for inclusion (include_path='.;C:\wamp\bin\php\php5.3.0\PEAR') in C:\wamp\www\test\files\functions.php on line 2

                Can anyone explain why the difference?

                  Write a Reply...