including files

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
mozartmatt
Forum Newbie
Posts: 8
Joined: Wed May 02, 2007 2:39 pm

including files

Post by mozartmatt »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Having trouble including files:

i need to include a file at the start of each page.  and then in this included file there are some settings that include other files.  For example:

Code: Select all

<?php

// This is File 1 (Used in different directories for example: content/file1.php, content/my_account/file1.php)

include('file2.php');

// This is File 2 (used in one location: eg. settings/file2.php) 

include('misc_settings.php');

?>
if file 1 is in different directories i can just add ../ to work backwards. The problem is includes in file 2. I can't just add ../ to the includes in file 2 to correspond with File 1, coz file 2 needs can be accessed from different directory levels.

Can anyone understand me, let alone help me? lol

M


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
arturm
Forum Commoner
Posts: 86
Joined: Fri Apr 13, 2007 8:29 am
Location: NY
Contact:

Post by arturm »

It is a very good practice to use absolute path in your includes

Code: Select all

include ($_SERVER['DOCUMENT_ROOT'].'/content/file1.php');
mozartmatt
Forum Newbie
Posts: 8
Joined: Wed May 02, 2007 2:39 pm

Post by mozartmatt »

Hi arturm,

Thanks for the tip! With an hour or so playing about with it, i've managed to fix it!

Appreciate your help!!

M
Z3RO21
Forum Contributor
Posts: 130
Joined: Thu Aug 17, 2006 8:59 am

Post by Z3RO21 »

arturm wrote:It is a very good practice to use absolute path in your includes

Code: Select all

include ($_SERVER['DOCUMENT_ROOT'].'/content/file1.php');
You do not need the () for include. Just a heads up :)
User avatar
arturm
Forum Commoner
Posts: 86
Joined: Fri Apr 13, 2007 8:29 am
Location: NY
Contact:

Post by arturm »

parenthesis are optional but since include(), require() etc. are functions it looks better when you use them.
http://us.php.net/manual/en/function.include.php
But it is just matter of preference.
Z3RO21
Forum Contributor
Posts: 130
Joined: Thu Aug 17, 2006 8:59 am

Post by Z3RO21 »

arturm wrote:parenthesis are optional but since include(), require() etc. are functions it looks better when you use them.
http://us.php.net/manual/en/function.include.php
But it is just matter of preference.
True I try and avoid them to lessen file size (even though it hardly matters) good point though
User avatar
aaronhall
DevNet Resident
Posts: 1040
Joined: Tue Aug 13, 2002 5:10 pm
Location: Back in Phoenix, missing the microbrews
Contact:

Post by aaronhall »

arturm wrote:parenthesis are optional but since include(), require() etc. are functions it looks better when you use them.
http://us.php.net/manual/en/function.include.php
But it is just matter of preference.
include and require are both constructs
Post Reply