[advanced search]
Results from the most recent live auction are here.
18 members in the live chat room. Join Chat!
Register Rules & FAQ NP$ Store Active Threads Mark Forums Read
Go Back   NamePros.Com > Design and Development > Webmaster Tutorials
User Name
Password

Old 08-24-2003, 10:51 AM   · #1
Dave S
New Member
 
Trader Rating: (0)
Join Date: Aug 2003
Posts: 3
NP$: 7.00 (Donate)
Dave S is an unknown quantity at this point
PHP require() & include()

One of the most useful functions that PHP can perform are the require and include functions. These two functions are very similar in the action that they perform. They can call a page from anywhere in your directory (or another one) to a page. The most comment use for this is where headers and footers are involved.

Say, for instance, you have a 10 page web site and you need to edit the links of the page which happen to be text links and appear at the top of each page. With PHP instead of having to edit each page individually you can edit just one - which in the long run will save you a lot of time when editing your pages.

To achieve this, you need to split your page into three sections: a header, the main page and the footer.

Copy the header of the page into a new file and name this new file "header.php" and do the same for the footer, except name this file "footer.php". Then delete the "header" and "footer" from each of the other pages. Where the header of each file was enter this code:

PHP Code:
<?php
require("header.php");
?>


Do this for the footer of each page as well, except enter the code below:

PHP Code:
<?php
require("footer.php");
?>


If you now upload all the pages, including the "header.php" and "footer.php" files and now, whenever you need to edit the header or footer of each page of each file, just open up the "header.php" and "footer.php" files and edit them - simple!

More where that came from @ http://www.the-dev.net


Please register or log-in into NamePros to hide ads
__________________
Dave

devnet - coding without the jargon
develement - no, that's not a typo
Dave S is offline   Reply With Quote
Old 08-24-2003, 10:56 AM   · #2
Dave S
New Member
 
Trader Rating: (0)
Join Date: Aug 2003
Posts: 3
NP$: 7.00 (Donate)
Dave S is an unknown quantity at this point
FAQ: The difference between include() and require()

This is a question I get asked so many times, I'll clear it up here and now...

Quote:
require
The require() statement includes and evaluates the specific file.
require() includes and evaluates a specific file. Detailed information on how this inclusion works is described in the documentation for include().
require() and include() are identical in every way except how they handle failure. include() produces a Warning while require() results in a Fatal Error. In other words, don't hesitate to use require() if you want a missing file to halt processing of the page. include() does not behave this way, the script will continue regardless. Be sure to have an appropriate include_path setting as well.

include
The include() statement includes and evaluates the specified file.
The documentation below also applies to require(). The two constructs are identical in every way except how they handle failure. include() produces a Warning while require() results in a Fatal Error. In other words, use require() if you want a missing file to halt processing of the page. include() does not behave this way, the script will continue regardless. Be sure to have an appropriate include_path setting as well.
When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward.

- quote from PHP.net



In the probable event that the language above has not made anything about the two functions any clearer, the main difference is that the require() function will cause a fatal error to appear on your site where as include() will only give you a warning if it can't find the file.

More from where that came from @ http://www.the-dev.net
__________________
Dave

devnet - coding without the jargon
develement - no, that's not a typo
Dave S is offline   Reply With Quote
Old 08-24-2003, 01:27 PM   · #3
Alpha
NamePros Regular
 
Name: Todd
Location: California
Trader Rating: (0)
Join Date: Jun 2003
Posts: 249
NP$: 401.00 (Donate)
Alpha is an unknown quantity at this point
VERY VERY useful information. As you can see I have merged them into one thread called PHP require() & include(). Thanks a lot
__________________
--Alpha
Alpha is offline   Reply With Quote
Old 08-24-2003, 01:41 PM   · #4
Dave S
New Member
 
Trader Rating: (0)
Join Date: Aug 2003
Posts: 3
NP$: 7.00 (Donate)
Dave S is an unknown quantity at this point
No problem, good idea for merging them - should've thought of that meself.

Cheers,

Dave
__________________
Dave

devnet - coding without the jargon
develement - no, that's not a typo
Dave S is offline   Reply With Quote
Old 02-12-2004, 01:25 PM   · #5
rambo6376
NamePros Regular
 
Name: ahmed
Location: houston
Trader Rating: (0)
Join Date: Nov 2003
Posts: 235
NP$: 327.00 (Donate)
rambo6376 is an unknown quantity at this point
Thanks for the good info!
__________________
www.seriousdiscussion.com
Serious talk forum for serious people.
AOL is like the best written virus I've ever seen...
For Sale Cheap! buybook.biz codingheaven.com phpxpert.com cityOfBerlin.info sedp.us
rambo6376 is offline   Reply With Quote
Old 03-22-2004, 12:40 AM   · #6
Anthony
Permanent Resident
 
Anthony's Avatar
 
Name: Anthony
Location: NYC
Trader Rating: (142)
Join Date: Dec 2003
Posts: 9,203
NP$: 26.71 (Donate)
Anthony has a brilliant futureAnthony has a brilliant futureAnthony has a brilliant futureAnthony has a brilliant futureAnthony has a brilliant futureAnthony has a brilliant futureAnthony has a brilliant futureAnthony has a brilliant futureAnthony has a brilliant futureAnthony has a brilliant futureAnthony has a brilliant future
Member of the Month
October 2004
I never knew there was a difference between the two. Thanks for a great badic tutorial on the two functions!
Anthony is offline   Reply With Quote
Closed Thread

NamePros is a revenue sharing forum.

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


Site Sponsors
Get Your Site Linked at LinkedKeywords.com Thousand Dollar Profits http://www.mobisitetrader.com/
Advertise your business at NamePros
All times are GMT -7. The time now is 02:45 AM.


Powered by: vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 2.4.0