LT   EN   RU  
Wednesday 8 December 2021 Straipsniai.lt - Independent and informative portal
Home
Phorum
Contacts
Login
Register   Login
News subscribe
Subscribe   Unsubscribe
Partners
www.slaptai.lt www.gamezone.lt
www.penki.lt www.hakeriai.lt
   
   
Advertising
Statistic
Visits since 2002 09 12 - 68348403
Pages in Straipsniai.lt: 40735
  
  Computers > Computer technologies > Programing > PHP
Lankomumo reitingas Print version Print version
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.

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:

<html>
<body><?php include("header.php"); ?><h1>Welcome to my home page</h1><p>Some text</p></body>
</html>

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:

<html>
<body><a href="http://www.w3schools.com/default.php">Home</a> |
<a href="http://www.w3schools.com/about.php">About Us</a> |
<a href="http://www.w3schools.com/contact.php">Contact Us</a>

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

<?php include("menu.php"); ?><h1>Welcome to my home page</h1><p>Some text</p></body>
</html>

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

<html>
<body>
<a href="default.php">Home</a> |
<a href="about.php">About Us</a> |
<a href="contact.php">Contact Us</a>
<h1>Welcome to my home page</h1>
<p>Some text</p>
</body>
</html>

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(), they only handle 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:

<html>
<body>

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

</body>
</html>

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 5Warning: include() [function.include]:
Failed opening 'wrongFile.php' for inclusion
(include_path='.;C:\php5\pear')
in C:\home\website\test.php on line 5Hello 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:

<html>
<body>

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

</body>
</html>

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 5Fatal 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.

            
Lankomumo reitingas

Diskusijos - Discusions

Versija spausdinimui - Print version

Atgal
Random tags:    Art (10)    Feng Shui (14)    Krishnaism (10)    Sound systems (10)    Procesors (2)    Fishing (14)    Medicine (5)    Mysticism (119)    Intercourse (265)    Law (11)    Hunting (3)    Skydiving (10)    Health (20)    History (4)    Operating systems (19)    V.Nabokov (54)    Tourism (46)    Egypt (43)    Geology (4)    Martial arts (7)    Religion (34)    Ecology (10)    Software (11)    Aviation (10)    SSL certificates (10)    Internet (4)    Programing (13)    Eurointegration (4)    Security (22)    Travels (2)    Floriculture (10)    Agrobusiness (2)    Computer games (64)    Computers (355)    Gymnastics (9)    Pedagogics (10)    E - commerce (10)    Agriculture (17)    Linux/Unix (2)    Philately (8)    Cats (14)    Badminton (3)    PHP (3)    Vampire (6)    Literature (24)    Sport gymnastics (9)    S.Valiulis (2)    Automobiles (10)    Sport (81)    Fencing (2)
1. Simple PHP mail script
2. Multiple Page Forms
1. Simple PHP mail script
2. Multiple Page Forms
Map