How to use the Include statement ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
l9pt5
Forum Newbie
Posts: 17
Joined: Wed Apr 11, 2007 10:41 am

How to use the Include statement ?

Post by l9pt5 »

Suppose I've three directories anh I have an include file called "inc.php", how would I use this file in my scripts that locate in other directories?

/dir1
/dir1/dir2
/dir1/include/inc.php

How would I use the above include file "inc.php" in "/dir1/dir2" script files?
How would I use the above include file "inc.php" in "/dir1" script files?

I tried different ways, but somehow it doesn't seem to be working. please post some tips. Thanks... :?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How to use the Include statement ?

Post by Christopher »

Code: Select all

include 'dir1/inc.php';
include 'dir1/dir2/inc.php';
(#10850)
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Re: How to use the Include statement ?

Post by stereofrog »

arborint wrote:

Code: Select all

include 'dir1/inc.php';
include 'dir1/dir2/inc.php';
This will use include_path, which may or may not be desired. To avoid confusions, it's safer to use absolute paths:

Code: Select all

include dirname(__FILE__) . '/dir1/inc.php';
l9pt5
Forum Newbie
Posts: 17
Joined: Wed Apr 11, 2007 10:41 am

Post by l9pt5 »

I think I didn't make my post clear, I only have one "inc.php" file in "/dir1/include" directory, but have many different scripts that are using it. These scripts that using the include file could be in any directory. Thanks...
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Then either set include_path or use absolute paths.
Post Reply