moose web power

Exploring and Using
Web Design Code

Include/Print - PHP Features

The "include" and "print" functions


A significant difference between .html or .htm documents and a .php document is that when a user calls for a .php document from a server, the server manipulates the document before sending it to the user's browser. For that reason one cannot create a .php document, and view it on your local computer.

While .php can bring interaction and flexibility to a webpage, a couple relatively simple codes are "include" and "print." The code in the main .php document "includes" code from an external document which in turn "prints" the code. If the "include" code is in several main documents and one modifies the code in the external document, it changes the code, appearance and functionality in all the main documents. This allows a single modification to affect many documents.

The basic code with this function is as follows. If php code is a part of a document, the document must be saved with a .php extension, eg main.php. In the main document use the following code:

<?php
  include 'somename.php';
?>


In an external document, one codes some standard HTML plus a couple lines of PHP codes before and after the HYML code and save it in the same folder as the main document with somename.php to match the name in the above code. The print function places the HTML code in the main document.

<?php
print '
  <table><tr><td>
  <img src="images/pic1.jpg" />
  </td></tr></table>

';
?>



A Strategy

Because PHP code does not display on a local computer, a strategy for generating code that will display is to create the code first in an .html document and the copy/paste it into an external document. After the code is created as desired,

  • select the code,
  • copy the selection,
  • create a PHP document,
  • type <?php print '
  • paste the code,
  • type the code ';?> and
  • *finally save that external document as somename.php
. Return to the main .html document,
  • remove the code that was selected and
  • replace it with the code <?php include 'somename.php';?> and
  • finally save that .html document as a .php document.
tag crowd