PHP Introduction to PHP Include and require Tutorial, PHP include and require
Posted on: April 14, 2006 at 12:00 AM
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.

 

More Tutorials on roseindia.net for the topic PHP Introduction to PHP Include and require Tutorial, PHP include and require.
require() and include()
require() and include()   hii, What is the difference between require() and include() in php?   hello,ADS_TO_REPLACE_1 The require... Error. The include() statement includes and evaluates the specified file. include
 
php include and require
php include and require       In php include and require are almost same but the difference between these two... and require are different. Example (using include): <?phpADS_TO_REPLACE_2
 
PHP Introduction to PHP Include and require Tutorial, PHP include and require
PHP include and require In this tutorial we will learn how to use PHP include... with include (require): 1. File path is checked. 2. PHP goes into HTML.... In PHP there are two very similar functions to include files: include() 
 
differences between require and include, include_once.
differences between require and include, include_once.  What are the differences between require and include, include_once?   Hi friends, Differebne 1: The include_once() function includes and evaluates the specified
 
php include
php include  i'm trying to include a header on my page but it doesn't display anything here's my code
 
include php in html
include php in html  Actually, i have two different pages for my application.. 1) includes the HTML tags and designing. And second includes the PHP... into HTML page.   This is how you can include a PHP file into HTML <
 
What are the differences between require and include, include_once?
What are the differences between require and include, include_once?  What are the differences between require and include, include_once
 
php Include
php Include:       Include command provides the opportunity to include several php pages within a single page. This feature facilitates us to save our time. Include takes a file name
 
PHP Autoload
_autoload($classname){ require_once $classname.'.php'; }ADS...PHP Autoloading Classes: It is very common in PHP programming that we need to write long listing of files which has to be include in a single file
 
What?s the difference between include and require?
What?s the difference between include and require?  What?s the difference between include and require
 
PHP MYSQL Interview Questions
scripting language, What is the difference between require and include? And in which case we can use required function in PHP? What is include in php? How how...PHP MYSQL Interview Questions  What kind of questions can be asked
 
PHP Tutorials from RoseIndia
file.  PHP Require In php include and require are almost same... in PHP. Top Tutorials of PHPADS_TO_REPLACE_1 PHP Tutorial Starting PHP PHP... Starting PHP Learn the basics of PHP with this quick and simple tutorial
 
Sitemap PHP Tutorial
Questions | Site Map | Business Software Services India PHP Tutorial... Tutorial Section Why PHP Is So Useful | Evolution of PHP for Scripting Needs... Tutorial Section - IIADS_TO_REPLACE_2 PHP Beginners Guide | About PHP | PHP
 
Datepicker not getting called through include function of php
;/title> </head> <body> <?php include "datepicker.php" ?>...Datepicker not getting called through include function of php  my...; When I call this datepicker through include function as below
 
PHP Tutorial
PHP Tutorial  Hi, I am new in PHP programming. Can anyone provide me good urls to learn PHP? Thanks   Hi, Start learning PHP at PHP Tutorial section. Thanks
 
