Go Back   Wiki NewForum > Career Forum (Documents/Tutorials/Advice) > Tech Forum & Tutorial > PHP Forum & Tutorial

Sponsored Links

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 05-04-2009, 05:09 PM
welcomewiki welcomewiki is offline
Member
 
Join Date: Dec 2008
Location: India
Posts: 36,428
Default PHP Include File

Server Side Includes (SSI) are used to create functions, headers, footers, or elements that will be reused on multiple pages.



Server Side Includes

You can insert the content of a file into a PHP file before the server executes it, with the include() or require() function. The two functions are identical in every way, except how they handle errors. The include() function generates a warning (but the script will continue execution) while the require() function generates a fatal error (and the script execution will stop after the error).


These two functions are used to create functions, headers, footers, or elements that can be reused on multiple pages.


This can save the developer a considerable amount of time. This means that you can create a standard header or menu file that you want all your web pages to include. When the header needs to be updated, you can only update this one include file, or when you add a new page to your site, you can simply change the menu file (instead of updating the links on all web pages).


The include() Function

The include() function takes all the text in a specified file and copies it into the file that uses the include function.
Reply With Quote
  #2  
Old 05-04-2009, 05:09 PM
welcomewiki welcomewiki is offline
Member
 
Join Date: Dec 2008
Location: India
Posts: 36,428
Default




Example 1



Assume that you have a standard header file, called "header.php". To include the header file in a page, use the include() function, like this:

Welcome to my home page

Some text


Reply With Quote
  #3  
Old 05-04-2009, 05:10 PM
welcomewiki welcomewiki is offline
Member
 
Join Date: Dec 2008
Location: India
Posts: 36,428
Default

Example 2

Now, let's assume we have a standard menu file that should be used on all pages (include files usually have a ".php" extension). Look at the "menu.php" file below:





Home |
About Us |
Contact Us



The three files, "default.php", "about.php", and "contact.php" should all include the "menu.php" file. Here is the code in "default.php":




Welcome to my home page

Some text






If you look at the source code of the "default.php" in a browser, it will look something like this:






Home |
About Us |
Contact Us

Welcome to my home page


Some text







And, of course, we would have to do the same thing for "about.php" and "contact.php". By using include files, you simply have to update the text in the "menu.php" file if you decide to rename or change the order of the links or add another web page to the site.





The require() Function

The require() function is identical to include(), except that it handles errors differently.


The include() function generates a warning (but the script will continue execution) while the require() function generates a fatal error (and the script execution will stop after the error).


If you include a file with the include() function and an error occurs, you might get an error message like the one below.


PHP code:







include("wrongFile.php");
echo "Hello World!";
?>






Error message:




Warning: include(wrongFile.php) [function.include]:
failed to open stream:
No such file or directory in C:\home\website\test.php on line 5 Warning: include() [function.include]:
Failed opening 'wrongFile.php' for inclusion
(include_path='.;C:\php5\pear')
in C:\home\website\test.php on line 5 Hello World!



Notice that the echo statement is still executed! This is because a Warning does not stop the script execution.


Now, let's run the same example with the require() function.
PHP code:





require("wrongFile.php");
echo "Hello World!";
?>






Error message:




Warning: require(wrongFile.php) [function.require]:
failed to open stream:
No such file or directory in C:\home\website\test.php on line 5 Fatal error: require() [function.require]:
Failed opening required 'wrongFile.php'
(include_path='.;C:\php5\pear')
in C:\home\website\test.php on line 5



The echo statement was not executed because the script execution stopped after the fatal error.


It is recommended to use the require() function instead of include(), because scripts should not continue executing if files are missing or misnamed.
Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Study Abroad: File a winning application hrmanager Career Forum (Documents/Tutorials/Advice) 0 12-14-2008 01:40 PM


All times are GMT. The time now is 05:01 PM.


Powered by vBulletin® Version 3.8.10
Copyright ©2000 - 2017, vBulletin Solutions, Inc.