Including PHP Files

Files that are included or required are interpreted as HTML code unless the PHP beginning and end tags are present at the beginning and end of the file.

  • include() - This statement is used to load and run a file at a set location only when the line the include statement is located on is executed.
  • include_once()
  • readfile()
  • require() - Replaces itself with the file with the name passed to the function. The file is read even if the line the require function is on is not ever executed.
  • require_once()
  • virtual()

Example

In most of my programs, I like to keep an include file that contains variables which may change. For example, when I interface to my SQL database, I create variables that I can used to refer by number to my database fields. This way, if I move a field in the database, or delete a field causing other field offset values to change, I will only need to change my file that is included in all other files.

Below is the include statement in the code.

<?php
include("phpvars.inc.php");
?>

My phpvars.inc.php file contains:

<?php
  $sitename="My Site";
  $siteurl="http://www.mysite.com";

  $urln=0;
  $sitetitlen=1;
  $descn=2;
  $approvn=3;
  $activen=4;
?>