PHP Training
in Session Management in PHP (Introduction to Session Management, Session Control... Introduction to php .ini file Downloading and installing wamp on windows... date example Introduction to PHP Variables Different
 
What is PHP ?
What is PHP? PHP is an acronym stand for Hypertext PreProcessor. It is an open... in HTML. PHP is free, efficient, and that's why it is very popular in web development scenario. PHP coding is enclosed within <?php   ?> tag
 
PHP Basics, PHP Basics tutorials for beginners
and print language construct.   Introduction to PHP array: This tutorial...PHP Basics - Basics Tutorials for the beginners Introduction to PHP... tutorial we have covered the following topics: What is PHP: In this topic
 
PHP Tutorial
on roseindia.net PHP Tutorial (adsbygoogle = window.adsbygoogle || []).push...PHP Tutorial  Hi, I am looking for PHP tutorials on roseindia.net. Provide me the urls of PHP tutorials? Thanks (adsbygoogle
 
php
php   what is php   PHP Tutorials
 
php
php  what is php
 
PHP SQL Table
to create a table in PHP. Understand with ExampleADS_TO_REPLACE_1 The Tutorial... PHP SQL Table       PHP SQL Table is used to create table in PHP. To create a table in PHP
 
php
php using javasript  how to set timer in php using javasript
 
php
php  i want to know about php and php plateform required software to make php program and database that used in php program?   Please visit the following link: PHP database
 
php
php  i want to know about php and php plateform required software to make php program and database that used in php program?   Please visit the following link: PHP database
 
PHP MySQL on Centos 5.x
PHP MySQL on Centos 5.x This Tutorial show you how to install MySQL support... MySQL support into your php application. In this tutorial I will show you how... from PHP application. Hope this tutorial will help you in installing MySQL
 
PHP
and extend for their personal use.ADS_TO_REPLACE_1 PHP has evolved to include... PHP Services PHP refers to Hypertext Preprocessor, a general... scripting language. PHP generally runs on web server. It is competent
 
php
php  show the constructor overloading in php
 
PHP
PHP  How to work in drupal with PHP
 
Introduction to PHP Array
What is Array? An array in PHP is actually an ordered map, what is a map..._TO_REPLACE_5  In above example <?php ?> is the delimiter, which is mandatory for any PHP program. $first is the name
 
php problem
php problem  Hello!! I have a problem ,i want to display the data of a single row in my database,this is the code: <?php</p> include 'connectMySql.php'; $username=$row['username']; if(! $conn ) { die('Could
 
PHP Installation Process
PHP Installation Process  Hi, I want to install the PHP Software in My systems. So, Can any one Suggest how to installation the PHP software in My system and what configuration require to Install. So, Kindly refer any online
 
PHP
PHP   hii, Define PHP?   Hello,ADS_TO_REPLACE_1 The PHP Hypertext Preprocessor is a programming language that allows web developers to create dynamic content that interacts with databases. PHP is basically used
 
php
php  what is php   PHP is one of the most used open source server-side scripting language, which runs on almost all the platform. PHP... and Solaris. To know more about PHP, Visit Here
 
php
php  how to write a program in php to create pdf file
 
Array length in PHP - PHP
Array length in PHP  a function to get the array length in PHP.  Hi Friend, Please visit the following link: http://www.roseindia.net/tutorial/php/phparray/php-array-length-for.html Thanks
 
Replace String in PHP - PHP
Replace String in PHP  What is the fastest way of replacing string in PHP Code?  Hi Friend, Please visit the following link: http://www.roseindia.net/tutorial/php/phpbasics/PHP-Array-Replace.html Thanks
 
php
php  plz tell me code for PHP SQL Insert,delete,update,view is used to insert the record from HTML page to Mysql database using in single PHP form
 
php
php  retrieve data from mysql in php   Please visit the following links: http://www.roseindia.net/sql/mysql-table/mysql-php-select.shtml http://www.roseindia.net/sql/mysql-example/select-where.shtml
 
PHP - WebSevices
/www/htdocs//include/dbelement.php on line 49, PHP Notice: Use of undefined... - assumed 'Comment' in /srv/www/htdocs//include/dbelement.php on line 49, PHP Notice...PHP  Hello, I am new in PHP World! I have to solve this Error
 
php
php  How to catch a image and link click counters using Ajax php can any one help me
 
php
php  how to set email remainder in php?can you help me pls
 
PHP
PHP  I want to create Calendar Event in PHP. Could u help me
 
PHP
PHP  How to check whether a username exists on a website using php and cURL and not MySQL
 
Static Method in PHP - PHP
Static Method in PHP  So far I can easily use the normal functions as variable in PHP? but how can use static method?  Hi Friend, Please visit the following link: http://www.roseindia.net/tutorial/php/phpoop/php
 
Multidimensional Array in PHP - PHP
/tutorial/php/phparray/arraymultidimension.html Thanks... in PHP completely goes over my head. Can any one present a better explanation on it with an example? Also the structure of Multidimensional Array in PHP
 
Complete php tutorial
Learn PHP at PHP - Hypertext Preprocessor tutorial page. Thanks...Complete php tutorial  Hi, I am trying to find the url to learn complete PHP in easily. Can some one provide me url for complete php tutorials
 
connection in php
connection in php  <?php include("include/db.php"); ?> <html> <head> <title>Mann Jewellary</title> <meta http...;categories</div> <ul id="cate"> <?php
 
connection in php
connection in php  <?php include("include/db.php"); ?> <html> <head> <title>Mann Jewellary</title> <meta http...;categories</div> <ul id="cate"> <?php
 
connection in php
connection in php  <?php include("include/db.php"); ?> <html> <head> <title>Mann Jewellary</title> <meta http...;categories</div> <ul id="cate"> <?php
 
Ads

Related Tags for PHP Introduction to PHP Include and require Tutorial, PHP include and require:


Ads

 
Advertisement null

Ads