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

 
 

PHP Introduction to PHP Include and require Tutorial, PHP include and require
Posted on: April 14, 2006 at 12:00 AM
Advertisement
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, The require() statement includes and evaluates the specific file.require() results in a Fatal Error. The include
 
php include and require
php include and require       In php include and require are almost same but the difference between these two.... In the following examples we will see how these two statements include and require
 
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
 
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
 
What?s the difference between include and require?
What?s the difference between include and require?  What?s the difference between include and require
 
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 <
 
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 Class Example: <?php function _autoload($classname){ require...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
 
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 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
; PHP Require In php include and require are almost same but the difference... in PHP. Top Tutorials of PHP PHP Tutorial Starting PHP PHP MySQL Zend... Learn the basics of PHP with this quick and simple tutorial. Designed
 
Sitemap PHP Tutorial
Questions | Site Map | Business Software Services India PHP Tutorial... by PHP  | PHP Ajax | PHP SimpleXML | PHP Ajax DB PHP Tutorial Section... Learn PHP | What a PHP Programmer Can Do PHP Beginner Tutorial Section - II
 
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 Tutorial
PHP Tutorial  Hi, I am looking for PHP tutorials on roseindia.net. Provide me the urls of PHP tutorials? Thanks   Hi, Here is the links on roseindia.net PHP Tutorial Complete PHP Tutorials Many Free PHP Tutorials
 
PHP Basics, PHP Basics tutorials for beginners
construct.   Introduction to PHP array: This tutorial gives you...PHP Basics - Basics Tutorials for the beginners Introduction to PHP... way to teach PHP scripting language. In our Online Free PHP tutorial we have
 
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
 
Introduction to PHP Array
What is Array? An array in PHP is actually an ordered map, what is a map...;value) An example of an array is given below: <?php $first=array('Name... true  In above example <?php ?> is the delimiter, which
 
PHP SQL Table
to create a table in PHP. Understand with Example The Tutorial covers an example... PHP SQL Table       PHP SQL Table is used to create table in PHP. To create a table in PHP
 
php
php   what is php   PHP Tutorials
 
php
php  what is php
 
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 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
php using javasript  how to set timer in php using javasript
 
PHP
and extend for their personal use. PHP has evolved to include a command line... PHP Services PHP refers to Hypertext Preprocessor, a general... scripting language. PHP generally runs on web server. It is competent
 
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
PHP  How to work in drupal with PHP
 
php
php  show the constructor overloading 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
 
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
 
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
 
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
 
PHP Training
, we will train in Session Management in PHP (Introduction to Session Management... What is PHP and how it works Introduction to php .ini file... PHP Functions Simple date example Introduction to PHP Variables
 
Best PHP tutorial for beginners
Best PHP tutorial for beginners  Hi Friends, I want to learn PHP... Tutorial index page. You find good tutorials for learning php from beginning. Installing PHP Installing PHP LAMP PHP MySQL Tutorial index page. Above tutorials
 
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
 
SQL PHP Where
the example we use PHP server side script that include host, user, password... is built, the php query include the Mysql query function and return the records... SQL PHP Where      
 
PHP Introduction
The quick introduction to the PHP programming language PHP is server... for their website. The PHP stands for PHP: Hypertext Preprocessor. The php program is simple text file with the .php or .php5  extensions. You can use
 
ppt php tutorial
ppt php tutorial  How can i work with ppt files in PHP
 
php
php  how to write a program in php to create pdf file
 
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   hii, Define PHP?   Hello, The PHP Hypertext Preprocessor is a programming language that allows web developers to create dynamic content that interacts with databases. PHP is basically used for developing web
 
PHP Introduction to PHP Echo and print Tutorial
Ok, this is a basic tutorial to help people just getting started with PHP. In this tutorial I will show you how to output to the webpage. We will use two... things first: all newcomers to PHP will be happy to know that PHP code can
 
PHP SQL close connection
'PHP SQL close Connection'. To understand and grasp the example we include... PHP SQL close connection       PHP SQL close connection is used to close a connection between the 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
 
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
 
The file function example in PHP, file php, file in php
In this php tutorial section we will be learning the file() function of PHP...() function in PHP array file ( filename, [,flags [, context]] ) It returns... values  as  FILE_BINARY(default),FILE_USE_INCLUDE_PATH,FILE_IGNORE_NEW_LINES
 
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
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
 
Advertisement

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


Advertisements Advertisements
 

 

 

DMCA.com