IT.COM

Tutorial: Using PHP require() & include()

Spaceship Spaceship
Watch
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:
<?php
require("header.php");
?>

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

PHP:
<?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
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
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...

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
 
0
•••
VERY VERY useful information. As you can see I have merged them into one thread called PHP require() & include(). Thanks a lot :)
 
0
•••
No problem, good idea for merging them - should've thought of that meself.

Cheers,

Dave
:)
 
0
•••
Thanks for the good info!
 
0
•••
I never knew there was a difference between the two. Thanks for a great badic tutorial on the two functions! :)
 
0
•••
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back