Look at also other topic groups, which I have done!
You can return to this topic group by using this menu and the link Table of topic groups in the top of the page.

Getting fragments


Functions, which get fragments

Fragments have been gathered together by using either readfile(), include() or require() functions. The file is normally inside parenthesis but concerning require quotation marks (' or ") are enough (for example <?php require(some.inc';?>). In addition it is noticeable that include(') should be enclosed with a curly brackets ({ + }) using statement block if it is inside a conditional block.

In addition of these function is needed for basic output the function print() or echo(), which either write a new code or print the brought code into another place. In the latter case the code has been defined as a variable. Below is an example of using a variable in order to print a string:

<?php $Variable="\"This is the text, which the should be printed by using a variable\"";
print $Variable;?>
prints the following text:
"This is the text, which the should be printed by using a variable"

The string could have ordinary text or for example HTML-code. If the string has single or double quotation marks it is necessary to put a backward slash (\) as the escape character before quotation marks. Otherwise the PHP-parser would interpret quotation marks either terminating the string or diving the string into pieces. If the string would have divided into pieces between sub-strings should be a dot (for example "string"."string"), when the string would be concatenated.

In some is necessary use so called arrays, which has several particles, for example:

$aLinks = array('Link1.inc','Link2.inc','Link3.inc');

Functions, which include fragments into main document should use so that the server would use as fast as possible but functions have also their special problems. Below is some basic information:

Server-related notes:

  1. All servers don't support the readfile() function.

  2. If the server founds an error, when the require() has been used and the required file can't be found, the server stops immediately downloading the page. If the include() would have used instead, the server could skip the error. It is the most safe to use the include() function.

  3. Servers have different error message printing. It might be impossible to debug all server errors online.

  4. With certain settings of the working mode and open_basedir relative references don't work. If pages are necessary to test offline it is necessary to think thoroughly reasonable naming conventions and put most included files to the same folder. Each data type could be searched by using file masks (for example *index.inc).

  5. Servers might also prevent using absolute paths (http://...), when it is impossible to share PHP-files with several folders.


What parts have been used as fragments

At least following fragments would be reasonable:

I had in the page DynamicMenus.php3 have once been the following structure (I have added comments):

/* The top part of the page: */
readfile('AdviceMeta.inc'); /* The generic beginning until the common HEAD-section. */
require ('DynamicMenus0Top.inc'); /* Page level HEAD-section and the start-tag of the BODY element.
/* The beginning of the BODY-section: */
readfile(topSection.inc'); /* Immediately after the start-tag of the BODY-element existing common part of the BODY-section. */
require('dynamicMenusButtons.inc'); /* Page level navigation buttons. */
readfile('AdviceOldInfo.inc'); /* Generic information for old browsers. */
/* Common navigation elements in the top part of the BODY-section: */
require (allPages4.inc');
requre ('AdviceNavigation.inc');
/* The content-related parts of the BODY-section: */
print "<div class=\"doc\" id=\"docSection\">"; /* The start-tag of the surround the actual content existing basic structural element. In some pages this is a part of the actual content of the page. */
require ('DynamicMenus0Content.inc'); /* The actual content of the page. */
/* Parts, which concern terminating the page: */
readfile(nextSection.inc'); /* Two navigation buttons (to the top of the page and to the next page). */
print "<br /></div>"; /* The end-tag of the structural element, which was written around the actual contents. */
readfile(bottomShort.inc'); /* The final termination of the page including termination tags for BODY and HTML elements. In some page the page I have advertisements and announcements before termination tags. */

Some fragments can think as as if modules. In the example above all browsers get the same modules. It is also possible to tailor modules for different browsers (I handle browser-detections further).


In page-level META tags a part of the content is common and part page-specific. For example before the page-specific description is the generic description of the sub-site:

<meta name="description" content="<?php readfile(metaContent.inc');?>..." />

I have made with PHP + JavaScript very effective way to control relative navigation links (up, prev and next) by setting to the HEAD-section:

<link rel="prev" href="javascript:prev();" />
<link rel="next" href="javascript:next();" />
<link rel="up" href="javascript:up();" />
<script language="JavaScript1.1" type="text/javascript">

function up(){
function prev(){
function next(){
</script><style type="text/css">

And into the BODY-section:

<td align="left" style="width:16px !important">
<a href="javascript:prev();" onmouseover="window.status='The\ previous\ page\ or\ anchor';return true;" onmouseout="window.status='';return true;" class="left" title="The previous revious page or anchor"><img
src="../Kuvat/buttons/Left.gif" border="0" alt="[Prev]" align=
"middle" style="height:16px; width:16px" /></a>
<td align="center" class="up">
<a href="javascript:up();" onmouseover="window.status='To\ the\ upper\ page\ level';return true;" onmouseout="window.status='';return true;" class="up" title="To the upper page level"><img
src="../Kuvat/buttons/TopSmallDouble.gif" border="0" alt="[Up]" align=
"middle" style="height:16px; width:16px" class="up" /></a></td>
<td align="right" style="width:16px !important">
<a href="javascript:next();" onmouseover="window.status='The\ next\ page\ or\ section';return true;" onmouseout="window.status='';return true;" title="the next page or section" class="right"><img
src="../Kuvat/buttons/Right.gif" border="0" alt="[Next]" align="middle" style="height:16px; width:16px" /></a>

<?php readfile('nextSection.inc'); ?>

In the nextSection.inc is the following HTML-code:

<div style="margin-top:1.0em"><div class="cNoPrint"><a href="#Top"><img
src="../Kuvat/buttons/Top.gif" border="0" alt="[Top]" align="middle" title="To the top of this page"/></a><br /><br /></div>
<div class="noPrint noBig" align="right"><a href="javascript:up()"><img
src="../Kuvat/buttons/TopSmallDouble.gif" border="0" alt="[Up]" align="middle" title="To the upper page level" /></a> <a href="javascript:next();"><img
src="../Kuvat/buttons/Right.gif" border="0" alt="[Next]" align="right" title="The next page or section" /></a></div><br /></div>

The disadvantage of using JavaScript is that the JavaScript-support must be enabled. The same functionality could be achieved by using only PHP for example at the following way (as an example going from the page Elementit0.php3 to Elementit.php3):

  1. Before the actual link set would be information about page-level variables (I would put them into the HEAD-section of the current document):
    $next = "Elementit.php3";
    $up = ...
  2. If the LINK element has been used, with PHP will generate attribute values from variables, for example rel="up" href="<?php print $up;?>"
  3. The link set in the bottom of the page will be included at the ordinary way by using <?php require ('...');?> or <?php include ('...');?>.
  4. The included file has link addresses, which are defined elsewhere by using variables. It could be like in two examples below:
    <?php print "...<a href=\"".$next."\"><img src=\"../Kuvat/buttons/Right.gif\" border=\"0\" alt=\"[Next]\" align=\"right\" title=\"The next section\" /></a>"; ?>
    ...<a href="<?php print $next;?>"><img src="../Kuvat/buttons/Right.gif" border="0" alt="[Next]" align="right" title="The next section" /></a>

Variables can be written also inside funtions and call assosiated functions, for example:

function nextSection(){
print $next; /* Variables don't work if they have defined outside the function if they have not explicit declared within the function as global variables (for example global $next, ...;). */

<a href="
<?php nextSection();?>"> ...

The easiest way is to predefine variable and if necessary variables have been in page level redefined. In all cases it is not necessary to redefine all variables in individual pages. It is however necessary to remember that variables takes the values, which are last defined before the associated functions. The following code doesn't work in any browser:

<link rel="up" href="<?php up();?> />
$up = "Content.php";
function up(){
global $up;
print $up;

In that example I have marked links as variables. I handle using variable more in the next section.