[main] [misc] [graphics] [page design] [site design] [xhtml] [css] [xml] [xsl] [schema] [javascript] [php] [mysql]

HVCC Home
Blackboard HVCC
Blackboard Manual
Faculty Association

php main
1. what is php
2. http basics
3. php basics
a. embedding php
* b. including php
c. php syntax
d. php data types
e. php variables
4. php expressions
5. php client side
6. php flow control
7. php manual


print version

Note that all external links will open up in a separate window.

This is a stripped down version of these pages for older browsers. These pages are really meant to be viewed in a standards compliant browser.

Directions for surfing with audio.

Including PHP

These tutorials are about PHP and its use for server-side Web programming.

Including PHP

As well as embedding PHP in documents, you can copy in code, PHP and otherwise, from other document sources.

PHP has two commands that allow you to do this:

  • include
  • require

Both do the same thing. The difference between them is that require causes the PHP parser to terminate the script if the document being referenced cannot be loaded, while include will not, and the PHP parser will try to process the document anyway.

The both have the same syntax:

<?php include somefile; ?>

<?php require somefile; ?>

The document being referenced is copied in at that location in the code of the main document. In essence, it replaces the include or require statement. This allows you to include document segments where you want them to appear.

The document being included is also treated as a discrete document. Any PHP in the document must be inside its own PHP tags, even if the entire included document is PHP. This makes sense if you think about it. It says, we don't know the nature of what we are including, so don't process its contents as PHP unless the contents specify to do so.

The document being referenced must be a local file, residing on the same server as the document attempting to include it. If you want to include remote files, then you must enable the allow_url_open option in your php.ini file. Remote file names must begin with an http: or ftp: protocol specification.

There is not requirement as to what you name your local file names that are referenced with your include and require statements. They can be any valid file name. The most common conventions are to use .html, .php, or .inc suffixes along with meaningful document names.

Newer code editors that are PHP aware will treat the .inc suffix as a PHP document, or can be told to do so. It is the recommended suffix for PHP code libraries. If your included content is pure HTML, then you should use an .html suffix.

If you include an external file multiple times in the same document, it will be copied into each location. If the external file contains functions that are defined within that file, this can cause an error as that it results in the multiple definition of the same function. PHP provides a way around this with the include_once and require_once. Documents included with these will only be loaded once, no matter how many times they are referenced. Note that they are only useful for code libraries. You cannot use them for identical pieces of content that you want displayed in multiple locations.

Scope of included content matches that of the location in which is was included.

[top]