PHP Introduction to PHP Include and require Tutorial, PHP include and require


 

PHP Introduction to PHP Include and require Tutorial, PHP include and require

These two similar functions are used template the pages. Which one to use? Find out the difference.

These two similar functions are used template the pages. Which one to use? Find out the difference.

PHP include and require

In this tutorial we will learn how to use PHP include and require functions.

There is a common practice to split the code into parts. Almost any big website uses this technique to template its contents.

When user loads the page from the server he sees the complete page but internally on the server each logical part of the page is stored in appropriate file. It is a useful method to select some repeating code chunk and store it in a separated file. When the time comes to make changes in the code you don't have to look through all the code but you go straight to the template file. And scripts on the server create resulting page from several template files.

In PHP there are two very similar functions to include files: include()  require().
Syntax:
  include $filename;
  require $filename;

where $filename: is the path of the file to include. The difference between these two functions is just in handling exception:
  include() will output warning message and the script continues
  require() will output error message and will halt the script
Most often error occurs when file does not exist in specified path.

If file to include has PHP script tags in the text (<?, ?>), then all PHP code is executed and result is included.
Now about how both functions work. When PHP interpreter meet the line with include (require):
1. File path is checked.
2. PHP goes into HTML text mode (just outputting all the code)
3. If PHP starting tags have been found, all functions are executed and only printed results are included as just text.
4. After the end-of-the file PHP interpreter switches to execution mode and continues executing main script after the include (require) line.

Example 1:
file res.html
<b>this is a text</b>


main code file test.php
<?
echo "<html>";
include 'res.html';
echo "</html>";
?>

Output:
<html><b>this is a text</b></html>


Example 2:
file to include vars.php
<?
$color = 'green';
$fruit = 'apple';
?>

main PHP code file test.php
<?
echo "A $color $fruit"; // outputs "A "
include 'vars.php';
echo "A $color $fruit"; // outputs "A green apple"
?>


The code in the file to be included inherits the variable context of the line where the include occurs. Any variables available at that line in the calling file are available in the called file code.

There is a big feature exists with these functions: you can include distant files and specify an URL as the file path.
Example:
include "http://www.yahoo.com/index.html"
Will process all the code received from target server.
You can even specify other protocols such is FTP for example.
Unfortunately this feature is unavailable for Windows type OS users.

 

Ads