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
Michael_C
Forum Commoner
Posts: 51
Joined: Thu Feb 09, 2006 4:32 pm
Location: California

Include() statement

Post by Michael_C »

Another newbie question.
I've been looking at code in hopes to understand an existing website.
There are some buttons and text displayed on web pages, that appear to be generated from a file called template_1.html, but the only reference to template_1.html is in an include() in another file.

One thing that is puzzling could be answered if the include() not only "pulls" in the code/script from the file being included, but also executes it.

The PHP manual states "The include() statement includes and evaluates the specified file".

What does "evaluates" mean - "execute", or ...?

Normally I would have assumed the include() would work as it does in many languages - e.g., a directive to treat the contents of the "included" file as if it appeared in the php program.

If the code isn't executed in the include file then I'm back at square one trying to figure how some sections on a web page are generated/displayed.

Thanks in advance,

Michael
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Moved to PHP - Code. :roll:
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

The included file may or may not contain PHP so it evaluates whether the file has PHP executes those portions and skips the rest.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

think of including a file as in copy and pasting that particular file directly into the script.. any variables within that script can be carried over and such.
Michael_C
Forum Commoner
Posts: 51
Joined: Thu Feb 09, 2006 4:32 pm
Location: California

Post by Michael_C »

Jcart wrote:think of including a file as in copy and pasting that particular file directly into the script.. any variables within that script can be carried over and such.
Then the code in the included file is "available", and can be called, but just having the include() won't cause the code to be executed.

Here is the crux of what I'm trying to understand:

There is a file "http://TheWebSite/dreamaccount/Members.htm with the following link:
href="http://TheWebSite/dreamaccount/">CLICK HERE TO LOGON TO MEMBERS AREA</a></span></b></p>

When you clicks on the link a web page is displayed that looks like the content is from:
//TheWebSite/dreamaccount/Member_main.html and
//TheWebSite/dreamaccount/template_1.htm

I'm assuming when the user click on the link //TheWebSite/dreamaccount/index.php is run
the last line in index.php is:
include($path . '' . TEMPLATE_FILE);

I have no idea why Member_main.html, nor template files are executed.

I can't find a single "call" to Member_main.html in any file, and the only reference to the template file is via the include.

Any thoughts?

Michael
PS. I want to understand how/why things are working currently before I carve into things. I need to hook into the existing code in this general area - from there, I plan to do most, if not all, the work in Php.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

See if you can find where TEMPLATE_FILE is defined. It should be a line of code like:

Code: Select all

define('TEMPLATE_FILE', 'somefile.ext');
//where some file.ext is the file you suspect is included.
Michael_C
Forum Commoner
Posts: 51
Joined: Thu Feb 09, 2006 4:32 pm
Location: California

Post by Michael_C »

neophyte wrote:See if you can find where TEMPLATE_FILE is defined. It should be a line of code like:

Code: Select all

define('TEMPLATE_FILE', 'somefile.ext');
//where some file.ext is the file you suspect is included.
The define is:

Code: Select all

define("TEMPLATE_FILE", $dbs->f("setup_template_file"));
and the setup_template_file field in Setup table is set to template_1.html

and again, the last line in index.php is:

Code: Select all

include($path . '' . TEMPLATE_FILE)
;


What I'm trying to figure out:
how does the Template file Html get executed - the last line in index.php wouldn't do it (or would it?)
how does the member_main.html get called

And finally, is my assumption correct - will index.php, in dreamaccount folder, be executed if link href="http://TheWebSite/dreamaccount/ is clicked?

Michael
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Then the code in the included file is "available", and can be called, but just having the include() won't cause the code to be executed.
Any php code included will be executed in the order that it is included.

for instance..

page1.php

Code: Select all

echo 1;
page2.php

Code: Select all

echo 2;
page3.php

Code: Select all

echo 3;
index.php

Code: Select all

include('page1.php');
include('page2.php');
include('page3.php');
This would output

Code: Select all

123
And finally, is my assumption correct - will index.php, in dreamaccount folder, be executed if link href="http://TheWebSite/dreamaccount/ is clicked?
Correct.
User avatar
Fractal
Forum Commoner
Posts: 54
Joined: Tue Aug 16, 2005 1:28 pm

Post by Fractal »

Michael_C wrote:And finally, is my assumption correct - will index.php, in dreamaccount folder, be executed if link href="http://TheWebSite/dreamaccount/ is clicked?

Michael
Only if the server's default page is index, this could be changed to default, home, or whatever you want.

Usually it searches for [default_page].html or [default_page].htm first, if those don't exist it then searches for [default_page].ext, ext being php, asp, cf, or whatever language you are using.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Usually it searches for [default_page].html or [default_page].htm first, if those don't exist it then searches for [default_page].ext, ext being php, asp, cf, or whatever language you are using.
I'm pretty sure apache by default includes these, in this order

Code: Select all

DirectoryIndex index.php index.html index.htm
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

depends on installation...
Michael_C
Forum Commoner
Posts: 51
Joined: Thu Feb 09, 2006 4:32 pm
Location: California

Post by Michael_C »

Thank you for both the confirmation and clarification on include() - your example showed how the 3 php files would be executed when index.php was run - does this apply to html files as well? Would page2.html execute if index.php had the following:

Code: Select all

include('page1.php'); 
include('page2.html'); 
include('page3.php');
if so, that explains how the Template.html content is displayed. The only mystery, to me, is how member_main.htm is invoked.

Any suggestions on where to look for member_main.htm - I've done a search of all files for "member_main" with no success. Any other places to check in the php environment?

Thanks again,

Michael
Post Reply