Online
 
Friday, 09 January 2009
 
 
More article:
Related Content:

PHP - Including Files
 

An important feature of PHP is its ability to include files. These files may contain additional PHP tags. When you are designing a web application, you can break out common components and place them in a single file. This step makes it much easier to change certain aspects in one place later, and have the change take effect across the entire application. To include a file, use the include keyword:


<?php
$title="My Cool Web Application";
include "header.inc";
?>

The header.inc file might look as follows:

<html><head>
<title><?php echo $title?></title>
</head>

This example illustrates two important concepts of included files in PHP. First, variables set in the including file are automatically available in the included file. Second, each included file starts out in HTML mode. In other words, if you want to include a file that has PHP code in it, you have to embed that code just as you would any other PHP code.

Note also that I used the .inc extension here. This is not a special file type, just an arbitrary extension name I chose. Since your Apache server is not set up to treat .inc files as PHP files, if you put this file somewhere under your document_root, people can browse to it and see the PHP source in that file directly. This is usually not a good idea, so I add these lines to my httpd.conf file:

<Files ~ "\.inc$">
Order allow,deny
Deny from all
</Files>

This blocks any direct access to .inc files.

The other option is to not put the files under document_root, or perhaps to name them .php instead. But be very careful with that last approach. Keep in mind that people will then be able to execute these scripts, when they were probably not designed to be executed in a standalone fashion.

Other ways to include files are through include_once, require, and require_once. The difference between include and require is simply that with include, if the file to be included does not exist, you get a warning, whereas with require you get a fatal error and script execution stops. The include_once and require_once variations ensure that the file being included has not been included already. This helps avoid things like function redefinition errors.

 

 

Tags: Add more tags...,
This entry was posted on . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a comment. Tags: Simple PHP, Pear, Easy PHP, PHP Tutorial, PHP MySQL, XSLT, Sap Tutorial, CSS Tutorial, XSL FO Java, SQL Tutorial.
Users' Comments (0)

Comment an article
  Name
  E-mail
   Title
Available characters: 4000
 Notify me of follow-up comments
This image contains a scrambled text, it is using a combination of colors, font size, background, angle in order to disallow computer to automate reading. You will have to reproduce it to post on my homepage
Enter what you see:

No comment posted

Natural 82
Statistic


Last Post

 
Top! Top!