A rather useful thing to use in php is the 'include' function. This allows a file to be included in a web page when the browser renders that page. Php files can be included in php pages , as can .txt files, and many other formats.
A bit more flexible than the
asp version, multiple flavors of this idea exist: include(), require(), include_once() & require_once(). Essentially, the difference is that if the include() errors for any reason (missing file, unreadable file, etc.), the page will contain an error but continue to be processed by the server. Using a require() in the same situation will stop the page altogether. Some developers suggest using only include() statements for that reason, while others suggest include() for desired info & require() for something absolutely necessary to the page. I suggest the latter.
As the header states, the focus of this page is the include() statement. Everything is for php version 4x, with something additional for version 5x near the bottom. See the bottom of the page if you're not sure what you've got to work with.
Including the file:
Once the php file and the file to be include are created, place the following code in the php file (in the Code View if you're using Dreamweaver®). Modify the path & file name to point to the included file.
<?php include('path/included_file.extension'); ?>
Dreamweaver®'s internal preview will also show the included file in the page, provided it does not require a server to process it (a .txt file, for example). Included php files wil get a php icon.
Example 1:
This paragraph comes from a separate text file.
Pretty cool.
One of the possible uses: a client could update this file, without inadvertently destroying the actual page.
One downside: in the text file, each sentence is on it's own line. As you can tell, when it displays here that formatting goes away.
OK, so formatting in a text (.txt) file does not carry through into a web page. So, then it might make sense to use a Rich Text File (.rtf) since that file type carries formatting with it. The following paragraph comes from
such a file.
Example 2:
{\rtf1\mac\ansicpg10000\cocoartf824\cocoasubrtf330
{\fonttbl\f0\fswiss\fcharset77 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\margl1440\margr1440\vieww9000\viewh8400\viewkind0
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\ql\qnatural\pardirnatural
\f0\fs24 \cf0 This content comes from a separate text file.\
Pretty cool.\
One of the possible uses: a client could update this file, without inadvertently destroying the actual page.\
One downside: in the text file, each sentence is on it's own line. As you can tell, when it displays here that formatting goes away.}
Well, that didn't work so well. Once is while a client will ask about using a Word® file. As a last-ditch attempt, we could try that. Initially I included a .doc file below this paragraph. Turns out that it blew up this page so bad that it just wasn't worth showing.
Hiding Errors from the User:
One other point worth noting: when the browser goes to find the included file, and has a problem doing so, an error message will appear in place of that file on the web page. Below, an include calls a text file that does not exist. Do note that the include appears once in this page's code, while the amount of error codes may look otherwise. To avoid this from happening on your web page, place
<?php error_reporting(0); ?> above the Doctype (without the quotes). This will eliminate the error codes from showing on the page.
Warning: main(../../../php_testing/4include/missing_file.txt): failed to open stream: No such file or directory in
C:\Domains\gchelak.com\wwwroot\resources\tutorials\php_include\include.php on line
89
Warning: main(../../../php_testing/4include/missing_file.txt): failed to open stream: No such file or directory in
C:\Domains\gchelak.com\wwwroot\resources\tutorials\php_include\include.php on line
89
Warning: main(): Failed opening '../../../php_testing/4include/missing_file.txt' for inclusion (include_path='.;c:\php4\pear') in
C:\Domains\gchelak.com\wwwroot\resources\tutorials\php_include\include.php on line
89
php version 5x:
If you're working with php version 5x, php can be of further assistance. Checking to see if a file exists and is readable before running the include() is another option. This simply requires the willingness to hand code a little php. Try the following:
<?php $includeFile = 'filename.ext'';
if (file_exists($includeFile) && is_readable($includeFile)) {
include($includeFile);
}
?>
The first line defines the variable to mean the particular file. The second first checks to see if the file exists, then if it is readable. The third is the include() as outlined above. If both statements in the second line are not valid, the include() does not get executed. If the include() can only be executed under the correct circumstances, then it cannot fail and generate errors. Its a little more typing (make it a Snippet in Dreamweaver® so it becomes point-&-click) but the functionality is worth it.
Not sure which version of php is running on your host? Place the following code in a blank .php file & run it on your testing server:
<?php
phpinfo();
?>