NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Webmaster Tutorials
Reload this Page Tutorial: Using PHP require() & include()

Webmaster Tutorials Instructional webmaster-related how-to's and tutorials.

Advanced Search


Closed Thread
 
LinkBack Thread Tools
Old 08-24-2003, 11:51 AM THREAD STARTER               #1 (permalink)
New Member
Join Date: Aug 2003
Posts: 3
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.
????: NamePros.com http://www.namepros.com/webmaster-tutorials/15167-tutorial-using-php-require-and-include.html

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
__________________
Dave

devnet - coding without the jargon
develement - no, that's not a typo
Dave S is offline  
Old 08-24-2003, 11:56 AM THREAD STARTER               #2 (permalink)
New Member
Join Date: Aug 2003
Posts: 3
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.
????: NamePros.com http://www.namepros.com/showthread.php?t=15167

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  
Old 08-24-2003, 02:27 PM   #3 (permalink)
NamePros Regular
Join Date: Jun 2003
Location: California
Posts: 245
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  
Old 08-24-2003, 02:41 PM THREAD STARTER               #4 (permalink)
New Member
Join Date: Aug 2003
Posts: 3
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  
Old 02-12-2004, 02:25 PM   #5 (permalink)
NamePros Regular
Join Date: Nov 2003
Location: houston
Posts: 234
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  
Old 03-22-2004, 01:40 AM   #6 (permalink)
NamePros Expert
 
Anthony's Avatar
Join Date: Dec 2003
Location: NYC
Posts: 9,134
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  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


Liquid Web Smart Servers  
All times are GMT -7. The time now is 09:56 AM.

Managed Web Hosting by Liquid Web
Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger