Using these files:
  HTML/JavaScript files can be copied into an code editor such as AceHTML. If the file does not require access to another
code or image file, they can then be viewed from the editor. If they require access to other files, such as 1.5, which
requires an image file, or 2.2a or 2.2d, which require access to another HTML file in a specific directory, then all the
files must be saved in an appropriate directory. For example, 1.5 assumes that the image file is in the same directory
as the HTML file.
  PHP files must be run on a local or remote server. As discussed in the book, they will NOT run directly from an
code editor. So, you must save individual code files in the required directory, e.g., C:\wamp\www\ for an Apache server
installed locally on your Windows computer. In some cases, data files are included in this file. In other cases, 
you must create your own data files.
  Command line interface (CLI) files, as discussed in Chapter 11, must be saved in a directory that contains the 
required PHP files.
  Note that in a few cases, the code as printed in the book required line breaks that will cause syntax errors if
the code is copied exactly as it appears in the book. For example, quote-delimited text strings cannot be broken, but
some of those text strings are too long for one line in the book. In this file, there are no such broken lines, and
code copied from this file should not result in syntax errors caused by broken text strings, (When broken text
strings are found, they produce an error message something like "unterminated text string.")


Document 1.1 (HelloWorldHTML.htm)

<html>
<head>
<title>First HTML Document</title>
</head>
<body>
Hello, world!
</body>
</html>
-----------------------------
Document 1.2 (HelloWorld.htm)

<html>
<head>
 <title>Hello, world!</title>
 <script language="javascript" type="text/javascript">
 // These statements display text in a document.
   document.write("Hello, world!");
   document.write("<br />It's a beautiful day!");
 </script>
</head>
<body>
<!-- No content in the body of this document. -->
</body>
</html>   
------------------------------ 
Document 1.3 (HelloWorld2.htm)

<html>
<head>
<title>Hello, world!</title>
</head>
<body>
<h1 align="center">First JavaScript</h1>
<hr />
<script language="javascript" type="text/javascript">
  document.write("<font size='5' color='red'><center>Hello, world!</font>");
  document.write("<br /><font size='7' color='blue'>It's a beautiful day!</center></font>");
</script>
</body>
</html>
-------------------------------
Document 1.4a (HelloWorld3.htm)

<html>
<head>
<title>Hello, world! (v.3)</title>
</head>
<body bgcolor="lightgreen" text="magenta">
<h1 align="center">First JavaScript</h1>
<script language="javascript" type="text/javascript">
  document.write("<font color='green'>This document was last modified on "+document.lastModified+"</font>");
</script>
<hr />
<script language="javascript" type="text/javascript">
  document.write("background = "+document.bgColor);
  document.write("<br />font = " + document.fgColor);
  document.write("<font size='5' color='red'><center>Hello,world!</font><br />");
  document.write("<font size='7' color='blue'>He said, &quot;It's a beautiful day!&quot;</center></font>");
</script>
</body>
</html>
-----------------------------------
Document 1.4b (HelloWorld3HTML.htm)

<html>
<head>
<title>Hello, world! (with HTML)</title>
<script language="javascript" type="text/javascript">
  document.write("<font color='green'> This document was last modified on "+document.lastModified+"</font>");
</script>
</head>
<body bgcolor="lightgreen" text="magenta">
<h1 align="center">First JavaScript</h1>
<hr />
<script language="javascript" type="text/javascript">
  document.write("background = "+document.bgColor);
  document.write("<br />font = " + document.fgColor);
</script>
<font size="5" color="red"><center>Hello,world!</font><br />
<font size="7" color="blue"> 
He said, "It's a beautiful day!"</center></font>"
</body>
</html>
------------------------
Document 1.5 (house.htm)

<html>
<head>
<title>Our New House</title>
<script language="javascript" type="text/javascript">
document.write("<font color='green'>This document was last modified on "+document.lastModified+"</font>");
</script>
</head>
<body>
<h1>Our New House</h1>
<p>
Here's the status of our new house. (We know you're fascinated!)</p>
<!-- Link to your image goes here. -->
<img src="house.jpg" align="left" /><br />
</body>
</html>
------------------------------
Document 2.1 (tagExamples.htm)

<html>
<head>
<title>Tag Examples</title>
</head>
<body bgcolor="white">
<h1>Here is a Level 1 Heading</h1>
<h2>Here is a Level 2 Heading</h2>
<hr />
<pre>
	Here is some <strong><em>preformatted text</em></strong> that has
 	been created with the pre element. Note that it retains the 
paragraph tab
included
in the <b><i>original       document</b></i>. Also, it does not "collapse" line feeds
and
		white          spaces. Often, it is easier to use preformatted text than it
is to use markup to get the same effect. Note, however, that the default 
rendering of
preformatted text is to use a monospaced Courier font. This is often a good choice for
displaying code in an HTML document, but perhaps not a good choice for other kinds of text content.
</pre><p><center>
<img src="checkmark.gif" align="left" />Here, a small graphic (the check box) has been inserted into 
the document using the "img" element. This text is outside the preformatted 
region, so the default font is different. If you look at the original document, you can also see that
white         spaces and line   feeds are now collapsed.
</p><p>
Note too, that the text is now centered. The way the text is displayed will 
depend on how you
have the display window set in your browser. It may change when you go from full screen to a window, for example.
</center></p><p>
Centering is now turned off. The default text alignment is to the left of your screen.
You can change the size and color of text <font size="7" color="blue"> by using the &lt;font&gt;</font>
<font color="purple">element.</font>
</body>
</html>
-------------------------
Document 2.2a (index.htm)

<html>
<head>
<title>My Page</title>
</head>
<body>
<!-- These absolute links are a bad idea! -->
Here are links to 
<a href="C:/Documents and Settings/David/desktop/
JavaScript/Book/homework.htm">homework</a> and 
<a href="C:/Documents and Settings/
  David/desktop/JavaScript/Book/resume.htm">
personal documents.</a>
</body>
</html>
--------------------------
Document 2.2b (resume.htm)

<html>
<head>
<title>Resum&eacute;</title>
</head>
<body>
Here is my resum&eacute;.
</body>
</html>
----------------------------
Document 2.2c (homework.htm)

<html><head>
<title>Homework</title>
</head>
<body>
Here are my homework problems.
</body>
</html>
------------------------------------------------------
Document 2.2d (index2.htm, a new version of index.htm)

<html>
<head>
<title>My Page</title>
</head>
<body>
<!-- Use these relative links instead! -->
Here are links to 
<a href="homework/homework.htm">
homework</a> and 
<a href="personal/resume.htm">
personal documents.</a>
</body>
</html>
------------------------- 
Document 2.3 (style1.htm)

<html>
<head>
<title>Style Sheets</title>
<style title="David's default" type="text/css">
	body.bright {background: red; font: 16pt serif; 
         color: blue; font-style: italic; font-weight: bold}
</style>
</head>
<body class="bright">
Here is the body.
</body>
</html>
------------------------
Document 2.4a (body.css)

body {background:silver; color:white; font:24pt Times}
h1 {color:red; font:18pt Impact;}
h2 {color:blue; font:16pt Courier;}
--------------------------
Document 2.4b (style2.htm)

<html>
<head>
<title>Style Sheet Example</title>
<link href="body.css" rel="stylesheet"   
  type="text/css" />
 
(See Color Example 2 for full-color output.)
</head>
<body>

  <h1>Heading 1</h1>
  <h2>Heading 2</h2>
  Here is some text.
</body>
</html>
-----------------------
Document 2.5a (rwb.css)

p.red {background:red;color:blue;font:20pt Times}
div.white {background:white;color:red;font:20pt Times}
span.blue {background:blue;color:white;font:20pt Times}
-----------------------
Document 2.5b (rwb.htm)

<html>
<head>
<title>A Red, White, and Blue Document</title>
<link href="rwb.css" rel="stylesheet" type="text/css" />
</head>
<body>
<img src="stars.jpg" height="150" width="250" />
<p class="red">
This text should be blue on a red background.
</p><p><div class="white" style="font-style: italic;">
This text should be red on a white background.
</div></p>
<p><span class="blue">This text should be white on a blue background.</span>
</p>
</body>
</html>
-----------------------------
Document 3.1 (radonTable.htm)

<html>
<head>
<title>Radon Table</title>
</head>
<body>
<h1>Results of radon testing</h1>
<p>
The table below shows some radon levels measured in residences.<br /> For values greater than or equal to 4 pCi/L, action should be taken<br /> to reduce the concentration of radon gas. For values greater than or<br />
equal to 3 pCi/L, retesting is recommended.
</p>
<table>
  <tr bgcolor="silver">
    <td>Location</td><td>Value, pCi/L</td>
  <td>Comments</td></tr>
  <tr>
    <td>DB's house, basement</td><td>15.6</td>
    <td bgcolor="pink">Action should be taken!</td></tr>
  <tr>
    <td>ID's house, 2nd floor bedroom</td><td>3.7</td>
    <td bgcolor="yellow">Should be retested.</td></tr>
  <tr>
    <td> FJ's house, 1st floor living room</td><td> 0.9</td>
    <td bgcolor="lightgreen">No action required.</td></tr>
  <tr>
    <td> MB's house, 2nd floor bedroom</td><td>2.9</td>
    <td bgcolor="lightgreen">No action required.</td></tr>
</table>
</body>
</html>
------------------------
Document 3.2 (tbody.htm)

<html>
<head>
  <title>Using the tbody element</title>
  <style>
    th {background-color:black; color:white;}
    tbody.cold {text-align:center; 
     font-weight:bold; background-color:gray;}
    tbody.cool {text-align:center; 
     font-weight:bold; background-color:silver;}
    tbody.hot {text-align:center; 
     font-weight:bold; background-color:ivory;}
  </style>
</head>
<body>
<table border>
  <tr><th>Month</th><th>Average<br />Temperature
<br />&deg;F</td></tr>
<tbody  class="cold">
  <tr><td >January</td><td>30.4</td></tr>
  <tr><td>February</td><td>33.0</td></tr>
  <tr><td>March</td><td>42.4</td></tr>
</tbody>
<tbody class="cool">
  <tr><td>April</td><td>52.4</td></tr>
  <tr><td>May</td><td>62.9</td></tr>
</tbody>
<tbody class="hot">
  <tr><td>June</td><td>71.8</td></tr>
  <tr><td>July</td><td>76.7</td></tr>
  <tr><td>August</td><td>75.5</td></tr>
</tbody>
<tbody class="cool">
  <tr><td>September</td><td>68.2</td></tr>
  <tr><td>October</td><td>56.4</td></tr>
</tbody>
<tbody class="cold">
  <tr><td>November</td><td>46.4</td></tr>
  <tr><td>December</td><td>35.8</td></tr>
</body>
</html>
----------------------------
Document 3.3 (cloudType.htm)

<html>
<head>
<title>Cloud Type Chart</title>
</head>
<body>
<table border="2">
<caption>Cloud Type Chart</caption>
<tr>
  <th align="center">Altitude</th>
  <th colspan="2">Cloud Name</th></tr>
<tr><td align="center" rowspan="3">High</td>
    <td colspan="2">Cirrus</td></tr>
    <tr><td colspan="2">Cirrocumulus</td></tr>
    <tr><td colspan="2">Cirrostratus</td></tr></tr>
<tr><td align="center" rowspan="2">Middle</td>
    <td colspan="2">Altocumulus</td></tr>
    <tr><td colspan="2">Altostratus</td></tr></tr>
<tr><td align="center" rowspan="5">Low</td>
    <td>Cumulus</td>      
    <td>nonprecipitating</td></tr>
<tr><td>Altocumulus</td>    
    <td>nonprecipitating</td></tr> <tr><td>Stratocumulus</td>
    <td>nonprecipitating</td></tr>
    <tr><td>Cumulonimbus</td>
    <td align="center" 
       bgcolor="silver">precipitating</td></tr>
    <tr><td>Nimbostratus</td> <td align="center" 
       bgcolor="silver">precipitating</td></tr></tr>
</table>
</body></html>
---------------------------
Document 3.4 (location.htm)

<html>
<head>
<title>Data Reporting Site Information</title>
</head>
<body>
<form>
  Please enter your last name: 
  <input type="text" name="last_name" size="20"
    maxlength="20" /><br />
  Please enter your latitude:
  <input type="text" name="lat" value="40" size="7"
    maxlength="7" />
    N <input type="radio" name="NS" value="N" checked /> 
     or S <input type="radio" name="NS" value="S" /><br />
  Please enter your longitude:
  <input type="text" name="lon" value="75" size="8" 
    maxlength="8" />
    E <input type="radio" name="EW" value="E" /> or W
  <input type="radio" name="EW" value="W" checked /><br />
  Please enter your elevation:
  <input type="text" name="elevation" size="8" maxlength="8" 
    /> meters<br />
  Please indicate the seasons during which your site reports 
    data:<br />
  Winter: <input type="checkbox" name="seasons"
    value="Winter" />
  Spring: <input type="checkbox" name="seasons" 
    value="Spring" />
  Summer: <input type="checkbox" name="seasons" 
    value="Summer" />
  Fall: <input type="checkbox" name="seasons" 
    value="Fall" />
</form>
</body>
</html>
-------------------------
Document 3.5 (select.htm)

<html>
<head>
<title>Pull-Down List</title>
</head>
<body><form>
Select a month from this menu:
  <select name="testing">
    <option value="1" selected>January</option>
    <option value="2">February</option>
    <option value="3">March</option>
    <option value="4">April</option>
    <option value="5">May</option>
    <option value="6">June</option>
    <option value="7">July</option>
    <option value="8">August</option>
    <option value="9">September</option>
    <option value="10">October</option>
    <option value="11">November</option>
    <option value="12">December</option>
  </select>
</form></body>
</html>
---------------------------------
Document 3.6 (siteDefinition.htm)

<html>
<head>
<title>Observation Site Descriptions</title>
</head>
<body>
<form>
<table border="2" cellpadding="5" cellspacing="2" 
  align="center">
  <caption><font size="+2">Observation Site 
  Descritions</font></caption>
  <tr bgcolor="lightblue">
    <th>Site #</th><th>Site Name</th><th>Latitude</th>
    <th>Longitude</td><th>Elevation</th>
  </tr>
  <tr bgcolor="palegreen">
    <td>Site 1</td>
    <td><input type="text" name="Name1" size="10"      
      maxlength="10" value="Name1" /></td>
    <td><input type="text" name="Latitude1" size="10"  
      maxlength="10"
      value="Latitude1" /></td>
    <td><input type="text" name="Longitude1" size="10" 
      maxlength="10" value="Longitude1" /></td>
    <td><input type="text" name="Elevation1" size="10" 
      maxlength="10" value="Elevation1" /></td>
  </tr>
  <tr bgcolor="ivory">
    <td>Site 2</td>
    <td><input type="text" name="Name2" size="10"      
       maxlength="10" value="Name2" /></td>
    <td><input type="text" name="Latitude2" size="10"  
       maxlength="10" value="Latitude2" /></td>
    <td><input type="text" name="Longitude2" size="10" 
       maxlength="10" value="Longitude2" /></td>
    <td><input type="text" name="Elevation2" size="10" 
       maxlength="10" value="Elevation2" /></td>
  </tr>
  <tr bgcolor="palegreen">
    <td>Site 3</td>
    <td><input type="text" name="Name3" size="10"      
       maxlength="10" value="Name3" /></td>
    <td><input type="text" name="Latitude3" size="10"  
       maxlength="10" value="Latitude3" /></td>
    <td><input type="text" name="Longitude3" size="10" 
       maxlength="10" value="Longitude3" /></td>
    <td><input type="text" name="Elevation3" size="10" 
       maxlength="10" value="Elevation3" /></td>
  </tr>
  <tr bgcolor="ivory">
    <td>Site 4</td>
    <td><input type="text" name="Name4" size="10"      
       maxlength="10" value="Name4" /></td>
    <td><input type="text" name="Latitude4" size="10"  
       maxlength="10" value="Latitude4" /></td>
    <td><input type="text" name="Longitude4" size="10" 
       maxlength="10" value="Longitude4" /></td>
    <td><input type="text" name="Elevation4" size="10" 
       maxlength="10" value="Elevation4" /></td>
  </tr>
  <tr bgcolor="palegreen">
    <td>Site 5</td>
    <td><input type="text" name="Name5" size="10"      
       maxlength="10" value="Name5" /></td>
    <td><input type="text" name="Latitude5" size="10"  
       maxlength="10" value="Latitude5" /></td>
    <td><input type="text" name="Longitude5" size="10" 
       maxlength="10" value="Longitude5" /></td>
    <td><input type="text" name="Elevation5" size="10" 
       maxlength="10" value="Elevation5" /></td>
  </tr>
</table>
</form>
</body>
</html>
----------------------------
Document 3.7 (location2.htm)

<html>
<head>
<title>Location information</title>
</head>
<body bgcolor="ivory">
<form method="post" 
    action="mailto:my_mail@university.edu" 
    enctype="text/plain">
 	Please enter your last name: 
   <input type="text" name="last_name" size="20" 
      maxlength="20" /><br/>
	Please enter your latitude:
	<input type="text" name="lat" size="7" 
        maxlength="7" />
	 N <input type="radio" name="NS" value="N" /> 
  or S <input type="radio" name="NS" value="S" /><br/>
	Please enter your longitude:
	<input type="text" name="lon" size="8" 
        maxlength="8" />
	 E <input type="radio" name="EW" value="E"> 
  or W <input type="radio" name="EW" value="W" /><br/>
	Please enter your elevation:
	<input type="text" name="elevation" size="8" 
        maxlength="8" /> meters<br/>
	<input type="submit" 
        value="Click here to send your data." />
</form>
</body>
</html>
------------------------
Document 3.8 (lists.htm)

<html>
<head>
  <title>Using HTML Lists</title>
</head>
<body>
This page demonstrates the use of unordered, ordered, and definition lists.
<ul>
  <li> Use unordered lists for "bulleted" items.</li>
  <li> Use ordered lists for numbered items. </li>
<li> Use definition lists for lists of items to be defined. </li>
</ul>
Here are three ways to organize content in an HTML document:
<ol>
  <li>Use a table. </li>
  <li>Use a list. </li>
  <li>Use <font face="courier">&lt;pre&gt; ... &lt;/pre&gt;</font> tags.  </li>
</ol>
This is a way to produce a neatly formatted glossary list.
<dl>
  <dt><strong>definition list</strong> 
    (<font face="courier">&lt;dl&gt;</font>)</dt>
  <dd>Use this to display a list of glossary items and their definitions. </dd>
  <dt><strong>ordered list</strong>  
    (<font face="courier">&lt;ol&gt;</font>) </dt>
  <dd>Use this to display a numbered list. </dd>
  <dt><strong>unordered list</strong>   
    (<font face="courier">&lt;ul&gt;</font>)</dt>
  <dd>Use this to display a list of bulleted items. </dd>
</dl>
</body>
</html>
-------------------------------
Document 3.9 (bookContents.htm)

<html>
<title>Table of Contents for My Book</title>
<body>
<h2>Table of Contents for My Book</h2>
<ol>
<strong><li>Chapter One</strong></li>
  <ol type="I">
    <li>Section 1.1</li>
      <ol type="i">
	    <li>First Topic</li>
        <li>Second Topic</li>
          <ul type="circle">
            <li><em> subtopic 1</em></li>
            <li><em> subtopic 2</em></li>
          </ul>
      </ol>
    <li>Section 1.2</li>
    <li>Section 1.3</li>
  </ol>
<strong><li>Chapter Two</strong></li>
  <ol type="I">
    <li>Section 2.1</li>
  <ol type="i">
    <li>First Topic</li>
    <li>Second Topic</li>
      <ul type="circle">
        <li><em> subtopic 1</em></li>
        <li><em> subtopic 2</em></li>
      </ul>
  </ol>
    <li>Section 2.2</li>
    <li>Section 2.3</li>
  </ol>
<strong><li>Chapter Three</strong></li>
  <ol type="I">
    <li>Section 3.1</li>
      <ol type="i">
        <li>First Topic</li>
        <li>Second Topic</li>
          <ul type="circle">
            <li><em> subtopic 1</em></li>
            <li><em> subtopic 2</em></li>
            <li><em> subtopic 3</em></li>
         </ul>
      </ol>
    <li>Section 3.2</li>
    <li>Section 3.3</li>
      <ol type="i">
        <li>First Topic</li>
        <li>Second Topic</li>
      </ol>
    <li>Section 3.4</li>
  </ol>
</ol>
</body>
</html>
------------------------------
Document 3.10a (frameMain.htm)

<html>
<head>
<title>A simple frameset document</title>
</head>
<frameset cols="30%, 70%" frameborder="1">
  	<frameset rows="60%, 40%">
      		<frame src="frame1.htm" scrolling="no" />
      		<frame src="frame2.htm" />
  	</frameset>
  	<frame name="homeFrame" src="homeFrame.htm" />
</frameset>
</html>
------------------------------
Document 3.10b (homeFrame.htm)

<html>
<head>
<title>My Home Frame</title>
</head>
<body bgcolor="lightgreen">
<h1><blink><font color="maroon"><b><i>Home page display goes here.</i></b></font></blink></h1>
</body>
</html>
---------------------------
Document 3.10c (frame1.htm)

<html>
<head>
<title>Title Frame</title>
</head>
<body bgcolor="pink">
<font size="+6" color="navy"><center><b><i>Frames
<br />Demo<br />
<a href="frameDescription.htm" /><img src="frame.gif" 
  border="2"></i></b></center></a>
</font>
</body>
</html>
---------------------------
Document 3.10d (frame2.htm)

<html>
<head>
<title>Gossip Column</title>
</head>
<body bgcolor="lightblue">	  
<font size="+3">
Links to other stuff...<br />
<a href="gossip.htm" target="homeFrame" />Gossip Column</a>
<br />
<a href="photoGallery.htm" target="homeFrame" />
  Picture Gallery</a><br />
<a href="homeFrame.htm" target="homeFrame" />home</a><br />
</font>
</body>
</html>
-------------------------------------
Document 3.10e (frameDescription.htm) 

<html>
<head>
<title>How this image was created.</title>
</head>
<body>
This image was created in Windows' Paint program.
<a href="frame1.htm" />Click here to return.</a>
</body>
</html>
--------------------------
Document 3.11 (cloud1.htm)

<html>
<head>
<title>Cloud Observations</title>
</head>
<body bgcolor="#aaddff">
<h1>Cloud Observations</h1>
<strong> Cloud Observations </strong>(Select as many cloud types as observed.)
<br />
<form>
<table>
  <tr> 
   <td><strong>High</strong> </td>
    <td> 
     <input type="checkbox" name="high" 
       value="Cirrus" /> Cirrus</td>
    <td> 
     <input type="checkbox" name="high"
       value="Cirrocumulus" /> Cirrocumulus </td>
    <td> 
      <input type="checkbox" name="high" 
       value="Cirrostratus" /> Cirrostratus </td></tr>
  <tr> 
    <td colspan="4"><hr noshade color="black" />
     </td></tr>
  <tr> 
    <td> <strong>Middle</strong> </td>
    <td> 
      <input type="checkbox" name="mid" 
        value="Altostratus" /> Altostratus </td>
    <td> 
      <input type="checkbox" name="mid" 
        value="Altocumulus" /> Altocumulus</td></tr>
  <tr> 
    <td colspan="4"><hr noshade color="black" />
     </td></tr>
  <tr> 
    <td> <strong>Low</strong></td>
    <td> 
    <input type="checkbox" name="low" value="Stratus" />   
      Stratus</td>
    <td> 
      <input type="checkbox" name="low"
        value="Stratocumulus" /> Stratocumulus</td>
    <td> 
    <input type="checkbox" name="low" value="Cumulus" />
      Cumulus </td></tr>
  <tr> 
    <td colspan="4"><hr noshade color="black" />
      </td></tr>
  <tr> 
    <td> <strong>Rain-Producing </strong> </td>
    <td> 
      <input type="checkbox" name="rain" 
        value="Nimbostratus" /> Nimbostratus</td>
    <td> 
      <input type="checkbox" name="rain" 
        value="Cumulonimbus" /> Cumulonimbus </td></tr>
</table>
</form>
</body>
</html>
--------------------------------------
(partial data file for Document 3.12a)
DRB Worcester PA									
40.178 -75.3325								
4030 5200								
Mon day  yr   hr min sec EST         PYR-1   PYR-2   T
7   1    2008 0  0   0   1           0.00031 0.00031 20.198
7   1    2008 0  1   0   1.000694444 0.00031 0.00031 20.174
7   1    2008 0  2   0   1.001388889 0.00031 0.00031 20.174

------------------------------------
Document 3.12a (pyranometerMain.htm)

<html>
<head>
<title>Display pyranometer data</title>
</head>
<frameset rows="10%, *">
    	<frame src="header.htm" scrolling="no" />
  	<frame src="pyranometer.dat" />
</frameset>
</html>
---------------------------
Document 3.12b (header.htm)

<html>
<head>
  <title></title>
</head>
<body>
<font face="courier" >
This is the header.<br />  
mon	&nbsp; &nbsp;day &nbsp; &nbsp; yr &nbsp; &nbsp;hr &nbsp; &nbsp; min &nbsp; &nbsp;sec &nbsp; &nbsp; EST &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PYR-1 &nbsp;PYR-2 &nbsp;T<br />	
</font>
</body>
</html>
-------------------------
Document 4.1 (circle.htm)

<html>
<head>
<title>Calculate area of a circle.</title>
<script>
var radius=prompt("Give the radius of a circle: ");
radius=parseFloat(radius);
var area=Math.PI*radius*radius;
alert("The area of the circle with radius="+radius+" is "+area+".");
</script>
</head>
<body>
</body>
</html>
-------------------------------------
Document 4.2 (incrementDecrement.htm)

<html>
<head>
<title>Increment/decrement operators</title>
<script>
	var x=3,y;
	y=(x++)+3;
	document.write("post-increment: y="+y+"<br />");
	document.write("x="+x+"<br />");
	x=3;
	y=(++x)+3;
	document.write("pre-increment: y="+y+"<br />");
	document.write("x="+x+"<br />");	
</script>
</head>
<body>
</body>
</html>
---------------------------------
Document 4.3 (mathFunctions2.htm)

<html>
<head>
 <title>Demonstration of the Math object.</title>
<script language="javascript" type="text/javascript">
  for (var i=1; i<=10; i++)
   with (Math) {
    var x=floor(100*(random()%1))+1;
    document.write(x+" "+sqrt(x)+" "+pow(x,3)+"<br />");
   }
</script>
</head>
<body>
</body>
</html>
-------------------------
Document 4.4 (grades.htm)

<html>
<head>
<title>Get letter grade</title>
<script language="javascript" type="text/javascript">
    var grade=
      parseFloat(prompt("What is your numerical grade?"));
  document.write("For a numerical grade of "+grade+
                 ", your letter grade is ");
  if (grade >= 90) document.write("A");
  else if (grade >= 80) document.write("B");
  else if (grade >= 70) document.write("C");
  else if (grade >= 60) document.write("D");
  else document.write("F");
  document.write(".");
</script>
</head>
<body>
</body>
</html>
------------------------
Document 4.5 (taxes.htm)

<html>
<head>
<title>Calculate income tax</title>
<script language="javascript" type="text/javascript">
var income=
prompt("Enter your income (no commas!): $");
income=parseFloat(income);
var tax,loRate=.17,hiRate=.37;
if (income<=50000.)
  tax=income*loRate;
else
  tax=50000.*loRate+(income-50000.)*hiRate;
document.write("For an income of $"+income+", your tax is $"+tax.toFixed(2)+".");
</script>


 
</head>
</body>
</html>
------------------------------
Document 4.6 (daysInMonth.htm)

<html>
<head>
<title>Days in Month</title>
<script language="javascript" type="text/javascript">
var month=prompt("Give month (1-12): ");
switch (month) {
  case "1":
  case "3":
  case "5":
  case "7":
  case "8":
  case "10":
  case "12": 
    alert("There are 31 days in this month."); break;
  case "4":
  case "6":
  case "9":
  case "11": 
    alert("There are 30 days in this month."); break;
  case "2": 
    alert("There are either 28 or 29 days in this month."); break;
  default: 
    alert("I do not understand your month entry.");
}
</script>
</head>
<body>
</body>
</html>
---------------------------
Document 4.7 (counter2.htm)

<html>
<head>
<title>Counter</title>
<script>
var k;
document.write("Here's a simple counter: "+"<br />");
for (k=0; k<=10; k++)
 	document.write(k+"<br />");
</script>
</head>
<body>
</body>
</html>
-----------------------------
Document 4.8 (countdown2.htm)

<html>
<head>
 <title>Countdown</title>
<script>
var k;
document.write("Start launch sequence!"  
  +"<br />");
for (k=10; k>=0; k--)
  document.write(k+"<br />");
document.write("FIRE!!");
</script>
</head>
<body>
</body>
</html>
---------------------------
Document 4.9 (gorilla1.htm)

<html>
<head>
<title>The elevator problem (with gorillas).</title>
<script language="javascript" type="text/javascript">
  var totalWeight=0.,limitWeight=500.,maxWeight=550.;
  var newWeight;
  do {
    newWeight=Math.floor(maxWeight*(Math.random()%1))+1;
    if ((totalWeight + newWeight) <= limitWeight) {
      totalWeight += newWeight;
      document.write(
         "New weight = " + newWeight + " total weight = " 
         +totalWeight + "<br />");
      newWeight=0.;
    }
    else document.write("You weigh " + newWeight + " lb. I'm sorry, but you can't get on.");
  } while ((totalWeight + newWeight) 
       <= limitWeight);
</script>
</head>
<body>
</body>
</html>
-------------------------------
Document 4.10 (newtonSqrt2.htm)

<html>
<head>
<title>Newton's square root algorithm</title>
<script language="javascript" type="text/javascript">
var n=prompt("Enter a positive number:");
n=parseFloat(n);
var g=n/2;
do {
  g = (g + n/g)/2.;
} while (Math.abs(g*g-n) > 1e-5);
alert(g+" is the square root of "+n+".");
</script>
</head>
<body>
</body>
</html>
-----------------------------------
Document 4.11 (stationPressure.htm)

<html>
<head>
<title>Convert sea level pressure to station 
pressure.</title>
<font size="+1">
<b>Convert sea level pressure to station pressure (true pressure)</b></font><br /><br />
</head>
<body bgcolor="lightblue">
This application converts sea level pressure to 
station pressure.<br />
Station pressure is the actual pressure at an 
observer's observing site.<br />
It is always less than or equal to sea level pressure (unless you are below<br />
sea level). 
<br />
<form>
Fill in elevation and sea-level pressure: 
<input type="text" name="elevation" value="0" size="8" maxlength="7" /> (m)
<input type="text" name="sea_level_pressure" value="1013.25" size="8" maxlength="7" /> (mbar) <br />
<input type="button" name="Calculate" 
  value="Click here to get station pressure:" 
  onclick="result.value=
    parseFloat(sea_level_pressure.value)-         parseFloat(elevation.value)/9.2;" />
<input type="text" name="result" size="8"
  maxlength="7" /> (mbar)<br /> 
<input type="reset" value="Reset all fields." />
</form>
</body>
</html>
-----------------------------
Document 4.12 (quadratic.htm)

<html>
<head>
<title>Solving the Quadratic Equation</title>
</head><body><form>
Enter coefficients for ax<sup>2</sup> + bx + c = 0:
<br />
a = <input type="text" value="1" name="a" />
 (must not be 0)<br />
b = <input type="text" value="2" name="b" /><br />
c = <input type="text" value="-8" name="c" /><br />
click for r<sub>1</sub> = <input type="text" value="0" name="r1" 
onclick="var A=parseFloat(a.value),B=parseFloat(b.value),
C=parseFloat(c.value);
r1.value=(-B+Math.sqrt(B*B-4.*A*C))/2./A;" /><br />
click for r<sub>2</sub> = <input type="text" 
value="0" name="r2" onclick="var A=parseFloat(a.value),B=parseFloat(b.value),
C=parseFloat(c.value);
r2.value=(-B-Math.sqrt(B*B-4.*A*C))/2./A;" /><br />
</form></body></html>
-----------------------------------
Document 4.13 (RectangularRule.htm)

<html>
<head>
<title></title>
</head>
<body>
<h2>Rectangular Rule integration</h2>
for f(x)=x<sup>2</sup>
<form>
  xo: <input type="text" name="x0" value="1" /><br />
  x1: <input type="text" name="x1" value="3" /><br />
  <input type="button" value="Click here to integrate." 
    onclick="var x,X0,X1,i,n=20,integral=0,y; //y=x*x
	X1=parseFloat(x1.value);
	X0=parseFloat(x0.value);
	dx=(X1-X0)/n;
	for(i=0; i<n; i++) {
	  x=X0 + i*dx + dx/2;
	  y=x*x;
	  integral+=y;
	}
	result.value=integral*dx; "/>
  <input type="text" name="result" value="result" /></br />
</form>
</body>
</html>
---------------------------
Document 5.1 (siteData.htm)

<html>
<head>
<title>Site Names</title>
<script>
  var siteID = ["Drexel",3,"home",101];
  var i;
  for (i=0; i<siteID.length; i++)
   document.write(i+", "+siteID[i]+"<br />");
</script>
</head>
<body>
</body>
</html>
----------------------------------
Document 5.2 (stacksAndQueues.htm)

<html>
<head>
<title>Stacks and Queues</title>
<script language="javascript" type="text/javascript">
  var a=[1,3,5,7], i;
// Treat the array like a stack.
  document.write("STACK:" + a + " length of a = " + a.length+"<br />");
  a.push(11,12,13);
  document.write(a + " length of a = " + a.length
  +"<br />");
  for (i=1; i<=3; i++) {
    a.pop();
    document.write(a + " length of a = " + 
    a.length+"<br />");
  }
// Treat the array like a queue.
document.write("QUEUE:" + a + "   length of a = " + a.length+"<br />");
  a.push(11,12,13);
  document.write(a + " length of a = " + a.length
  +"<br />");
  for (i=1; i<=3; i++) {
  a.shift();
  document.write(a + " length of a = " + a.length
  +"<br />"); 
  }
</script>
</head> 
<body></body>
</html>
-----------------------
Document 5.3 (sort.htm)

<html>
<head>
<title>Sorting Arrays</title>
<script language="javascript" type="text/javascript">
var a=[7,5,13,3];
document.write(a + "   length of a = " + a.length+"<br />");
a.sort();
document.write(a + "   length of a = " + a.length+"<br />");
</script>
</head> 
 <body>
</body>
</html>
---------------------------- 
Document 5.4 (TwoDArray.htm)

<html>
<head>
<title>Two-D arrays</title>
<script language="javascript" type="text/javascript">
var siteID = new Array();
siteID[0]=new Array("Drexel",39.955,-75.188,10);
siteID[1]=new Array("home",40.178,-75.333,140);
siteID[2]=new Array("NC",35.452,-81.022,246);
siteID[3]=new Array("Argentina",-34.617,-58.37,30);
siteID[4]=new Array("Netherlands",52.382,4.933,-1);
var r,c,n_rows=siteID.length,n_cols=siteID[0].length;
for (r=0; r<n_rows; r++) {
  document.write(siteID[r][0]);
  for (c=1; c<n_cols; c++) {
	document.write(", "+siteID[r][c]);
  }	
  document.write("<br />");
}
</script>
</head>
<body>
</body>
</html>
------------------------------
Document 5.5 (TwoDArray_2.htm)

<html>
<head>
<html>
<head>
<title>"Multidimensional" arrays</title>
<script language="javascript" type="text/javascript">
var siteID = new Array();
function IDArray(ID,lat,lon,elev) {
  this.ID=ID;
  this.lat=lat;
  this.lon=lon;
  this.elev=elev;
}
siteID[0]=new IDArray("Drexel",39.955,-75.188,10.);
siteID[1]=new IDArray("home",40.178,-75.333,140.);
siteID[2]=new IDArray("NC",35.452,-81.022,246);
siteID[3]=new IDArray("Argentina",-34.617,-58.37,30.);
siteID[4]=new IDArray("Netherlands",52.382,4.933,-1);

var i;
for (i=0; i<siteID.length; i++) {
	document.write(siteID[i].ID+
	", "+siteID[i].lat+", "+siteID[i].lon+", "+siteID[i].elev+"<br />");
}
</script>
</head>
<body>
</body>
</html>
----------------------------
Document 5.6 (formArray.htm)

<html>
<head>
<title>Using the elements[] array to access values in forms.</title>
</head>
<body>
<form name="myform">
  A[0]<input type="text" value="3" /><br />
  A[1]<input type="text" value="2" /><br />
</form>
<script language="javascript" type="text/javascript">
for(var i=0; i<document.myform.elements.length; i++) {
  document.write("A["+i+"] = "+document.myform.elements[i].value+"<br />");
}
</script>
</body>
</html>
--------------------------
Document 5.7 (sumForm.htm)

<html>
<head>
<title>Sum a column of values</title>
</head>
<body>
<form name="sumform">
  <input type="text" value="3.3" /><br />
  <input type="text" value="3.9" /><br />
  <input type="text" value="7.1" /><br />
Here is the sum of all the values.<br />
  <input type="text" name="sum" value="0" 
    /><br />
</form>
<script language="javascript" type="text/javascript">
	var sum=0;
	for (var i=0; i<(sumform.elements.length-1); i++) sum+=parseFloat(sumform.
elements[i].value);
sumform.elements[sumform.
elements.length-1].value=sum;
</script>
</body>
</html>
---------------------------
Document 5.8 (sumForm2.htm)

<html>
<head>
<title>Sum a column of values</title>
</head>
<body>
<form name="sumform">
<table border>
  <tr><td><input type="text" value="Value 1" /></td>
  <td><input type="text" value="3.3" /></td></tr>
  <tr><td><input type="text" value="Value 2" /></td>
  <td><input type="text" value="3.9" /></td></tr>
  <tr><td><input type="text" value="Value 3" /></td>
  <td><input type="text" value="7.1" /></td></tr>
</table>
Here is the sum of all the values.<br />
  <input type="text" name="sum" value="0" 
    /><br />
</form>
<script language="javascript" type="text/javascript">
  var sum=0;
  for (var i=1; i<(sumform.elements.length-1); i+=2)	     
    sum+=parseFloat(sumform.elements[i].value);
  sumform.elements[sumform.elements.length-1].value=sum;
</script>
</body>
</html>
-------------------------------
Document 5.9 (buttonAccess.htm)

<html>
<head>
<title>Accessing Radio Buttons and Checkboxes</title>
</head>
<body>
Access contents of form fields...<br />
<form>
Give name: <input type="text" name="Ename" size="15" value="Mr. Bland" /><br />
Employee is punctual: 
Y <input type="radio" name="punctual" value="Y" 
    checked />&nbsp; &nbsp; &nbsp; 
N <input type="radio" name="punctual" value="N" /><br />
Employee likes these animals:
Dogs <input type="checkbox" name="animals" value="dogs" />
Cats <input type="checkbox" name="animals" value="cats" 
    checked />
Boa constrictors <input type="checkbox" name="animals" 
    value="boas" checked /><br />    
<input type="button" 
    value="Check here to examine form contents." 
  onclick="howMany.value=elements.length; 
    contents.value=elements[parseFloat(n.value)].value; 
	var i;
	if (punctual[0].checked)
	    alert(Ename.value+' is always on time.');
	else
	    alert(Ename.value+' is always late.');
	for (i=0; i<animals.length; i++) {
	  if (animals[i].checked) alert(Ename.value+
           ' likes '+animals[i].value);
	}; " /><br />
# elements: <input type="text" name="howMany" 
    value="0" /><br />    
Which one (0 to # elements - 1)? <input type="text" name="n" 
    value="1" />
Contents: <input type="text" name="contents" 
    value="--" /><br />
</form>
</body>
</html>
--------------------------------
Document 5.10 (chooseSelect.htm)

<html>
<head>
<title>Using values from a select list</title>	 
<script language="JavaScript" type="text/javascript" >
  function whichSelected(list) {					  
    var n=list.length;	  
	var i;
	var s="";	
	for (i=1; i<n; i++) {
	  if (list.options[i].selected) 
      s = s+" "+list.options[i].value;
    }
	return s;														
  }
</script>
</head>
<body >
<h2>Shows how to access values from a <font face="courier">&lt;select&gt;</font> tag.</h2> 
<h3>Only one item can be chosen...</h3>
  <select name="unique" size="3">
    <option value="unique1" selected>unique 1</option>
    <option value="unique2" >unique 2</option>
    <option value="unique3" >unique 3</option>
</select><br />			  
Click in this field to see what you have chosen: <input type="text" name="selectedUnique" 
onfocus="selectedUnique.value = 
  unique.options[unique.selectedIndex].value ;" /><br />   
<h3>Multiple items can be chosen...</h3>	
Hold down <font face="Arial"><b>Shift</b></font> or 
<font face="Arial"><b>Ctrl</b></font> 
key to make multiple selections.<br />
<select name="multiple" size="3" multiple>
  <option value="multiple1" selected>multiple 1</option>
  <option value="multiple2">multiple 2</option>
  <option value="multiple3" >multiple 3</option>
</select><br />							  
	Click on this field to see what you have chosen: 
<input size="40" type="text" name="selectedMultiple"
  onfocus="selectedMultiple.value=whichSelected(multiple);" /><br />
</body>
</html>
-----------------------------
Document 5.11 (siteData4.htm)

<html>
<head>
<title>"Multidimensional" arrays</title>
<script language="javascript" src="siteData.dat">
// This file defines the site characteristics.
</script>
<script language="javascript" type="text/javascript">
var i;
for (i=0; i<siteID.length; i++) {
	document.write(siteID[i].ID+", "+siteID[i].lat+", "+siteID[i].lon+","+siteID[i].elev+"<br />");
}
</script>
</head>
<body>
</body>
</html>
-----------------------------------------
Data file siteData.dat for siteData4.htm:
 

var siteID = new Array();
function IDArray(ID,lat,lon,elev) {
  this.ID=ID;
  this.lat=lat;
  this.lon=lon;
  this.elev=elev;
}
siteID[0]=new IDArray("Drexel",39.955,-75.188,10.);
siteID[1]=new IDArray("home",40.178,-75.333,140.);
siteID[2]=new IDArray("NC",35.452,-81.022,246);
siteID[3]=
   new IDArray("Argentina",-34.617,-58.367,30.);
siteID[4]=new IDArray("Netherlands",52.382,4.933,-1);

-----------------------------
Document 5.12 (password1.htm)

<html>
<head>
<title>Check a password</title>
<script language="javascript" type="text/javascript">
var PWArray=new Array();
PWArray[0]="mypass";
PWArray[1]="yourpass";
</script>
</head>
<body>
<form>
Enter your password: <input type="password" name="PW" value=""
onchange="var found=false; result.value='not OK';
 for (var i=0; i<PWArray.length; i++)
   if (PW.value == PWArray[i]) {
     found=true;
     result.value='OK'; 
   } " /><br />
(Tab to or click on this box to check your password.)<br />
<input type="text" name="result" 
value="Click to check password." />
</form>
</body>
</html>
-------------------------------
Document 5.13 (magicSquare.htm)

<html>
<head>
<title>magic Square</title>
<script language="javascript" type="text/javascript">
	var a=[[8,1,6],[3,5,7],[4,9,2]];
	var r,c; //alert(a[0].length);
	for (r=0; r<a.length; r++) {
		for (c=0; c<a[0].length; c++)
			document.write(a[r][c]+" ");
		document.write("<br />");
	}
</script>
</head>
<body>
</body>
</html>
--------------------------
Document 6.1 (circle1.htm)

<html>
<head>
<title>Circle Area (1)</title>
<script language="javascript" type ="text/javascript">
	function getArea(r) {
	  return Math.PI*r*r;
	}
</script>
</head>
<body>
<h1>Circle Area (1)</h1>
<p> 
<form> 
  Enter radius, then press tab key or click on "area" box.<br />
  radius (cm):
  <input type="text" name="radius" size="6" maxlength="7" 
   value="-99",
   onblur = "area.value=getArea(parseFloat(radius.value));">
   area (cm<sup>2</sup>): 
  <input type="text"  name="area" size="6" maxlength="7" value="-99">
</form>
</body>
</html>
--------------------------
Document 6.2 (circle2.htm)

<html>
<head>
<title>Circle Area (2)</title>
<script language="javascript" type ="text/javascript">
  function getArea(r) {
   return Math.PI*parseFloat(r.value)*parseFloat(r.value);
  }
</script>
</head>
<body>
<h1>Circle Area (1)</h1>
<p> 
<form> 
  Enter radius, then press tab key or click on "area" box.<br />
  radius (cm):
  <input type="text" name="radius" size="6" maxlength="7" 
   value="-99",	onblur = "area.value=getArea(radius);">
   area (cm<sup>2</sup>): 
  <input type="text"  name="area" size="6" maxlength="7" value="-99">
</form>
</body>
</html> 
--------------------------
Document 6.3 (circle3.htm)

<html>
<head>
<title>Circle Area (4)</title>
<script language="javascript" type ="text/javascript">
	function getArea(f) {
	  var r=parseFloat(f.radius.value);
	  f.area.value = Math.PI*r*r;
	}
</script>
</head>
<body>
<h1>Circle Area (3)</h1>
<form> 
  Enter radius, then press tab key or click on "area" box.<br />
  radius (cm):
  <input type="text" name="radius" size="6" 
    maxlength="7" value="-99",
    onblur = "getArea(form);" />
  area (cm<sup>2</sup>): 
  <input type="text"  name="area" size="6" 
    maxlength="7" value="-99" />
</form>
</body>
</html>
------------------------------
Document 6.4 (circleStuff.htm)

<html>
<head>
<title>Circle Stuff</title>
<script language="javascript" type ="text/javascript">
	function circleStuff(f) {
	  var r=parseFloat(f.radius.value);
	  f.area.value=Math.PI*r*r;
	  f.circumference.value=2.*Math.PI*r;
	}
</script>
</head>
<body bgcolor="#99ccff">
<h1>Circle Stuff</h1>
<form> 
  Enter radius, then press tab key or click on "area" box.<br />
  radius (cm):
  <input type="text" name="radius" size="6" 
    maxlength="7" value="-99",
    onblur = "circleStuff(form);" />
  area (cm<sup>2</sup>): 
  <input type="text" name="area" size="6" 
    maxlength="7" value="-99" />
  circumference(cm): 
  <input type="text" name="circumference" size="6" 
    maxlength="7" value="-99" />
</form>
</body>
</html>
-------------------------------
Document 6.5 (circleStuff2.htm)

<html>
<head>
<title>Circle Stuff with Arrays</title>
<script language="javascript" type ="text/javascript">
  function circleStuff(r) {
    var A = Array();	
    A[0] = Math.PI*r*r;
    A[1] = 2.*Math.PI*r;	
    return A;
  }
</script>
</head>
<body bgcolor="#99ccff">
<h1>Circle Stuff</h1>
<form> 
  Enter radius, then press tab key or click on "area" or "circumference field.<br />
  radius (cm):
  <input type="text" name="radius" size="6" 
    maxlength="7" value="-99",
    onblur = "var A = Array(); 
    A = circleStuff(parseFloat(radius.value));
    area.value = A[0]; circumference.value = A[1]; " />
  area (cm<sup>2</sup>): 
  <input type="text" name="area" size="6" 
    maxlength="7" value="-99" />
  circumference(cm): 
  <input type="text" name="circumference" size="6" 
    maxlength="7" value="-99" />
</form>
</body>
</html>
------------------------------
Document 6.6 (parseIntBug.htm)

<html>
<head>
<title>parseInt() "bug"</title>
</head>
<body>
<form>
integer value: <input name="x" value="09" /><br />
Click for parseInt("string") result: <input name="x_int" 
  onclick="x_int.value=parseInt(x.value); " /><br />
Click for parseInt("string",10) result: <input name="x_int10"
  onclick="x_int10.value=parseInt(x.value,10); " /><br />
Click for parseFloat("string") result: 
  <input name= "x_float"
  onclick="x_float.value=parseFloat(x.value); " />
</form>
</body>
</html>
-----------------------------
Document 6.7 (calculator.htm)

<html>
<head>
<title>Simple Calculator</title>
</head>
<body bgcolor="silver">
<form>
  Type expression to be evaluated, using numbers 
    and +, -, *, /:<br /> 
  <input type="text" name="expression" size="30" 
    maxlength="30" 
    onchange="result.value=eval(expression.value);"
  />
  <input type="text" name="result" size="8" 
    maxlength="8" />
</form>
</body>
</html>
-----------------------------
Document 6.8 (factorial2.htm)

<html>
<title>Calculate n!</title>
<body>
<script language="JavaScript" type="text/javascript">
function nFactorial(n) {
		if (n<=1) return 1;
		else return n*nFactorial(n-1);
	}
</script>
</head>
<h1>Calculate n factorial (n!)</h1>
<p> 
<form> 
  Enter n (a non-negative integer):
  <input type="text" name="n" size="2" maxlength="3" value="0" 
  onblur="factorial.value=
    nFactorial(parseInt(n.value,10));" />  
 (Press Tab to get n!.)<br>
  <input type="text" name="factorial" size="10" 
    maxlength="11" value="1" /> <br />
</form>
</body>
</html>
----------------------------
Document 6.9 (fibonacci.htm)

<html>
<title>Calculate Fibonacci numbers</title>
<body>
<script language="JavaScript" type="text/javascript">
  function Fib(n) {
    if (n<=2) return 1;
    else return Fib(n-1)+Fib(n-2);
  }
</script>
</head>
<h1>Calculate the n<sup>th</sup> Fibonacci number</h1>
<p> 
<form> 
  Enter n (a positive integer):
  <input type="text" name="n" size="2" maxlength="3" value="1"
  onblur="FibN.value=Fib(parseInt(n.value));" /> (Press Tab to get n<sup>th</sup> 
  Fibonacci number.)<br>
  <input type="text" name="FibN" size="8" 
    maxlength="8" value="1" />
</form>
</body>
</html>
--------------------------
Document 6.10 (towers.htm)
	
<html>
<head>
<title></title>
<script language="javascript" type="text/javascript">
  function move(n,start,end,intermediate) {
   if (n > "0") {
    move(n-1,start,intermediate,end);
    document.write("move ring "+n+
     " from "+start+" to "+end+".<br />");
    move(n-1,intermediate,end,start);
   }
  }
  var n=prompt("Give n:");
  move(n,"A","C","B");
</script>
</head>
<body>
</body>
</html>
---------------------------
Document 6.11a (passID.htm)

<html>
<head>
<title>Get ID and password.</title>
<script language="javascript" type="text/javascript">
  function checkIDPW() {
    var PWinput=login_form.PW.value;
    var IDinput=login_form.ID.value;
    var flag=prompt("ID = "+IDinput+
      ", PW = "+PWinput+". OK (y or n)?");
    if (flag == "y") return true; else return false;
  }
</script>
</head>
<body>
  <form method="link" action="catchID.htm" 
    name="login_form" onsubmit="checkIDPW();">
  ID: <input type="text" name="ID">
  PW: <input type="text" name="PW">
  <input type="submit" value="Access protected page.">
</form>
</body>
</html>
----------------------------
Document 6.11b (catchID.htm)

<html>
<head>
<title>Receive ID and password from another 
  document.</title>
</head>
<body>
<form name="catchForm">
<input type="hidden" name="info">
</form>
<script language="javascript" type="text/javascript">
catchForm.info.value=window.location;
// alert(window.location);
function getID(str)
{
  theleft=str.indexOf("=")+1;
  theright=str.lastIndexOf("&");
  return str.substring(theleft,theright);
}
function getPW(str) {
  theleft=str.lastIndexOf("=")+1;
  return str.substring(theleft);
}
document.write("ID is "+getID(catchForm.info.value)+", PW is "+getPW(catchForm.info.value));
</script>
</body>
</html>
-------------------------
Document 6.12 (sort2.htm)

<html>
<head>
<title>Sorting Arrays</title>
<script language="javascript" type="text/javascript">
  function compare(x,y) {
    var X=parseFloat(x); Y=parseFloat(y);
      if (X<Y) return -1;
      else if (X==Y) return 0;
      else return 1;
  }
  var a=[7,5,13,3];
  var i;
  document.write(a + " length of a = " + a.length+"<br />");
  a.sort(compare);
  document.write(a + " length of a = " + a.length+"<br />");
</script>
</head> 
<body>
</body>
</html>
----------------------------
Document 6.13 (dewpoint.htm)

<html>
<head>
<title>Dewpoint Calculator</title>
<body>
<script language="JavaScript" type="text/javascript">
  function getDewpoint(T,RH) {
    var a=17.27,b=237.7,alpha;
    var temp=parseFloat(T.value);
    var rh=parseFloat(RH.value)/100;
    alpha=a*temp/(b+temp)+Math.log(rh);
    return ((b*alpha)/(a-alpha)).toFixed(2);
  }
</script>
</head>
<h1>Dewpoint Temperature Calculator</h1>
<p> 
<form> 
<input type="reset" value="Reset" /><br />
Temperature:
<input type="text" name="T" size="5" maxlength="6" 
  value="-99" /> C <br />
Relative Humidity:
<input type="text" name="RH" size="6" maxlength="6" 
  value="-99" /> % <br />
<br />
<input type="button"
  value=    "Click here to get dewpoint temperature (deg C)." 
  onclick="DP.value=getDewpoint(T,RH)" /> 
<br /><br />
Dewpoint Temperature: <input type="text" name="DP" size="5" maxlength="6" value="-99" /> C<br />
</p>
</form>
</body>
</html>
------------------------
Document 6.14 (loan.htm)

<html>
<head>
<title>Loan Calculator</title>
<body bgcolor="#99ccff">
<script language="JavaScript" type="text/javascript">
	function getPayment(P,r,n) {
	  r=r/100./12.;
	  var M=P*r/(1.-1./Math.pow(1.+r,n));
	  return M.toFixed(2)
	}
</script>
</head>
<h1>Loan Calculator</h1>
<p> 
<form> 
Principal Amount: $:
<input type="text" name="amount" size="9" 
  maxlength="9" value="0" /><br />
Annual rate: %
<input type="text" name="rate" size="6" 
  maxlength="6" value="0" />
<br />
Number of Months: 
<input type="text" name="n" size="3" 
  maxlength="3" value="0" /><br />
<input type="button"
  value="Click here to get monthly payment."
  onclick=
   "monthly.value=getPayment(parseFloat(amount.value),
    parseFloat(rate.value),parseInt(n.value,10));" />
<br />
Monthly Payment: $
<input type="text" name="monthly" size="9" 
  maxlength="9" />
</form>
</body>
</html>
----------------------------
Document 6.15 (legendre.htm)

<html>
<head>
  <title>Legendre Polynomials</title>
  <script language="JavaScript" type="text/javascript">
  function Legendre(n,x) {				   
    if (n == 0) return 1;
    else if (n == 1) return x;
    else 
   return (2*n-1)/n*Legendre(n-1,x)-(n-1)/n*Legendre(n-2,x);
  }
  </script>
</head>
<body>
<h3>Calculate the n<sup>th</sup> Legendre polynomial, P<sub>n</sub>(x), for any x and n &ge; 0</h3>
<form> 
  n (&ge; 0): 
<input type="text" name="n" value="3" /><br />
  x: 
<input type="text" name="x" value="1.5" /><br />
<input type="button" 
  value="Click here to calculate Legendre polynomial" onclick="L.value=
  Legendre(parseInt(n.value,10),parseFloat(x.value));" />
<br />
   Legendre polynomial: <input type="text" name="L" />
</form>
</body>
</html>
-----------------------------
Document 6.16 (buildMenu.htm)

<html>
<head>
<title>Build a variable-length pull-down menu</title>
<script language="javascript" type="text/javascript">
  var listItems = new Array();
  listItems[0]="thing1";
  listItems[1]="thing2";
  listItems[2]="thing3";
  listItems[3]="things4";
  listItems[4]="newthing";
  function buildSelect(list,things) {
    var i;//alert(things);
    for (i=0; i<things.length; i++)
      list.options[i]=new Option(things[i],things[i]);
  }
  function getSelected(list) {
    var i;
    for (i=0; i<list.length; i++)
      if (list.options[i].selected) 
        return list.options[i].value;
  }
</script>
</head>
<body onload="buildSelect(menuForm.stuff,listItems);" >
<form name="menuForm" >
Here's the menu:<br />
Click on an item to select it.<br />
<select name="stuff" size="10" 
  onchange="listChoice.value=getSelected(stuff); ">
</select><br />
This is the item selected:
<input type="text" name="listChoice" value=""/>
</form>
</body>
</html>
------------------------------------
Document 6.17a (passwordProtect.htm)

<html>
<script language="JavaScript"><!--
function check() {
/* This code uses the name of an htm file as a "password."
Prompt the user for the password.
*/
  var pwd = prompt('Enter password before continuing...');
/* Access the file, assuming it's in the same directory. 
If it's not, then include a string pointing to the location.
*/
  location.href=pwd + ".htm";
}
// -->
</script>
<head>
<title></title>
</head>
<body>
<p>You will need a password to visit my site.</p>
  <form>
    <input type="button" onClick="check()" 
     value="Click here to enter site." />
  </form>
</body>
</html>
----------------------------
Document 6.17b (&_crazy.htm)

<html>
<head>
  <title></title>
</head>
<body>
<p>You have entered &_crazy.htm.</p>
</body>
</html>
----------------------------
Document 7.1. (getCalib.htm)

<html>
<head>
<title>Get calibration constant</title>
<script language="javascript">
  document.write("This document last modified on "
    + document.lastModified+".")
</script>
</head>
<body>
<h2>Get calibration constants for water vapor instrument</h2>
<p>
<form method="post" action="getCalib.php">
Enter serial number here: <input type="text" name="SN" value="WV2-157" /><br />
<input type="submit" 
  value="Click here to get calibration constants" />
</body>
</html>
----------------------------
Document 7.2. (getCalib.php)

<?php
// Extract instrument ID from POST data...
  $SN=$_POST["SN"];
  $len=strlen($SN);
// Open WV instrument calibration constant file...
  $inFile = "WVdata.dat";
  $in = fopen($inFile, "r") or die("Can't open file");
// Read one header line...
  $line=fgets($in);

// Search rest of file for SN match...
  $found=0;
  while  ((!feof($in)) && ($found == 0)) {
// Could do it like this...
//	  $line=fgets($in);
//	  $values=sscanf($line,"%s %f %f %f %f %f");
// or like this...
//   	  fscanf($in,"%s %f %f %f %f %f",
//            $SN_dat,$A,$B,$C,$beta,$tau);
  list($SN_dat,$A,$B,$C,$beta,$tau)=fscanf($in,
                              "%s %f %f %f %f %f");
	  if (strncasecmp($SN_dat,$SN,$len)==0)  
  $found=1;
	}
  fclose($in);
  if ($found == 0) echo 
         "Couldn't find this instrument.";
  else {
// Build table of outputs...
  echo "<p><table border='2'><tr><th>Quantity</th><th>Value</th></tr>"."</td></tr>";
  echo "<tr><td>Instrument ID</td><td>$SN</td></tr>";
  echo "<tr bgcolor='silver'><td colspan='2'>
    Calibration Constants</td></tr>";
  echo "<tr><td>A</td><td>$A</td></tr>";
  echo "<tr><td>B</td><td>$B</td></tr>";
  echo "<tr><td>C</td><td>$C</td></tr>";
  echo "<tr><td>&tau;</td><td>$tau</td></tr>";
  echo "<tr><td>&beta;</td><td>$beta</td></tr>";
  echo "</table>";
  }
?>
------------------------------
Document 7.3. (helloWorld.php)

<?php
  echo "Hello, world!";
?>
---------------------------
Document 7.4. (PHPInfo.php)

<?php
  echo phpinfo();
?>
------------------------------------
(partial data file for Document 7.5)
SN A B C beta tau
WV2-113, 0.762, 0.468, 0.20, 0.65, 0.10
------------------------------
Document 7.5. (writeCalib.php)

<html>
<head>
<title>Get calibrations for water vapor instrument</title>
</head>
<body>
<?php
// Extract instrument ID from POST data
  $SN=$_POST["SN"];
  $len=strlen($SN);
// Open WV instrument calibration constant file
  $inFile = "WVdata.dat";
  $outFile="WVreport.csv";
  $in = fopen($inFile, "r") or 
                   exit("Can't open file.");
  $out=fopen($outFile,a);
// Read one header line...
// Read one header line
  	$line=fgets($in);
// Search rest of file for SN match
	$found=1;
	while  ((!feof($in)) && ($found == 1)) {
	  $line=fgets($in);
	  $values=sscanf($line,"%s %f %f %f %f %f");
	  list($SN_dat,$A,$B,$C,$beta,$tau)=$values;
	  if (strncasecmp($SN_dat,$SN,$len)==0) 
          $found=0;
	}
	fclose($in);
	if ($found == 1) 
        echo "Couldn't find this instrument.";
	else {
// Build table of outputs
echo "<p><table border='2'>
     <tr><th>Quantity</th><th>Value</th></tr>";
echo "</td></tr>";
echo "<tr><td>Instrument ID</td><td>$SN</td></tr>";
echo "<tr bgcolor='silver'>
     <td colspan='2'>Calibration Constants</td></tr>";
echo "<tr><td>A</td><td>$A</td></tr>";
echo "<tr><td>B</td><td>$B</td></tr>";
echo "<tr><td>C</td><td>$C</td></tr>";
echo "<tr><td>&tau;</td><td>$tau</td></tr>";
echo "<tr><td>&beta;</td><td>$beta</td></tr>";
echo "</table>";
	}
fprintf($out,"Data have been reported for: %s,%f,%f,%f,%f,%f\n",$SN,$A,$B,$C,$tau,$beta);
fclose($out);
?>
</body>
</html>
---------------------------
Document 7.6a (quadrat.htm)

<head>
<title>Solving the Quadratic Equation</title>
</head>
<body>
<form method="post" action="quadrat.php">
Enter coefficients for ax<sup>2</sup> + bx + c = 0:
<br />
a = <input type="text" value="1" name="a" />
 (must not be 0)<br />
b = <input type="text" value="2" name="b" /><br />
c = <input type="text" value="-8" name="c" /><br /><br />
<input type="submit" value="click to get roots..." />
</form>
</body>
</html>
---------------------------
Document 7.6b (quadrat.php)

<?php
$a = $_POST["a"];
$b = $_POST["b"];
$c = $_POST["c"];
$d = $b*$b - 4*$a*$c;
if ($d == 0) {
  $r1 = $b/(2*$a);
  $r2 = "undefined";
}
else if ($d < 0) {
  $r1 = "undefined";
  $r2 = "undefined";
}
else {
  $r1 = (-$b + sqrt($b*$b - 4*$a*$c))/2/$a;;
  $r2 = (-$b - sqrt($b*$b - 4*$a*$c))/2/$a;;
}
echo "r1 = " . $r1 . ", r2 = " . $r2; 
?>
---------------------------------
Document 7.7a (WeatherReport.htm)

<html>
<head>
<title>Weather Report</title>
<script language="javascript" type="text/javascript" >
  var alreadySubmitted = false;
  function submitForm ( )
  {
    if (alreadySubmitted)
    {
      alert("Data already submitted. Click on 'Reset Button' and start over." );
      return false;
    }
    else
    {
      alreadySubmitted = true;
      return true;
    }
  }
</script>
</head>
<body>
<h2>Report weather observations</h2>
<form method="post" action="WeatherReport.php" 
     onSubmit="return submitForm(this.form);" >
  Date (mm/dd/yyyy) : <input type="text" name="date" 
     value="09/23/2007" /><br />
  Time (UT hh:mm:ss): <input type="text" name="time" 
     value="17:00:00" /><br />
  Air temperature (deg C):<input type="text" name="T" 
     value="23" /><br />
  Barometric pressure (millibar): <input type="text"    
     name="BP" value="1010" /><br />
  Cloud cover (octas 0-8): <input type="text" name="octas" 
     value="7" /><br />
  Precipitation today (total mm): <input type="text" 
     name="precip" value="2.3" /><br />
  <input type="submit" name="PushButton" 
     value="Click to submit..." /><br />
  <input type="reset" value="Reset Button" 
     onClick="alreadySubmitted=false;"/>
</body>
</html>
---------------------------------
Document 7.7b (WeatherReport.php)

<?php
  $date=$_POST["date"];
  $time=$_POST["time"];
  $T=$_POST["T"];
  $BP=$_POST["BP"];
  $octas=$_POST["octas"];
  $precip=$_POST["precip"];
  echo "You have reported:<br />" .
    "date:" . $date . "<br />" . 
    "time: " . $time . "<br />" .
    "BP : " . $BP . "<br />" .
    "octas: " . $octas . "<br />" .
    "precip: " . $precip . "<br />";
  $out=fopen("WeatherReport.csv","a");
  fprintf($out,"%s, %s, %.1f, %.2f, %u, %.2f\r\n",
            $date,$time,$T,$BP,$octas,$precip);
  fclose($out);
?>
--------------------------
Document 8.1 (PWcalc2.htm)

<html>
<head>
<title>WV calculations for calibrated instrument</title>
<script language="javascript">
	document.write("This document last modified on "+document.lastModified+".");
</script>
<script language="javascript">
function getSunpos(m,d,y,hour,minute,second,Lat,Lon) {
with (Math) {
// Explicit type conversions to make sure inputs are treated like numbers, not strings.
  m=parseInt(m,10); d=parseInt(d,10); 
  y=parseInt(y,10); 
  hour=parseFloat(hour); minute=parseFloat(minute); second=parseFloat(second);
  Lat=parseFloat(Lat); Lon=parseFloat(Lon);
// Julian date  
  var temp=ceil((m-14)/12);
//This number is always <=0.
  var JD = d - 32075 + floor(1461*(y+4800+temp)/4)
    +floor(367*(m-2-temp*12)/12) 
// m-2-temp*12 is always > 0
    -floor(3*(floor((y+4900+temp)/100))/4);
  JD =JD-0.5+hour/24+minute/1440+second/86400;
// Solar position, ecliptic coordinates
  var dr=PI/180;
  var T=(JD-2451545)/36525; 
  var L0=280.46645+36000.76983*T+0.0003032*T*T;
  var M=357.52910+35999.05030*T-0.0001559*T*T-0.00000048*T*T*T;
  var M_rad=M*dr;
  var e=0.016708617-0.000042037*T-0.0000001236*T*T; 
  var C=(1.914600-0.004817*T-0.000014*T*T)*sin(M_rad)
    +(0.019993-0.000101*T)*sin(2*M_rad)+0.000290*sin(3*M_rad);
  var L_save=(L0+C)/360;
  if (L_save < 0) var L_true=(L0+C)-ceil(L_save)*360;
  else var L_true=(L0+C)-floor(L_save)*360;

  if (L_true < 0) L_true+=360;
  var f=M_rad+C*dr;
  var R =1.000001018*(1-e*e)/(1+e*cos(f));  
// Sidereal time
  var Sidereal_time=280.46061837+360.98564736629*(JD-2451545)+0.0003879*T*T-T*T*T/38710000; 
  S_save=Sidereal_time/360;
  if (S_save < 0) Sidereal_time=Sidereal_time-ceil(S_save)*360;
  else Sidereal_time=Sidereal_time-floor(S_save)*360;
  if (Sidereal_time < 0) Sidereal_time+=360; 
// Obliquity
  var obliquity=23+26/60+21.448/3600-46.8150/3600*T-0.00059/3600*T*T +0.001813/3600*T*T*T; 
// Ecliptic to equatorial
  var right_Ascension =  
    atan2(sin(L_true*dr)*cos(obliquity*dr),
    cos(L_true*dr)); 
  var declination = 
    asin(sin(obliquity*dr)*sin(L_true*dr)); 
  var Hour_Angle = Sidereal_time + Lon 
    right_Ascension/dr; // Don't know why!! 
  var Elev=asin(sin(Lat*dr)*sin(declination)+
    cos(Lat*dr)*cos(declination)*cos(Hour_Angle*dr)); 
/* relative air mass from Andrew T. Young, Air mass and refraction (Eq. 5),
 Appl. Opt., 33, 6, 1108-1110 (1994) */
  var cosz=cos(PI/2-Elev); 
  } // End with (Math) 
  var airm=(1.002432*cosz*cosz+0.148386*cosz+
    0.0096467)/(cosz*cosz*cosz+0.149864*cosz*cosz+
    0.0102963*cosz+0.000303978);
  return airm;
}
function get_PW(IR1,IR2,A,B,C,beta,tau,airm,p) {
/* NOTE: 
	1. Station pressure may be included in these calculations in the future.
	2. No addition operations in these calculations, so explicit string conversions to numbers not required.
*/
  var x = C*airm*tau - (Math.log(IR2/IR1)-A)/B;
  var PW = Math.pow(x,1/beta)/airm;
  return Math.round(PW*1000)/1000;
}
</script>
</head>
<body bgcolor="white">
<h2>Calculations for Total Precipitable Water Vapor (PW)</h2>
<p> 
<form>
<table border="2">
  <tr bgcolor="silver"><td colspan="4">
    <b>Location:</b></td></tr>
    <td>longitude (decimal degrees): </td>
    <td> <input type="text" name="lon" value="-75.188" size="8"> </td>
    <td>latitude (decimal degrees): </td><td> 
<input type="text" name="lat"
      value="39.955" size="8"></td>
  </tr>
  <tr bgcolor="silver"><td colspan="4">
<b>Calibration constants:</b></td>
  </tr>
  <tr><td><b>A (you <u><i>must</i></u> provide a value)</b>, B, and C:</td>
    <td><input type="text" name="A" value="" size="8"></td>
    <td><input type="text" name="B" value="0.468" size="8"></td>
    <td><input type="text" name="C" value="0.2" size="8"></td>
    <tr><td colspan="2">&beta; and &tau;:</td>
    <td><input type="text" name="beta" value="0.65" size="4"></td>
    <td><input type="text" name="tau" value="0.10" size="4"></td>
  </tr>
  <tr bgcolor="silver"><td colspan="4"><b>Date:</b></td>
  </tr>
  <tr><td>mm/dd/yyyy</td>
    <td><input type="text" name="mon" value="4" size="3"></td>
    <td><input type="text" name="day" value="5" size="3"></td>
    <td><input type="text" name="yr" value="2007" size="5"></td>
  </tr>
  <tr bgcolor="silver"><td colspan="4"><b>Time:</b></td></tr>
  <tr><td>hh:mm:ss (<b><i><u>must</u></i> be Universal Time</b>)</td>
    <td><input type="text" name="hr" value="14" size="3"></td>
    <td><input type="text" name="min" value="33" size="3"></td>
    <td><input type="text" name="sec" value="15" size="3"></td>
  </tr>
  <tr><td colspan="2" bgcolor="silver">
    <b>Station pressure (mbar, not currently used in calculation):</b></td>
    <td colspan="2"><input type="text" name="p" value="1013"size="7"></td>
  </tr>
  <tr bgcolor="silver"><td colspan="4"><b>Instrument voltages:</b></td></tr>
  <tr>
    <td>IR1</td>
    <td><input type="text" name="IR1" value="0.742" size="5"> </td>
    <td>IR1<sub>dark</sub></td>
    <td><input type="text" name="IR1_dark" value="0.003" size="5"></td>
  </tr>
  <tr>
    <td>IR2</td>
    <td><input type="text" name="IR2" value="0.963" size="5"></td>
    <td>IR2<sub>dark</sub></td>
    <td><input type="text" name="IR2_dark" value="0.004" size="5"></td>
  </tr>
</table>
<input type="button" value="Click here to calculate relative air mass and PW"
onclick="
// Get relative air mass			
airm.value=getSunpos(this.form.mon.value,
  this.form.day.value,this.form.yr.value,
  this.form.hr.value,this.form.min.value,
  this.form.sec.value,
  this.form.lat.value,this.form.lon.value);
// then PW
PW.value=get_PW(this.form.IR1.value-this.form.IR1_dark.value,
  this.form.IR2.value-this.form.IR2_dark.value,
  this.form.A.value,this.form.B.value,
  this.form.C.value,this.form.beta.value,
  this.form.tau.value,
  this.form.airm.value,this.form.p.value); 
airm.value=Math.round(airm.value*10000)/10000;">
<br />
Relative air mass: <input type="text" name="airm" value="0"size="7">
<br />
Overhead precipitable water vapor (cm H<sub>2</sub>O): 
<input type="text" name="PW" value="0" size="7">
</form>
</body>
</html>
---------------------------
Document 8.2a (PWcalc3.htm)

<html>
<head>
<title>WV calculations for calibrated instrument</title>
<script language="javascript">
 document.write("This document last modified on "
   +document.lastModified+".");
</script>
</head>
<body bgcolor="white">
<h2>Calculations for Total Precipitable Water Vapor (PW)</h2>
<p>
<form method="post" action="PWcalc3.php">
<table border="2">
  <tr bgcolor="silver"><td 
      colspan="4"><b>Location:</b></td></tr>
  <td>longitude (decimal degrees): </td>
  <td> <input type="text" name="lon" value="-75.188"
     size="8"> </td>
  <td>latitude (decimal degrees): </td>
  <td><input type="text" name="lat" value="39.955"
    size="8"></td></tr>
  <tr bgcolor="silver"><td colspan="4">
    <b>Instrument Serial Number:</b></td></tr>
  <tr><td colspan="4"><input type="text" name="SN"
    value="WV2-117" /></td></tr>
  <tr bgcolor="silver">
    <td colspan="4"><b>Date:</b></td></tr>
  <tr><td>mm/dd/yyyy</td>
  <td><input type="text" name="mon" value="4"
    size="3"></td>
  <td><input type="text" name="day" value="5"
    size="3"></td>
  <td><input type="text" name="yr" value="2005"
    size="5"></td></tr>
  <tr bgcolor="silver"><td colspan="4"><b>Time:</b></td>
  <tr><td>hh:mm:ss (<b><i><u>must</u></i> be Universal 
     Time</b>)</td>
  <td><input type="text" name="hr" value="14"
    size="3"></td>
  <td><input type="text" name="min" value="33"
    size="3"></td>
  <td><input type="text" name="sec" value="15"
    size="3"></td></tr>
  <tr><td colspan="2" bgcolor="silver"><b>Station
     pressure (mbar, not currently used in
     calculation):</b></td>
  <td colspan="2"><input type="text" name="p"
     value="1013" size="7"></td></tr>
  <tr bgcolor="silver"><td colspan="4">
     <b>Instrument voltages:</b></td></tr>
  <tr>
  <td>IR1</td>
  <td><input type="text" name="IR1" value="0.742"
    size="5"> </td>
  <td>IR1<sub>dark</sub></td>
  <td><input type="text" name="IR1_dark" value="0.003"
    size="5"></td>
</tr>
<tr>
<td>IR2</td>
<td><input type="text" name="IR2" value="0.963"
  size="5"></td>
<td>IR2<sub>dark</sub></td>
<td><input type="text" name="IR2_dark" value="0.004"
  size="5"></td>
</tr>
</table>
<input type="submit" 
  value="Click here to calculate PW" />
</form>
</body>
</html>
---------------------------
Document 8.2b (PWcalc3.php)

<html>
<title>WV calculations for calibrated instrument
</title>
<?php 
function getJD($m,$d,$y,$hour,$minute,$second) {
// Julian date  
  $temp=ceil(($m-14)/12);//This number is always <= 0.
  $JD = $d - 32075 + floor(1461*($y+4800+$temp)/4)
    +floor(367*($m-2-$temp*12)/12) // m-2-temp*12 is always > 0.
    -floor(3*(floor(($y+4900+$temp)/100))/4);
  $JD =$JD-0.5+$hour/24+$minute/1440+$second/86400;
  return $JD;
}
function getSunpos($m,$d,$y,$hour,$minute,$second,$Lat,$Lon) {
// Retrieve Julian date
  $JD=getJD($m,$d,$y,$hour,$minute,$second);
// Solar position, ecliptic coordinates
  $dr=pi()/180;
  $T=($JD-2451545)/36525; 
  $L0=280.46645+36000.76983*$T+0.0003032*$T*$T;
  $M=357.52910+35999.05030*$T-0.0001559*$T*$T-0.00000048*$T*$T*$T;
  $M_rad=$M*$dr;
  $e=0.016708617-0.000042037*$T-0.0000001236*$T*$T; 
  $C=(1.914600-0.004817*$T-0.000014*$T*$T)*sin($M_rad)
    +(0.019993-0.000101*$T)*sin(2.*$M_rad)+0.000290*sin(3.*$M_rad);
// Replacement code for L_true=fmod(L0+c,360)
  $L_save=($L0+$C)/360;
  if ($L_save < 0) $L_true=($L0+$C)-ceil($L_save)*360;
  else $L_true=($L0+$C)-floor($L_save)*360;
  if ($L_true < 0) $L_true+=360;
  $f=$M_rad+$C*$dr;
  $R =1.000001018*(1-$e*$e)/(1.+$e*cos($f));  
// Sidereal time
  $Sidereal_time=280.46061837+360.98564736629*($JD-2451545.)+0.0003879*$T*$T-$T*$T*$T/38710000; 
// Replacement code for Sidereal=fmod(Sidereal,360)
  $S_save=$Sidereal_time/360.;
  if ($S_save < 0.) $Sidereal_time=$Sidereal_time-ceil($S_save)*360;
  else $Sidereal_time=$Sidereal_time-floor($S_save)*360;
  if ($Sidereal_time < 0.) $Sidereal_time+=360; 
// Obliquity
  $obliquity=23+26/60+21.448/3600-46.8150/3600*$T-0.00059/3600*$T*$T +0.001813/3600*$T*$T*$T; 
// Ecliptic to equatorial
  $right_Ascension = atan2(sin($L_true*$dr)*cos($obliquity*$dr),cos($L_true*$dr)); 
  $declination = asin(sin($obliquity*$dr)*sin($L_true*$dr)); 
  $Hour_Angle = $Sidereal_time + $Lon - $right_Ascension/$dr;
$elev=asin(sin($Lat*$dr)*sin($declination)+cos($Lat*$dr)*cos($declination)*cos($Hour_Angle*$dr)); 
/* relative air mass from Andrew T. Young, Air mass and refraction (Eq. 5),
 Appl. Opt., 33, 6, 1108-1110 (1994) */
  $cosz=cos(pi()/2-$elev); 
//  echo $cosz; 
$airm=(1.002432*$cosz*$cosz+0.148386*$cosz+0.0096467)/($cosz*$cosz*$cosz+
0.149864*$cosz*$cosz+0.0102963*$cosz+0.000303978);
  return $airm;
}
?>
</head>
<body bgcolor="white">
<?php 
echo "<h2>Calculations for Total Precipitable Water Vapor (PW)</h2>";
$m=getSunpos($_POST["mon"],$_POST["day"],$_POST["yr"],$_POST["hr"],$_POST["min"],$_POST["sec"],
$_POST["lat"],$_POST["lon"]);	
$IR1=$_POST["IR1"]-$_POST["IR1_dark"];
$IR2=$_POST["IR2"]-$_POST["IR2_dark"];
	$A=$_POST["A"];
	$B=$_POST["B"];
	$C=$_POST["C"];
	$beta=$_POST["beta"];
	$tau=$_POST["tau"];
	$SN=$_POST["SN"];
	$len=strlen($SN);
// Open WV instrument calibration constant file
	$inFile = "WVdata.dat";
  	$in = fopen($inFile, 'r') or die("Can't open file");
// Read one header line
  	$line=fgets($in);
// Search rest of file for SN match
	$found=1;
	while  ((!feof($in)) && ($found == 1)) {
	  $line=fgets($in);
	  $values=sscanf($line,"%s %f %f %f %f %f\n");
	  list($SN_dat,$A,$B,$C,$beta,$tau)=$values;
	  if (strncasecmp($SN_dat,$SN,$len)==0) {
	    $found=0;
	  }
	}
    fclose($in);
// Build table of outputs
echo "<p><table border='2'><tr><th>Input</th><th>Value</th></tr>";

echo "</td></tr>";
echo "<tr><td>Instrument SN</td><td>$SN</td></tr>";
echo "<tr bgcolor='silver'><td colspan='2'>Calibration Constants</td></tr>";
echo "<tr><td>A</td><td>$A</td></tr>";
echo "<tr><td>B</td><td>$B</td></tr>";
echo "<tr><td>C</td><td>$C</td></tr>";
echo "<tr><td>&tau;</td><td>$tau</td></tr>";
echo "<tr><td>&beta;</td><td>$beta</td></tr>";
echo "<tr bgcolor='silver'>
  <td colspan='2'>Measurements</td></tr>";
echo "<tr><td>IR1 (sunlight - dark)</td><td>$IR1</td></tr>";
echo "<tr><td>IR2 (sunlight - dark)</td><td>$IR2</td></tr>";
echo "</table></p>";
$x = $C*$m*$tau - (log($IR2/$IR1)-$A)/$B;
$PW = pow($x,1./$beta)/$m;
echo "<p><table border='2'><tr><th>Output</th>
  <th>Value</th></tr>";
echo "<tr><td>relative air mass</td><td>";
echo round($m,4);
echo "</td></tr>";
echo "<tr><td>PW, cm H<sub>2</sub>O</td><td>";
echo round($PW,4);
echo "</table></p>";
?>
</body>
</html>
------------------------------
Document 8.3 (circleStuff.php)

<?php
/* function CIRCLESTUFF($r) {...} 
   will also work because PHP function names are case-
   insensitive!
*/
  function CircleStuff($r) {
	  $area=M_PI*$r*$r;
	  $circumference=2*M_PI*$r;
    return array($area,$circumference);
  }
	list($area,$circumference) = CircleStuff(3.);
	echo $area . ", " . $circumference;
?>
------------------------------------
(partial data file for Document 8.4)
1 1991 31
 3.2, 0.4, 3.8, 4.5, 3.3, 1.9, 1.6, 3.7, 0.8, 2.3, 2.8, 2.4, 2.5, 3.2, 4.1, 3.9, 5.0, 4.4, 4.4, 5.5, 3.0, 3.7, 2.2, 2.0
 2.6, 2.8, 2.3, 2.3, 1.2, 2.4, 3.1, 4.0, 3.6, 2.9, 6.0, 4.4, 0.8, 3.8, 3.5, 4.5, 2.7, 3.4, 6.6, 5.2, 1.6, 1.2, 2.3, 2.4

2 1991 28
 4.6, 5.9, 3.1, 3.2, 4.5, 4.4, 3.9, 4.4, 7.5, 8.4,10.2, 9.2, 8.1, 6.3, 3.1, 3.5, 2.2, 1.4, 0.4, 4.2, 5.4, 4.0, 2.9, 1.7
 2.5, 2.3, 2.1, 1.5, 2.3, 4.1, 5.3, 6.0, 6.0, 9.7,11.3,12.7,13.0,13.0,11.6, 9.9, 9.6, 8.7, 5.4, 5.1, 5.3, 5.6, 4.4, 4.2

--------------------------
Document 8.4 (windspd.php) 

<?php
$inFile="windspd.dat";
$outFile="windspd.out";
$in = fopen($inFile, "r") or die("Can't open file.");
$out=fopen($outFile,"w");
while (!feof($in)) {
// Read one month, year, # of days.
  fscanf($in,"%u %u %u",$m,$y,$nDays);
  if (feof($in)) exit;
  echo $m . ', ' . $y . ', ' . $nDays . '<br />';
  $nMissing=0;
  for ($i=1; $i<=$nDays; $i++) {
    $hrly = fscanf($in,"%f,%f,%f,%f,%f,%f,%f,%f,%f, %f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f");
    for ($hr=0; $hr<24; $hr++) {
	// echo $hrly[$hr] . ', ';
	if ($hrly[$hr] == -1) $nMissing++;
    }	
    // echo $hrly[23] . '<br />';
  }
  echo 'Number of missing hours this month is ' . $nMissing   
.'.<br />';
	fprintf($out,"%u, %u, %u\r\n",$m,$y,$nMissing);
}
echo "All done.<br />"
// fclose($in);
// fclose($out);
?>
-----------------------------------
(density.dat file for Document 8.5)

material density (kg/m^3)
water 1000
aluminum 2700
gold 19300
silver 10500
oxygen 1.429
air 1.2

(volume.dat file for Document 8.5)

shape volume
cube $L*$L*$L
sphere 4/3*M_PI*$R*$R*$R
cylinder M_PI*$R*$R*$L
block $L*$W*$H

---------------------------
Document 8.5a (getMass.htm)

<html>
<head>
<title>Calculate mass</title>
</head>
<body>
<form name="form1" method="post" action="getMass.php">
Enter length: <input type="text" name="L" value="3" /><br />
Enter width: <input type="text" name="W" value="2" /><br />
Enter height: <input type="text" name="H" value="10" /><br />
Enter radius: <input type="text" name="R" value="3" /><br />
<select name="shapes" size="10">
  <option value="cube">cube</option>
  <option value="cylinder">cylinder</option>
  <option value="block">rectangular block</option>
  <option value="sphere">sphere</option>
</select>
<select name="material" size="10">
  <option value="air">air</option>
  <option value="aluminum">aluminum</option>
  <option value="gold">gold</option>
  <option value="oxygen">oxygen</option>
  <option value="silver">silver</option>
  <option value="water">water</option>	
</select>
<input type="submit" value="Click to get volume."
<!--
<input type="button" value="click"
onclick="alert(shapes.selectedIndex);
alert(material.options[material.selectedIndex].value); " />
 -->
  />
</form></body></html> 
---------------------------
Document 8.5b (getMass.php)

<?php
print_r($_POST);
$material=$_POST[material];
$shape=$_POST[shapes];
$L=$_POST[L];
$W=$_POST[W];
$H=$_POST[H];
$R=$_POST[R];
echo "<br />" . $material . ", " . $shape . "<br />";
$materialFile=fopen("density.dat","r");
$shapeFile=fopen("volume.dat","r");
// Read materials file.
$found=false;
$line=fgets($materialFile);
while ((!feof($materialFile)) && (!$found)) {
  $values=fscanf($materialFile,"%s %f",$m,$d);
  if (strcasecmp($material,$m) == 0) {
    echo $material . ", " . $m . ", " . $d . "<br />";
	$found=true;
  }
}
// Read volume file.
$found=false;
$line=fgets($shapeFile);
while ((!feof($shapeFile)) && (!$found)) {
  $values=fscanf($shapeFile,"%s %s",$s,$v);
  if (strcasecmp($shape,$s) == 0) {
    echo $shape . ", " . $v . "<br />";
	$found=true;
  }
}
// Close both data files.
fclose($materialFile);
fclose($shapeFile);
// Calculate mass.
$vv=$v . "*$d";
echo "Result: ".eval("return round($vv,3);")." kg<br />"; 
?>
---------------------------
Document 8.6 (bmp_info.php)

<?php
$inFile="turkey.bmp";
// Get the size of this file.
echo "File size: " . filesize($inFile)."<br />";
$in=fopen($inFile,'r');
// Read header.
$c=array();	  
for ($i=0; $i<14; $i++) {
	$c[$i]=ord(fgetc($in));
	echo $c[$i]." ";
}
echo "<br />";
// Calculate file size.
$size=$c[5]*16777216+$c[4]*65536+$c[3]*256+$c[2];
echo "File size = ".$size." bytes.<br />";
// Find offset to start of image.
$offset=$c[10];
echo "Offset to start of image = ".$offset."<br />";
// Read image information record.
for ($i=0; $i<40; $i++) {
  $c[$i]=ord(fgetc($in));
  echo $c[$i]." ";
}
echo "<br />";
// Get # of rows and columns.
$cols=$c[7]*16777216+$c[6]*65536+$c[5]*256+$c[4];
$rows=$c[11]*16777216+$c[10]*65536+$c[9]*256+$c[8];
echo "This image has ".$rows." rows and ".$cols." columns.<br />";
// Get some other information.
$nPlanes=$c[13]*256+$c[12];
echo "# of color planes = ".$nPlanes."<br />";
$bitsPerPixel=$c[15]*256+$c[14];
echo "Bits per pixel = ".$bitsPerPixel."<br />";
$compressionType=$c[19]*16777216+$c[18]*65536+$c[17]*256+$c[16];
echo "Compression type = ".$compressionType."<br />";
$imageSize=$c[23]*16777216+$c[22]*65536+$c[21]*256+$c[20];
echo "Image size = ".$imageSize."<br />";
$Xresolution=$c[27]*16777216+$c[26]*65536+$c[25]*256+$c[24];
echo "X-resolution = ".$Xresolution."<br />";
$Yresolution=$c[31]*16777216+$c[30]*65536+$c[29]*256+$c[28];
echo "Y-resolution = ".$Yresolution."<br />";
$nColors=$c[35]*16777216+$c[34]*65536+$c[33]*256+$c[32];
echo "number of colors = ".$nColors."<br />";
$importantColors=$c[39]*16777216+$c[38]*65536+$c[37]*256+$c[36];
echo "important colors = ".$importantColors."<br />";
// Close the file.
fclose($in);
?>
---------------------------
Document 8.7 (bmp_read.php)

<?php
$inFile="turkey.bmp";
echo filesize($inFile)."<br />";
$in=fopen($inFile,'r');
// Read header.
$ch=array();
for ($i=0; $i<14; $i++) {
	$ch[$i]=ord(fgetc($in));
	echo $ch[$i]." ";
}
echo "<br />";
//$offset=$ch[10];
for ($i=0; $i<40; $i++) {
  $ch[$i]=ord(fgetc($in));
  echo $ch[$i]." ";
}
echo "<br />";
$cols=$ch[5]*256+$ch[4];
$bytes=3*$cols;
// Each row is padded to contain a multiple of 4 bytes.
$nPad=4-$bytes%4;
echo "# of pad bytes = ".$nPad."<br />";
$rows=$ch[9]*256+$ch[8];
echo "rows and columns: ".$rows."  ".$cols."<br />";
// Read image.
for ($r=1; $r<=$rows; $r++) {
  for ($c=1; $c<=$cols; $c++) {
    for ($i=0; $i<=2; $i++) {
	  $ch[$i]=fgetc($in);
	  echo ord($ch[$i]);
	}
	echo "  ";
  }
  // Read pad bytes at end of line.
  for ($p=1; $p<=$nPad; $p++) {
	$pad=fgetc($in); 
	echo "pad";
  }  
  echo "<br />";
}
fclose($in);
?>
--------------------------------
Document 8.8 (bmp_grayscale.php)

<?php
$inFile="turkey.bmp";
$outFile="turkey_grayscale.bmp";
echo filesize($inFile)."<br />";
$in=fopen($inFile,'r');
$out=fopen($outFile,'w');
// Read header.
$ch=array();
for ($i=0; $i<14; $i++) {
  //$ch[$i]=ord(fgetc($in));
  //echo $ch[$i]." ";
  //fwrite($out,chr($ch[$i]),1);
  fwrite($out,fgetc($in));
}
echo "<br />";
//$offset=$ch[10];
for ($i=0; $i<40; $i++) {
  $ch[$i]=ord(fgetc($in));
  echo $ch[$i]." ";
  fwrite($out,chr($ch[$i]),1);
}
echo "<br />";
$cols=$ch[5]*256+$ch[4];
$bytes=3*$cols;
$nPad=4-$bytes%4; // Each row padded to contain a multiple of 4 bytes.
echo "# of pad bytes = ".$nPad."<br />";
$rows=$ch[9]*256+$ch[8];
echo "rows and columns: ".$rows."  ".$cols."<br />";
// Read image.
for ($r=1; $r<=$rows; $r++) {
  for ($c=1; $c<=$cols; $c++) {
    for ($i=0; $i<=2; $i++) {
	  $ch[$i]=fgetc($in);
	}
$avg=(ord($ch[0])+ord($ch[1])+ord($ch[2]))/3;
	fwrite($out,chr($avg),1); fwrite($out,chr($avg),1);
fwrite($out,chr($avg),1);
  }
  // Read pad bytes at end of line.
  for ($p=1; $p<=$nPad; $p++) {
	$pad=fgetc($in); 
	fwrite($out,$pad);
  }  
}
fclose($in);
fclose($out);
echo "A grayscale file has been created.<br />";
?>
-------------------------------
Document 8.9 (bmp_hidetext.php)

<?php
$inFile="turkey.bmp";
$outFile="turkey_text.bmp";
echo filesize($inFile)."<br />";
$in=fopen($inFile,'r');
$out=fopen($outFile,'w');
$hiddenText="Please don't eat me!";
$startRow=9;
// Read header.
$ch=array();
for ($i=0; $i<14; $i++) {
  $ch[$i]=ord(fgetc($in));
  echo $ch[$i]." ";
  // Write starting row for text here, in unused byte.
  if ($i==6) fwrite($out,chr($startRow),1); 
  else fwrite($out,chr($ch[$i]),1); 
}
echo "<br />";
//$offset=$ch[10];
for ($i=0; $i<40; $i++) {
  $ch[$i]=ord(fgetc($in));
  echo $ch[$i]." ";
  fwrite($out,chr($ch[$i]),1);
}
echo "<br />";
$cols=$ch[7]*16777216+$ch[6]*65536+$ch[5]*256+$ch[4];
$bytes=3*$cols;
$nPad=4-$bytes%4; // Each row padded to contain a multiple of 4 bytes.
echo "# of pad bytes = ".$nPad."<br />";
$rows=$ch[11]*16777216+$ch[10]*65536+$ch[9]*256+$ch[8];
echo "rows and columns: ".$rows."  ".$cols."<br />";
// Read image.
$K=strlen($hiddenText);
$knt=0;
for ($r=1; $r<=$rows; $r++) {
  for ($c=1; $c<=$cols; $c++) {
    for ($i=0; $i<=2; $i++) {
	  $ch[$i]=fgetc($in);
	}
	$avg=(ord($ch[0])+ord($ch[1])+ord($ch[2]))/3;
	fwrite($out,chr($avg),1);
	fwrite($out,chr($avg),1);
	fwrite($out,chr($avg),1);
  }
  // Read pad bytes at end of line.
  for ($p=1; $p<=$nPad; $p++) {
	$pad=fgetc($in); 
	if (($r>=$startRow) && ($knt<$K)) {
	  // Write text into pad bytes.
	  fwrite($out,substr($hiddenText,$knt,1),1);
	  $knt++;
	}
	else fwrite($out,$pad,1);
  }  
}
fclose($in);
fclose($out);
echo "A grayscale file has been created.<br />";
?>
----------------------------------
Document 8.10 (TransformImage.php)

<?php 
$imagePath="turkey.gif"; // existing image
$im=ImageCreateFromGIF($imagePath);
Header('Content-type: image/png');
ImagePNG($im); // ImagePNG($im,{filename}) saves to file.
ImageDestroy($im);
?>
------------------------------------
Document 8.11 (CompoundInterest.php)

<html >
<head>
<title>Calculate Compound Interest</title>
</head>
<body>
<h3>Calculate Compound Interest</h3>
<form action="<?php $_SERVER['PHP_SELF']?>" method="post">
Initial amount (no commas), $: <input type="text" 
    name="initial" value="10000" /><br />
Annual interest rate, %: <input type="text" name="rate"
    value="4" /><br />
How many years?: <input type="text" name="years" 
    value="20" /><br />
<input type="submit" 
    value="Generate compound interest table." />
</form>
<?php
	$initial=$_POST["initial"];
	$rate=$_POST["rate"];
	$years=$_POST["years"];
	echo $initial." ".$rate." ".$years."<br />";
	for ($i=1; $i<=$years; $i++) {
	  $amount=$initial*pow(1+$rate/100,$i);
	  echo $i." ".number_format($amount,2)."<br />";
	}
?>
</body>
</html>
-----------------------------
Document 9.1 (keyedArray.php)

<?php
// Create an array with user-specified keys...	
echo '<br />A keyed array:<br />';
$stuff = array('mine' => 'BMW', 'yours' => 'Lexus', 
  'ours' => 'house');
foreach ($stuff as $key => $val) {	 
  echo '$stuff[' . $key . '] = '. $val . '<br />';
}	  
?>
--------------------------------------
Document 9.2 (ConsecutiveKeyArray.php)

<?php
$a = array('david','apple','Xena','Sue');	
echo "Using for... loop<br />";
for ($i=0; $i<sizeof($a); $i++)
  echo $a[$i] . '<br />';
echo "Using implied keys with foreach... loop<br />";
foreach ($a as $i => $x) 
  echo 'a[' . $i . '] = ' . $x . '<br />';

echo "An array with keys starting at an integer other than 0<br />";
$negKey = array(-1 => 'BMW', 'Lexus', 'house');
for ($i=-1; $i<2; $i++)
  echo $negKey[$i] . '<br />';

echo 'A keyed array with consecutive character keys...<br />';
$stuff = array('a' => 'BMW', 'b' => 'Lexus', 'c' => 'house');
for ($i='a'; $i<='c'; $i++)
  echo $stuff[$i] . '<br />';
?>
------------------------------
Document 9.3 (base_1Array.php)

<?php
echo '<br />A keyed array with indices starting at 1...<br />';
$a = array(1 => 63.7, 77.5, 17, -3);					
foreach($a as $key => $val) {
  echo 'a[' . $key . '] = '. $val . '<br />';
}	
for ($i=1; $i<=sizeof($a); $i++)
  echo $a[$i] . '<br />'; 
?>
------------------------
Document 9.4 (two-D.php)

<?php
echo '<br />A 2-D array<br />';
$a = array(
  0 => array(1,2,3,4),
  1 => array(5,6,7,8),
  2 => array(9,10,11,12),
  3 => array(13,14,15,16),
  4 => array(17,18,19,20)
);
$n_r=count($a); echo '# rows = ' . $n_r . '<br />';
$n_c=count($a[0]); echo '# columns = ' . $n_c . '<br />';
for ($r=0; $r<$n_r; $r++) {
  for ($c=0; $c<$n_c; $c++)
    echo $a[$r][$c] . ' ';
  echo '<br />';
}
?>
------------------------
Document 9.5 (sort1.php)

<?php
// Create and sort an array
$a = array('david','apple','sue','xena');	
echo 'Original array:<br />';
for ($i=0; $i<sizeof($a); $i++)
  echo $a[$i] . '<br />';
sort($a);	  
echo 'Sorted array:<br />';
for ($i=0; $i<sizeof($a); $i++)
  echo $a[$i] . '<br />';		
?>
------------------------
Document 9.6 (sort2.php)

<?php
	$a=array(3.3,-13,-0.7,14.4);
	sort($a);
	for ($i=0; $i<sizeof($a); $i++)
	  echo $a[$i] . '<br />';
?>
------------------------
Document 9.7 (sort3.php)

<?php
function compare($x,$y) {	  
  return strcasecmp($x,$y);
}
// Create and sort an array
$a = array('Xena', 'Sue', 'david', 'apple');
echo 'Original array:<br />';
for ($i=0; $i<sizeof($a); $i++)
  echo $a[$i] . '<br />';
echo 'Sorted array with user-defined comparisons of elements:<br />';  	
usort($a,"compare");	
for ($i=0; $i<sizeof($a); $i++)
  echo $a[$i] . '<br />';		
?>
----------------------------------
Document 9.8 (StacksAndQueues.php)

<html>
<head>
<title>Stacks and Queues</title>
</head>
<body>
<?php
  $a = array(-17,"David", 33.3,"Laura");
// Treat $a like a stack (last in, first out)
  echo "The original array (element [0] is the \"oldest\" element):<br />";
  print_r($a);
// Add two elements to $a
  array_push($a,"Susan",0.5);
  echo "<br />Push two elements on top of stack:<br />";
  print_r($a);
// Remove three elements from $a
  array_pop($a); array_pop($a); array_pop($a);
  echo "<br />Remove three elements from top of stack:<br />";
  print_r($a);
// Treat $a like a queue (first in, first out)
  $a = array(-17,"David", 33.3,"Laura");
  echo "<br />Back to original array:<br />";
  print_r($a);
  echo "<br />Remove two elements from front of queue:<br />";
  array_shift($a);
  array_shift($a);
  print_r($a);
  echo "<br />Add three elements to end of queue:<br />";
  array_push($a,"Susan",0.5,"new_guy");
  print_r($a);
  echo "<br />Add a \"line crasher\" to the beginning of the queue:<br />";
  array_unshift($a,"queue_crasher_guy");
  print_r($a);
?>
</body>
</html>
----------------------------
Document 9.9a (quadrat2.htm)

<html>
<head>
<title>Solving the Quadratic Equation</title>
</head>
<body>
<form method="post" action="quadrat_2.php">
Enter coefficients for ax<sup>2</sup> + bx + c = 0:
<br />
a = <input type="text" value="1" name="coeff[0]" />
 (must not be 0)<br />
b = <input type="text" value="2" name="coeff[1]" /><br />
c = <input type="text" value="-8" name="coeff[2]" /><br />
<br /><input type="submit" value="click to get roots..." />
</form>
</body>
</html>
----------------------------
Document 9.9b (quadrat2.php)

<?php
var_dump($_POST["coeff"]);
echo "<br />";
$coefficientArray=array_keys($_POST["coeff"]);
$a = $_POST["coeff"][$coefficientArray[0]];
$b = $_POST["coeff"][$coefficientArray[1]];
$c = $_POST["coeff"][$coefficientArray[2]];
$d = $b*$b - 4.*$a*$c;
if ($d == 0) {
  $r1 = $b/(2.*$a);
  $r2 = "undefined";
}
else if ($d < 0) {
  $r1 = "undefined";
  $r2 = "undefined";
}
else {
$r1 = (-$b + sqrt($b*$b - 4.*$a*$c))/2./$a;;
$r2 = (-$b - sqrt($b*$b - 4.*$a*$c))/2./$a;;
}
echo "r1 = " . $r1 . ", r2 = " . $r2; 
?>
-----------------------------
Document 9.10a (quadrat3.htm)

<html>
<head>
<title>Solving the Quadratic Equation</title>
</head>
<body>
<form method="post" action="quadrat3.php">
Enter coefficients for ax<sup>2</sup> + bx + c = 0:
<br />
a = <input type="text" value="1" name="coeff[]" />
 (must not be 0)<br />
b = <input type="text" value="2" name="coeff[]" /><br />
c = <input type="text" value="-8" name="coeff[]" /><br />
<br /><input type="submit" value="click to get roots..." />
</form>
</body>
</html>
-----------------------------
Document 9.10b (quadrat3.php)

<?php
var_dump($_POST["coeff"]);
echo "<br />";
$coefficientArray=$_POST["coeff"];
$a = $coefficientArray[0];
$b = $coefficientArray[1];
$c = $coefficientArray[2];
$d = $b*$b - 4.*$a*$c;
if ($d == 0) {
  $r1 = $b/(2.*$a);
  $r2 = "undefined";
}
else if ($d < 0) {
  $r1 = "undefined";
  $r2 = "undefined";
}
else {
$r1 = (-$b + sqrt($b*$b - 4.*$a*$c))/2./$a;;
$r2 = (-$b - sqrt($b*$b - 4.*$a*$c))/2./$a;;
}
echo "r1 = " . $r1 . ", r2 = " . $r2; 
?>
-----------------------------
Document 9.11a (CloudObs.htm)

<html>
<head>
<title>Cloud Observations</title>
</head>
<body bgcolor="#aaddff">
<h1>Cloud Observations</h1>
<strong> Cloud Observations </strong>(Select as many cloud types as observed.)
<br />
<form method="post" action="CloudObs.php" />
<table>
  <tr> 
   <td><strong>High</strong> </td>
    <td> 
     <input type="checkbox" name="high[]" 
       value="Cirrus" /> Cirrus</td>
    <td> 
     <input type="checkbox" name="high[]"
       value="Cirrocumulus" /> Cirrocumulus </td>
    <td> 
      <input type="checkbox" name="high[]" 
       value="Cirrostratus" /> Cirrostratus </td></tr>
  <tr> 
    <td colspan="4"><hr noshade color="black" />
     </td></tr>
  <tr> 
    <td> <strong>Middle</strong> </td>
    <td> 
      <input type="checkbox" name="mid[]" 
        value="Altostratus" /> Altostratus </td>
    <td> 
      <input type="checkbox" name="mid[]" 
        value="Altocumulus" /> Altocumulus</td></tr>
  <tr> 
    <td colspan="4"><hr noshade color="black" />
     </td></tr>
  <tr> 
    <td> <strong>Low</strong></td>
    <td> 
    <input type="checkbox" name="low[]" value="Stratus" />   
      Stratus</td>
    <td> 
      <input type="checkbox" name="low[]"
        value="Stratocumulus" /> Stratocumulus</td>
    <td> 
    <input type="checkbox" name="low[]" value="Cumulus" />
      Cumulus </td></tr>
  <tr> 
    <td colspan="4"><hr noshade color="black" />
      </td></tr>
  <tr> 
    <td> <strong>Rain-Producing </strong> </td>
    <td> 
      <input type="checkbox" name="rain[]" 
        value="Nimbostratus" /> Nimbostratus</td>
    <td> 
      <input type="checkbox" name="rain[]" 
        value="Cumulonimbus" /> Cumulonimbus </td></tr>
</table>
<input type="submit" value="Click to process..." />
</form>
</body>
</html>
-----------------------------
Document 9.11b (CloudObs.php)

<?php
   $high = $_POST["high"];
   $n = count($high);
   echo "For high clouds, you observed<br />";
   for ($i=0; $i<$n; $i++)
      echo $high[$i] . "<br>";
   $mid = $_POST["mid"];
   $n = count($mid);  
   echo "For mid clouds, you observed<br />";
   for ($i=0; $i<$n; $i++)
     echo $mid[$i] . "<br />";
   $low = $_POST["low"];
   $n = count($low);  
   echo "For low clouds, you observed<br />";
   for ($i=0; $i<$n; $i++)
     echo $low[$i] . "<br />";
   $rain = $_POST["rain"];
   $n = count($rain);  
   echo "For precipitating clouds, you observed<br />";
   for ($i=0; $i<$n; $i++)
     echo $rain[$i] . "<br />";
?>
-------------------------
Document 9.12 (histo.php)

<?php
  function buildHisto($a,$lo,$hi,$n_boxes) {
    echo "building histogram...<br />";
    $h=array();
	// echo "Number of boxes = ".$n_boxes."<br />";
    for ($i=0; $i<$n_boxes; $i++) {
	  array_push($h,0);
	  // echo $h[$i]."<br />";
    }
	echo "size of histogram array = ".sizeof($h)."<br />";
	for ($n=0; $n<sizeof($a); $n++) {
	  $i=floor($a[$n]/$n_boxes);
	  if ($i==sizeof($h)) $i--; // handles value of 100
	  $h[$i]++;
	}
	$sum=0;
	for ($i=0; $i<sizeof($h); $i++) {
	  echo "h[".$i."] = ".$h[$i],"<br />";
	  $sum+=$h[$i];
	}
	echo "# of entries = ".$sum."<br />";
  }
  $in=fopen("histo.dat","r");
  $a=array();
  $i=0;
  while (!feof($in)) {
    fscanf($in,"%f", $s);
$a[$i]=$s;
$i++;
	// array_push($a,$s); will also work.
	// echo 'a['.$i.'] = '.$a[$i].'<br />';
  }
/* Alternative code
  $i=0;
  while (!feof($in)) {
    fscanf($in,"%f",$a[$i]);
    $i++;
  }
*/
  echo 'Number of scores: '.sizeof($a).'<br />';
  buildHisto($a,0,100,10);
  fclose($in);
?>
-------------------------------
Document 9.13 (cardShuffle.php)

<?php
  $deck = array();
  for ($i= 0; $i<52; $i++) {
    $deck[$i]=$i+1;
	echo $deck[$i]." ";
  }
  echo "<br  />"; 
  for ($i=0; $i<52; $i++) {
    $j=rand(0,51);	
	$save=$deck[$i];
	$deck[$i]=$deck[$j];
	$deck[$j]=$save;
  }
  for ($i=0; $i<52; $i++)
    echo $deck[$i]." ";
  echo "<br />";
  sort($deck);
  echo "Resort deck...<br />";
  for ($i=0; $i<52; $i++)
    echo $deck[$i]." ";
  echo "<br /";
?>
-----------------------------
(data file for Document 9.14)
Date       Value
01/15/2006 17.3
01/20/2006 0.55
05/17/2006 83.9
09/09/2006 9.33
11/13/2006 15
01/01/2007 74.4
02/28/2007 64.4
05/05/2007 100
06/06/2007 64.4
12/12/2007 22.54
-------------------------------
Document 9.14a (DataReport.htm)

<html>
<head>
  <title>Data Management Application</title>
</head>
<body>
<form method="post" action="DataReport.php">
Date (mm/dd/yyyy format): <input type="text" name="date" value="12/12/2007" /><br />
Value (number): <input type="text" name="x" value="17.7" /><br />
find date: <input type="radio" name="choose" value="find_date" /> &nbsp; &nbsp; &nbsp;
find value: <input type="radio" name="choose" value="find_value" /> &nbsp; &nbsp; &nbsp;
insert new report in chronological order: <input type="radio" name="choose" value="insert" /> 
 &nbsp; &nbsp; &nbsp;
view all reports: <input type="radio" name="choose" value="view_all" checked /> <br />
<input type="submit" value="Click here." /> 
</form>
</body>
</html>
-------------------------------
Document 9.14b (DataReport.php)

<?php
$new_date=$_POST["date"];
$new_value=$_POST["x"];
$choose=$_POST["choose"];
$fileName="FinalExam2009_3.txt";
echo $new_date." ".$new_value." ".$choose."<br />";
$in=fopen($fileName,"r");
$line=fgets($in); // read header
echo $line."<br />";
switch($choose) {
  case "view_all":
	while (!feof($in)) {
	  fscanf($in,"%s %f",$date,$x);
	  echo $date." ".$x."<br />";
	} 
  	break;  
  case "find_date":
    $found=false;
  	while (!feof($in) && (!$found)) {
	  fscanf($in,"%s %f",$date,$x);
	  if (strtotime($date)==strtotime($new_date)) {
	    echo $date." ".$x."<br />";
		$found=true;
	  }
	}
	if (!$found) echo "This record not found.<br />";
	break;
  case "find_value":
    $found=false;
  	while (!feof($in)) {
	  fscanf($in,"%s %f",$date,$x);
	  if ($x==$new_value) {
	    echo $date." ".$x."<br />";
		$found=true;
	  }
	}
	if (!$found) echo "This record not found.<br />";
	break;
  case "insert":
  	$temp=array(); $i=0;
	$added=false; $duplicate=false;
	// copy data into array
	while (!feof($in)) {
	  fscanf($in,"%s %f",$date,$x);
  if ((strtotime($date)==strtotime($new_date)) &&  
        ($x==$new_value)) $duplicate=true;
	  if ((strtotime($date)>strtotime($new_date)) && 
        (!$added) && (!$duplicate)) { // insert new record
	    $temp[$i][0]=$new_date; $temp[$i][1]=$new_value; 
         $added=true; $i++;
	  }
	  $temp[$i][0]=$date; $temp[$i][1]=$x; $i++;
	}
	if (strtotime($new_date)>strtotime($temp[$i-1][0])) {
	  $temp[$i][0]=$new_date; $temp[$i][1]=$new_value;
	}
	echo "new array...<br />";
	for ($i=0; $i<count($temp); $i++) {
	  echo $temp[$i][0]." ".$temp[$i][1]."<br />";
	}
  if ($duplicate) echo "Duplicate record, not added.<br />";
    break;
}
fclose($in);
?>
---------------------------------
Document 9.15 (InsertionSort.php)

<?php
function insertionSort(&$a,$first,$last) {
  if ($first<$last) {
    insertionSort($a,$first,$last-1);
	insertInOrder($a[$last],$a,$first,$last-1);
  }
  return $a;
}
function insertInOrder($element,&$a,$first,$last) {
  if($element>=$a[$last]) $a[$last+1]=$element;
  else if ($first<$last) {
    $a[$last+1]=$a[$last];
	insertInOrder($element,$a,$first,$last-1);
  }	
  else {
    $a[$last+1]=$a[$last];
    $a[$last]=$element;
  }
}  
$x=array(7,3,4,13,544,-17,-1,0);
$x=insertionSort($x,0,7);
var_dump($x);
?>
----------------------------------------
Document 10.1 (conditionalExecution.php)

<?php
  function getRoots($a,$b,$c) {
    echo "This function calculates roots...";
  }
  $i = 2;
  if ($i == 0) {
    echo "i equals 0";
  } 
  elseif ($i == 1) {
    echo "i equals 1";
  } 
  elseif ($i == 2) {
    echo "i equals 2";
  } 
  else {
    echo "i is not 0, 1, or 2";
  }
  echo "<br />";
  $discriminant=0.3;
  if ($discriminant < 0.)
    echo "There are no real roots.<br />";
  elseif ($discriminant == 0.) {
    echo "There is one real root.<br />";
    $r1 = -$b/$a/2;
    echo $r1;
  }
  else {
    echo "There are two real roots.<br />";
    list($r1,$r2) = getRoots($a,$b,$c);
    echo "<br />Print the roots here...";
  }  
?>
-------------------------------
Document 10.2 (daysInMonth.php)

<?php
$month=5;
switch ($month) {
  case 1:
  case 3:
  case 5:
  case 7:
  case 8:
  case 10:
  case 12: 
    echo "There are 31 days in this month.<br />"; break;
  case 4:
  case 6:
  case 9:
  case 11: 
    echo "There are 30 days in this month.<br />"; break;
  case 2: 
    echo "There are either 28 or 29 days in this month.
<br />"; break;
  default: 
    echo "I do not understand your month entry.";
}
?>
-----------------------------
Document 10.3 (countdown.php)

<?php
  for ($i=10; $i>=0; $i--)
    echo $i . "<br />";
  echo "FIRE!<br />";
?>
--------------------------------
Document 10.4 (loopExamples.php)

<?php
  $a = array(17,-13.3, "stringThing","PHP");
  foreach ($a as $x)
    echo "$x<br />";
  for ($i=0; $i<=sizeof($a); $i++)
    echo $a[$i] . '<br />';
  $a = array(1 => 17,2 => -13.3, 3 => "stringThing", 
     4 => "PHP");
  foreach ($a as $k => $x)
    echo "a[" . $k . "] = " . $x . "<br />";
  $b = array(77, 33, 4);
  foreach($b as $x) {
    echo("$x" . "<br />");
  }
?>
---------------------------
Document 10.5 (foreach.php)

<?php
  $a = array(17,-13.3, 
    "stringThing","PHP");
  foreach ($a as $x)
    echo "$x<br />";

  $a = array(1 => 17,2 => -13.3, 
     3 => "stringThing", 4 => "PHP");
  foreach ($a as $k => $x)
    echo "a[" . $k . "] = " . $x . "<br />";
?>
---------------------------
Document 10.6 (squares.php)

<?php
$x=0;
do {
  $x++;
  echo $x . ', ' . $x*$x . '<br />';
} while ($x*$x < 100.);	
?>
-----------------------------
(data file for Document 10.7)
01/14/2007 17:33:01
02/28/2007 09:15:00
----------------------------
Document 10.7 (dateTime.php)

<?php
  $in=fopen("dateTime.txt","r");
  while (!feof($in)) {
    fscanf($in,"%d/%d/%d %d:%d:%d",$day,$month,$year,$hour,$min,$sec);
	printf("%'02d/%'02d/%4d %'02d:%'02d:%'02d<br />",$day,$month,$year,$hour,$min,$sec);
  }
?>
---------------------------------
Document 10.8 (formatExample.php)

<?php
  $a=67;
  $b=.000717;
  $c=-67;
  $d=83.17;
  $e="Display a string.";
  printf("\t%c\n\r%e\n\r%f",$a,$b,$b); 
  // no line feeds!
  printf("<br /><br />%s<br />%e<br />%f
          <br />",$a,$b,$b);
  printf("<br />%d %u<br />",$a,$a);
  printf("<br />%d %u<br />",$c,$c); 
  // note effect of %u!
  printf("<br />He said, \"Let's go!\"<br />");
  printf("<br />Your discount is \$%'012.2f<br />",$d);
  printf("<br />%'x26s<br />",$e);
?>
--------------------------------
Document 10.9 (arrayDisplay.php)

<?php
$a = array("VW", 17.3, "GM", 44, "BMW");
print_r($cars);
$result = print_r($cars,true);
printf("<br />%s",$result);
vprintf("<br />%s, %f, %s, %u, %s",$a);
$result = sprintf("<br />%s, %f, %s, %um %s",
   $a[0],$a[1],$a[2],$a[3],$a[4]);
echo '<br />' . $result;
?>
------------------------------------
Document 10.10 (stringFunctions.php)

<?php /* Created on: 6/13/2009 */ ?>
<html>
<body>
<?php
   $str1="Hello, world!";	
   $str2="world";
   echo stristr($str1,'w') . "<br />";
   echo substr_compare($str1,$str2,8) . "<br />"; 
   echo strpos($str1,"wor") . "<br />";   
   echo stristr($str1,"wor") . "<br />";
?>
</body>
</html>
-----------------------------
Document 10.11 (arrayPop.php)

<?php
$stack = array("orange", "banana", "apple", "lemon");
$fruit=array_pop($stack);
print_r($stack);
?>
------------------------------
Document 10.12 (arrayPush.php)

<?php
$stack = array("red", "grn");
$n = array_push($stack, "blu", "wh");
print_r($stack);
$stack[] = "blk";
printf("<br />%u<br />",$n);
print_r($stack);
printf("<br />%u<br />",sizeof($stack));
?>
-------------------------------
Document 10.13 (arrayShift.php)

<?php
$queue = array("orange", "banana", "raspberry", "mango");
print_r($queue);
$rottenFruit = array_shift($queue);
echo '<br />' . $rottenFruit;
echo '<br />' . count($queue);
?>
---------------------------------
Document 10.14 (arrayUnshift.php)

<?php
$a = array("orange", "banana", "raspberry", "mango");
print_r($a);
array_unshift($a,"papaya","mangosteen");
echo '<br />' . count($a) . '<br />';
print_r($a);
?>
-------------------------------------
Document 10.15 (SortingFunctions.php)

<?php
function compare($a,$b) {
  return $b-$a;
}
$x=array(7,3,4,13,544,-17,-1,0);
sort($x);
echo "normal sort<br />";
var_dump($x);
sort($x,SORT_STRING);
echo "<br />sort as string<br />";
var_dump($x);
usort($x,"compare");
echo "<br />normal sort, descending order<br />";
var_dump($x);
$a=array('mine' => 'BMW','hers' => 'Lexus',
  'ours' => 'House');
sort($a);
echo "<br />sort keyed array<br />";
var_dump($a);
echo "<br />but the keys haven't been retained:<br />";
foreach($a as $key => $val)
  echo '$a[' . $key . '] = '. $val . '<br />';
$a=array('mine' => 'BMW','hers' => 'Lexus',
  'ours' => 'House');
asort($a);
echo "use asort() to retain the keys<br />";
foreach($a as $key => $val)
  echo '$a[' . $key . '] = '. $val . '<br />';
arsort($a);  
echo 
  "use ursort() to sort keyed array in reverse order<br />";
foreach($a as $key => $val)
  echo '$a[' . $key . '] = '. $val . '<br />';
ksort($a);
echo 
  "use ksort() to sort by key rather than by value<br />";  
foreach($a as $key => $val)
  echo '$a[' . $key . '] = '. $val . '<br />';
krsort($a);
echo 
  "use krsort() to sort array in reverse order by key<br />";
foreach($a as $key => $val)
  echo '$a[' . $key . '] = '. $val . '<br />';
?>
------------------------------
<data file for Document 10.16)
Site Lat Lon
brooks 40.01 -75.99
europe 50.5 5.3
south -30 88
farsouth -79 -167
--------------------------------
Document 10.16(ExplodeArray.php)

<?php
  $a=file("LatLon.dat");
  var_dump($a);
  echo "<br />";
  for ($i=1; $i<sizeof($a); $i++) {
    list($s,$la,$lo)=explode(" ",$a[$i]);
	echo $s.", ".$la.", ".$lo."<br />";
  }
	
  foreach ($a as $s) {
    list($site,$Lat,$Lon)=explode(" ",$s);
	echo $site.", ".$Lat.", ".$Lon."<br />";
  }
?>
----------------------------
Document 10.17 (varDump.php)

<?php
$a = array('david','apple','Xena','Sue');
$b = array();	
list($b[0],$b[1],$b[2],$b[3]) = $a;
var_dump($b);
?>	
------------------------------
Document 10.18 (arrayList.php)

<?php
$stuff = array('I','love','PHP.');
list($who,$do_what,$to_what) = $stuff;
echo "$who $do_what $to_what" . "<br />";
list($who, , $to_what) = $stuff;
echo "$who $to_what<br />";
$a = array('david','apple','Xena','Sue');
$b = array();	
list($b[0],$b[1],$b[2],$b[3]) = $a;
var_dump($b);
echo "<br />Access with for... loop.<br />";
for ($i=0; $i<count($b); $i++) echo $b[$i] . "<br />";
echo "Access with foreach... loop.<br />";
foreach ($b as $key => $x) echo "a[" . $key . "] = " . $x . "<br />";
?>
--------
CLI 11.1

C:\PHP>php hello.php
Hello, world!
C:\PHP>
--------
CLI 11.2

C:\PHP>php pdf_1.php .5 .5
Array
(
	[0] => pdf_1.php
	[1] => -.5
	[2] => .5
)
-------------------------
Document 11.1 (pdf_1.php)

<?php
  $a = $_SERVER['argv'];
  print_r($a);
  $x1=$a[1]; $x2=$a[2];
  $n=200;
  $sum=0; $dx=($x2-$x1)/$n;
  for ($i=1; $i<=$n; $i++) {
    $x=$x1+($i-1)*$dx;
    $y1=exp(-$x*$x/2)/sqrt(2.*M_PI);
    $x=$x1+$i*$dx;
    $y2=exp(-$x*$x/2)/sqrt(2.*M_PI);
    $sum+=$y1+$y2;
  }
  echo "\n" . $sum*$dx/2.;
?>
--------
CLI 11.3

C:\PHP>php pdf_1.php .5 .5
Array
(
	[0] => pdf_1.php
	[1] => -.5
	[2] => .5
)

0.38292418907776
C:\PHP>
-------------------------
Document 11.2 (pdf_2.php)

<?php
  echo "\nGive lower and upper limits for evaluating pdf,\nseparated by a space: ";
  fscanf(STDIN,"%f %f",$x1,$x2);
  echo $x1 . ", " . $x2;
  $n=200;
  $sum=0; $dx=($x2-$x1)/$n;
  for ($i=1; $i<=$n; $i++) {
    $x=$x1+($i-1)*$dx;
    $y1=exp(-$x*$x/2)/sqrt(2.*M_PI);
    $x=$x1+$i*$dx;
    $y2=exp(-$x*$x/2)/sqrt(2.*M_PI);
    $sum+=$y1+$y2;
  }
  echo "\n" . $sum*$dx/2.;
?>
--------
CLI 11.4

C:\PHP>php pdf_2.php

Give lower and upper limits for evaluating pdf,
separated by a space: -3 3
-3, 3
0.99729820978444
--------------------------
Document 11.3a (pdf_3.htm)

<html>
<head>
<title>Integrate the normal probability density function</title>
</head>
<body>
<h3>Evaluate the normal probability density function</h3>
<form method="post" action="pdf_3.php">
x1: <input type="text" name="x1" value="-0.5" /><br />
x2: <input type="text" name="x2" value=".5"   /><br />
<input type="submit" value="Click to evaluate." />
</form>
</body>
</html>
--------------------------
Document 11.3b (pdf_3.php)

<?php
  if ($_SERVER['argc'] > 0) {
    $a = $_SERVER['argv'];
    print_r($a);
    $x1=$a[1]; $x2=$a[2];
  }
  else {
    $x1=$_POST['x1'];
	$x2=$_POST['x2'];
	echo $x1 . ", " . $x2 . "<br />";
  }
  $n=200;
  $sum=0; $dx=($x2-$x1)/$n;
  for ($i=1; $i<=$n; $i++) {
    $x=$x1+($i-1)*$dx;
	$y1=exp(-$x*$x/2)/sqrt(2.*M_PI);
	$x=$x1+$i*$dx;
	$y2=exp(-$x*$x/2)/sqrt(2.*M_PI);
	$sum+=$y1+$y2;
  }
  echo $sum*$dx/2.;
?>
------------------------------
Document 12.1 (ImageSpace.php)

<?php
Header ("Content-type: image/gif");
// define title
$TitleString = "Graphics Display Space";
// dimensions of plotting space
$x_max = 800; $y_max = 200;
// define font size (1-5, smallest to largest)
$font_size=5;
// starting point for title
$x_title = 10; $y_title = 30.;
// create image space
$im = ImageCreate($x_max,$y_max) or 
  die ("Cannot Initialize new GD image stream");
// define colors -- first call fills background
$background_color = ImageColorAllocate($im, 234, 234, 234); 
// define text color
$black = ImageColorAllocate($im,0,0,0);
// display text
ImageString($im,$font_size,0,0,"(0,0)",$black);
ImageString($im,$font_size,720,180,"(800,500)",$black);
ImageString($im,$font_size,$x_title,$y_title,
$TitleString,$black);
// display image
ImageGIF($im);
// release resources
ImageDestroy($im);   
?>
--------------------------
Document 12.2a ( pie1.php)

<?php
Header ("Content-type: image/gif");
// define title
$TitleString = "Pie Chart";
// dimensions of plotting space
$x_max = 800; $y_max = 500;
// starting point for title
$x_title = 10; $y_title = 30.;
// create image space
$im = ImageCreate($x_max,$y_max) or 
 	die ("Cannot Initialize new GD image stream");
// define colors -- first call fills background
$background_color = ImageColorAllocate($im, 234, 234, 234); 
// define text color
$black = ImageColorAllocate($im,0,0,0);
// display text
ImageString($im,5,$x_title,$y_title,$TitleString,$black);
// display image
ImageGIF($im);
// release resources
ImageDestroy($im);   
?>
-------------------------
Document 12.2b (pie2.php)

<?php
Header ("Content-type: image/gif");
// define title
$TitleString = "Pie Chart";
// default data for testing, up to 12 values
$A = array(60,50,40,100,50,50,75,5,10,15,20,35);
$legends = array("Item1","Item2","Item3","Item4","Item5","Item6",
  "Item7","Item8","Item9","Item10","Item11","Item12");
// upper left-hand corner of legend space
$x0_legend = 400; $y0_legend = 75;
// size of legend color boxes
$legend_size = 25;
// vertical space between legend color boxes;
$dy_legend=30;
// dimensions of plotting space
$x_max = 800; $y_max = 500;
// create image space
$im = ImageCreate($x_max,$y_max) or die ("Cannot Initialize new GD image stream");
// define colors -- first call fills background
$background_color = ImageColorAllocate($im, 234, 234, 234); 
// define text color
$black = ImageColorAllocate($im,0,0,0);
// define pie slice colors
$ColorCode = array("255,0,0","51,0,255","51,255,51","255,153,0",
  "0,204,153","204,255,102",
  "255,102,102","102,204,255","204,153,255","255,51,153",
  "204,0,255","255,255,51");
$PieColor=array();
for ($i=0; $i<12; $i++) {
  $ColorCodeSplit = explode(',',$ColorCode[$i]);
  $PieColor[$i] =
ImageColorAllocate($im,$ColorCodeSplit[0],
  $ColorCodeSplit[1],$ColorCodeSplit[2]);
}
// starting point for title
$x_title = 10; $y_title = 30.;
// display text
ImageString($im,5,$x_title,$y_title,$TitleString,$black);
// Display legend
$n = count($A);
for ($i=0; $i<$n; $i++) {
  ImageFilledRectangle($im,$x0_legend,
    $y0_legend+$dy_legend*$i,$x0_legend+$legend_size,
    $y0_legend+$dy_legend*$i+$legend_size,$PieColor[$i]);  
  ImageString($im,5,$x0_legend+$legend_size+5,
    $y0_legend+$dy_legend*$i+5,$legends[$i],$black);
}
// display image
ImageGIF($im);
// release resources
ImageDestroy($im);   
?>
-------------------------
Document 12.2c (pie3.php)

<?php
Header ("Content-type: image/gif");
$TitleString = "Pie Chart";
$A=array(60,50,40,100,50,50,75,5,10,15,20,35);
$legends = array("Item1","Item2","Item3","Item4","Item5",
"Item6","Item7","Item8","Item9","Item10","Item11","Item12");
// dimensions of plotting space
$x_max = 800; $y_max=500;
// center point for pie chart
$x0 = 200; $y0 = 250;
// diameter of pie
$dia = 360;
// starting point for title
$x_title = 40; $y_title = 40.;
// upper left-hand corner of legend space
$x0_legend = 400; $y0_legend = 75;
// size of legend color boxes
$legend_size = 25;
// vertical space between legend color boxes;
$dy_legend = 30;
// create image space
$im = ImageCreate($x_max,$y_max) or die ("Cannot Initialize new GD image stream");
// define colors
$background_color = ImageColorAllocate($im, 234, 234, 234); // first call fills background
$black=ImageColorAllocate($im,0,0,0);
// pie section colors for up to $n_max sections
$ColorCode = array("255,0,0","51,0,255","51,255,51","255,153,0","0,204,153","204,255,102","255,102,102","102,204,255","204,153,255","255,51,153","204,0,255","255,255,51");
$n_max = count($ColorCode);
$PieColor = array();
for ($i=0; $i<$n_max; $i++) {
  $ColorCodeSplit = explode(',',$ColorCode[$i]);
  $PieColor[$i] = ImageColorAllocate($im,
  $ColorCodeSplit[0],$ColorCodeSplit[1],$ColorCodeSplit[2]);
}
// Convert data array into angles, total of 360 deg.
$sum = array_sum($A);
$n = count($A);
$start = array();
$end = array();
$start[0] = 0;
for ($i=0; $i<$n; $i++) {
  $slice = $A[$i]/$sum*360;
  if ($i>0) $start[$i] = $end[$i-1];
  $end[$i] = $start[$i] + $slice;
}
// Display title
ImageString($im,5,$x_title,$y_title,$TitleString,$black);
// draw filled arcs
for ($i=0; $i<$n; $i++) {
  ImageFilledArc($im,$x0,$y0,$dia,$dia,$start[$i],$end[$i],
    $PieColor[$i],IMG_ARC_PIE);
}
// Display legend
for ($i=0; $i<$n; $i++) {
  ImageFilledRectangle($im,$x0_legend,
    $y0_legend+$dy_legend*$i,$x0_legend+$legend_size,
    $y0_legend+$dy_legend*$i+$legend_size,$PieColor[$i]);  
  ImageString($im,5,$x0_legend+$legend_size+5,
    $y0_legend+$dy_legend*$i+5,$legends[$i],$black);
}
// Display and release allocated resources.
ImageGIF($im);
ImageDestroy($im); 
?>  
------------------------------
(data file for Document 12.3a)
Quarterly Sales
17.7 	January-March
15	April-June
19.2	July-September
30	October-December

---------------------------------
Document 12.3a (pieChartTest.htm)

<html>
<head>
<title></title>
</head>
<body>
<form method="post" action="pieChart.php" />
  <input type="text" value="pieChart.dat" name="fileName" 
   /><br />
  <input type="submit" 
   value=
  "Click here to generate pie chart from specified file." />
</form>
</body>
</html>
-----------------------------
Document 12.3b (pieChart.php)

<?php
function generatePie($TitleString,$A,$legends) {
Header ("Content-type: image/gif");
//$TitleString="Pie Chart";
//$A=array(60,50,40,100,50,50,75,5,10,15,20,35);
//$legends=array("Item1","Item2","Item3","Item4","Item5",
//"Item6","Item7","Item8","Item9","Item10","Item11",
//"Item12");
// dimensions of plotting space
$x_max=800; $y_max=500;
// center point for pie chart
$x0=200; $y0=250;
// diameter of pie
$dia=360;
// starting point for title
$x_title=40; $y_title=40.;
// upper left-hand corner of legend space
$x0_legend=400; $y0_legend=75;
// size of legend color boxes
$legend_size=25;
// vertical space between legend color boxes;
$dy_legend=30;
// create image space
$im = ImageCreate($x_max,$y_max) or 
  die ("Cannot Initialize new GD image stream");
// define colors
$background_color = ImageColorAllocate($im, 234, 234, 234); // first call fills background
$black=ImageColorAllocate($im,0,0,0);
// pie section colors for up to 12 sections
$ColorCode = array("255,0,0","51,0,255","51,255,51","255,153,0",
  "0,204,153","204,255,102","255,102,102","102,204,255",
  "204,153,255","255,51,153","204,0,255","255,255,51");
$PieColor=array();
for ($i=0; $i<12; $i++) {
  $ColorCodeSplit = explode(',',$ColorCode[$i]);
  $PieColor[$i] = ImageColorAllocate($im, $ColorCodeSplit[0],$ColorCodeSplit[1],$ColorCodeSplit[2]);
}
// Convert data array into angles, total of 360 deg.
$sum=array_sum($A);
$n=count($A);
$start=array();
$end=array();
$start[0]=0;
for ($i=0; $i<$n; $i++) {
  $slice=$A[$i]/$sum*360;
  if ($i>0) $start[$i]=$end[$i-1];
  $end[$i]=$start[$i]+$slice;
}
// Display title
ImageString($im,5,$x_title,$y_title,$TitleString,$black);
// draw filled arcs
for ($i=0; $i<$n; $i++) {
  ImageFilledArc($im,$x0,$y0,$dia,$dia,$start[$i],$end[$i],
    $PieColor[$i],IMG_ARC_PIE);
}
// Display legend
for ($i=0; $i<$n; $i++) {
  ImageFilledRectangle($im,$x0_legend,
    $y0_legend+$dy_legend*$i,$x0_legend+$legend_size,
    $y0_legend+$dy_legend*$i+$legend_size,$PieColor[$i]);
  ImageString($im,5,$x0_legend+$legend_size+5,
    $y0_legend+$dy_legend*$i+5,$legends[$i],$black);
}
// Display and release allocated resources.
ImageGIF($im);
ImageDestroy($im);   
}
// MAIN PROGRAM -----------------------
$inFile=$_POST["fileName"];
//$inFile="pieChart.dat";
$in=fopen($inFile,"r") or exit("Can't open this file.");
$A=array();
$legends=array();  
// rtrim() removes line feeds, etc. from end of string
$Title=rtrim(fgets($in)); 
$i=-1;
while (!feof($in)) {
  $i++;
  fscanf($in,"%f %s",$A[$i],$legends[$i]);
  $A[$i]=round($A[$i],0);
}
fclose($in);
generatePie($Title,$A,$legends);
?>
--------------------------
Document 12.4a (Hbar1.php)

<?php
Header ("Content-type: image/gif");
// define default data
$chartTitle = "Monthly temperature deviations from climatological average";
// Assumes equal +/- values --> odd number of values
$x_labels = array("-2.5","-2.0","-1.5","-1.0","-0.5",
  "0.0","+0.5","+1.0","+1.5","+2.0","+2.5");
$y_labels = array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep",
      "Oct","Nov","Dec");
$x_values = array(1.3,0.9,-0.2,-0.5,1,0,-2.5,
                  -.5,.6,.2,.7,2.3);
$n_x = count($x_labels); // number of x labels 
$n_y = count($y_labels); // number of y labels
$xvalue_max = 2.5;
// define image space
$x_max = 800;
$y_max = 500;
// starting coordinates for title
$x0_title = 10; $y0_title = 10;
// space between x-axis labels
$dx = 50;
// space between horizontal bars
$dy = 30;
// y-tic size
$y_tic = 10;
// bar height
$bar_height = 25;
// label offsets
$xlabel_offset = 40; $ylabel_offset = 80;
$xaxis_xoffset = 0; $xaxis_yoffset = 20;
$x0 = 100; $y0 = 60; // starting coordinates for of x-axis
// create and color image space background
$im = imageCreate ($x_max, $y_max) or die ("Cannot Initialize new GD image stream");
$background_color = ImageColorAllocate($im,234,234,234);
// define colors
$text_color = ImageColorAllocate($im,0,0,0); // text color
$line_color = ImageColorAllocate($im,0,0,0); // line color
$title_font_size = 5; // large font for title
// black text for title
$title_color = ImageColorAllocate($im,0,0,0); 
// draw chart title
ImageString($im,$title_font_size,$x0_title,$y0_title,
  $chartTitle,$title_color);
// draw x-axis
ImageLine($im,$x0,$y0,$x0+$dx*($n_x-1),$y0,$line_color);
// draw x labels
for ($i=0; $i<$n_x; $i++) {
 ImageString($im,$title_font_size,$x0+$xaxis_xoffset+$i*$dx,
  $y0-$xaxis_yoffset,$x_labels[$i],$text_color);
 ImageLine($im,$x0+$xaxis_xoffset+$i*$dx,$y0,
  $x0+$xaxis_xoffset+$i*$dx,$y0+$y_tic,$line_color);
}
// draw y labels
for ($i=0; $i<$n_y; $i++) {
  ImageString($im,$title_font_size,$xlabel_offset,
    $ylabel_offset+$i*$dy,$y_labels[$i],$text_color);
}
// Create GIF image.
ImageGIF($im);
//  Release allocated resources. 
ImageDestroy($im);   
?>
--------------------------
Document 12.4b (Hbar2.php)

<?php
Header ("Content-type: image/gif");
// define default data
$chartTitle = "Monthly temperature deviations from average";
// Assumes equal +/- values --> odd number of values
$x_labels = array("-2.5","-2.0","-1.5","-1.0",
  "-0.5","0","+0.5","+1.0","+1.5","+2.0","+2.5");
$y_labels = array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep",
      "Oct","Nov","Dec");
$x_values = array(1.3,0.9,-0.2,-2.5,1,0,-1.1,
                 -.5,.6,2.2,.7,.3);
$n_x = count($x_labels); // number of x labels 
$n_y = count($y_labels); // number of y labels
$xvalue_max = 2.5;
// define image space
$x_max = 800;
$y_max = 500;
// starting coordinates for title
$x0_title = 10; $y0_title = 10;
// space between x-axis labels
$dx = 50;
// space between horizontal bars
$dy = 30;
// y-tic size
$y_tic = $dy*($n_y+1);
// bar height
$bar_height = 25;
// label offsets
$xlabel_offset = 40; $ylabel_offset = 80;
$xaxis_xoffset = 0; $xaxis_yoffset = 20;
$x0 = 100; $y0 = 60; // starting coordinates for of x-axis
// location of vertical 0-axis
$x0axis_offset = $x0 + $dx*floor($n_x/2);
$x_length = 100;
// create and color image space background
$im = imageCreate ($x_max, $y_max) or die ("Cannot Initialize new GD image stream");
$background_color = ImageColorAllocate($im,234,234,234);
// define colors
// text color
$text_color = ImageColorAllocate($im,0,0,0); 
// line color 
$line_color = ImageColorAllocate($im,0,0,0); 
// negative bar color
$negative = ImageColorAllocate($im,0,0,255); 
// positive bar color
$positive = ImageColorAllocate($im,255,0,0); 
// large font for title 
$title_font_size = 5; 
// black text for title
$title_color = ImageColorAllocate($im,0,0,0); 
// draw chart title
ImageString($im,$title_font_size,$x0_title,$y0_title,
  $chartTitle,$title_color);
// draw x-axis
ImageLine($im,$x0,$y0,$x0+$dx*($n_x-1),$y0,$line_color);
// draw x labels
for ($i=0; $i<$n_x; $i++) {
 ImageString($im,$title_font_size,$x0+$xaxis_xoffset+$i*$dx,
   $y0-$xaxis_yoffset,$x_labels[$i],$text_color);
 ImageLine($im,$x0+$xaxis_xoffset+$i*$dx,$y0,
   $x0+$xaxis_xoffset+$i*$dx,$y0+$y_tic,$line_color);
}
// draw y labels and bars
for ($i=0; $i<$n_y; $i++) {
  $x_scaled = 
ImageString($im,$title_font_size,$xlabel_offset,
  $ylabel_offset+$i*$dy,$y_labels[$i],$text_color);
}
// draw bars
for ($i=0; $i<$n_y; $i++) {
  $x_scaled = $x_values[$i]/$xvalue_max*$dx*floor($n_x/2);
  $color = $negative;
  if ($x_scaled >= 0) $color = $positive;
  ImageFilledRectangle($im,$x0axis_offset,
   $ylabel_offset+$i*$dy,$x0axis_offset+$x_scaled,
   $ylabel_offset+$i*$dy+$bar_height,$color);
}
// draw vertical 0-axis
ImageLine($im,$x0axis_offset,$y0,$x0axis_offset,
  $y0+$dy*$n_y+$dy,$line_color);
// draw line across bottom
ImageLine($im,$x0,$y0+$dy*$n_y+$dy,$x0+$dx*($n_x-1),
  $y0+$dy*$n_y+$dy,$line_color);
// Create GIF image.
ImageGIF($im);
//  Release allocated resources. 
ImageDestroy($im);   
?>
----------------------------------
Document 12.5a (HbarChartTest.htm)

<html>
<head>
<title></title>
</head>
<body>
<h3>Create a horizontal bar chart</h3>
<form method="post" action="Hbarchart.php">	Chart title (text): <input size="50" type="text" name="Title" value="Monthly ranges" /><br />	
  Data arrays (values separated by one space):<br />
  Minimum values: <input size="50" type="text" 
  name="A_min" value="0.1 6 3 5.3 9.9 8.7 0.5 2 3 4 5 6" 
   /><br />
  Maximum values: <input size="50" type="text" 
   name="A_max" 
   value="1.5 2 7.8 4.5 6.4 7 9 10 3.3 0.5 4.4 3.3" /><br />
  Minimum data value: <input size="4" name="min" value="0" 
   /><br />
  Maximum data value: <input size="4" name="max" value="10"
   /><br />
  X-axis labels (string values separate by one space):  
  <input size="50" type="text" name="X"
   value="0 1 2 3 4 5 6 7 8 9 10" /><br />
  Bar labels (string values separated by one space): 
  <input size="80" type="text" name="Y" 
    value="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
   /><br />
  Bar height (pixels): <input type="text" name="BarHeight"
   value="25" size="3" /><br />
  Vertical space between bars (pixels): 
 <input type="text" name="dy" value="30" size="3" /><br />
 <input type="submit" 
  value="Click here to generate horizontal bar chart." />
<br />
  <input type="reset" 
Value = 
"Click here to reset all fields to their original values." /><br />
</form>
</body>
</html>
------------------------------
Document 12.5b (HbarChart.php)

<?php
function HorizontalBarChart($Title,$A_min,$A_max,$X,$Y,
                           $bar_height,$dy) {
Header ("Content-type: image/gif");
// define default data
$chartTitle = $Title;
// Allows +/- values
$xmin_values = explode(' ',$A_min);
$xmax_values = explode(' ',$A_max);
$x_labels = explode(' ',$X);
$y_labels = explode(' ',$Y);
$n_x = count($x_labels); // number of x labels 
$n_y = count($y_labels); // number of y labels		 
$xvalue_max=$_POST["max"];   
$xvalue_min=$_POST["min"];
// define image space
$x_max = 800;
$y_max = 500;
// starting coordinates for title
$x0_title = 10; $y0_title = 10;
// space between x-axis labels
$dx = 50;
// space between horizontal bars
// y-tic size
$y_tic=$dy*($n_y+1);
// label offsets
$xlabel_offset = 40; $ylabel_offset = 80;
$xaxis_xoffset = 0; $xaxis_yoffset = 20;
$x0 = 100; $y0 = 60; // starting coordinates for x-axis
// location of vertical 0-axis
$x0axis_offset = $x0 + $dx*floor($n_x/2);
$x_length = 100;
// create and color image space background
$im = imageCreate ($x_max, $y_max) or die ("Cannot Initialize new GD image stream");
$background_color = ImageColorAllocate($im,234,234,234);
// define colors
$text_color = ImageColorAllocate($im,0,0,0); // text color
$line_color = ImageColorAllocate($im,0,0,0); // line color
$negative = ImageColorAllocate($im,0,0,255); // negative bar color
$positive = ImageColorAllocate($im,255,0,0); // positive bar color
$title_font_size = 5; // large font for title
// black text for title
$title_color = ImageColorAllocate($im,0,0,0); 
// draw chart title
ImageString($im,$title_font_size,$x0_title,$y0_title,
  $chartTitle,$title_color);
// draw x-axis
ImageLine($im,$x0,$y0,$x0+$dx*($n_x-1),$y0,$line_color);
// draw x labels
for ($i=0; $i<$n_x; $i++) {
 ImageString($im,$title_font_size,$x0+$xaxis_xoffset+$i*$dx,
  $y0-$xaxis_yoffset,$x_labels[$i],$text_color);
 ImageLine($im,$x0+$xaxis_xoffset+$i*$dx,$y0,
  $x0+$xaxis_xoffset+$i*$dx,$y0+$y_tic,$line_color);
}
// draw y labels and bars
for ($i=0; $i<$n_y; $i++) {
 ImageString($im,$title_font_size,$xlabel_offset,
  $ylabel_offset+$i*$dy,$y_labels[$i],$text_color);
}
// draw bars	
for ($i=0; $i<$n_y; $i++) {
 $xmin_scaled = ($xmin_values[$i] - $xvalue_min)/
    ($xvalue_max - $xvalue_min)*($n_x-1)*$dx; 
   $xmax_scaled = ($xmax_values[$i] -  
   $xvalue_min)/($xvalue_max - $xvalue_min)*($n_x-1)*$dx;
   $color = $negative;
  if ($xmin_values[$i] >= 0) $color = $positive;   
  ImageFilledRectangle($im,$x0 + $xmin_scaled,
    $ylabel_offset + $i*$dy,$x0 + $xmax_scaled,
    $ylabel_offset + $i*$dy + $bar_height,$color);
} 
// draw vertical 0-axis
// ImageLine($im,$x0axis_offset,$y0,$x0axis_offset,$y0+$dy*$n_y+$dy,$line_color);
// draw line across bottom
ImageLine($im,$x0,$y0+$dy*$n_y+$dy,
  $x0+$dx*($n_x-1),$y0+$dy*$n_y+$dy,$line_color);
// Create GIF image.
//ImageGIF($im,"HbarChart.gif");
ImageGIF($im);
//  Release  allocated  ressources. 
ImageDestroy($im);   
}
// MAIN PROGRAM ------------------------------------
$Title = $_POST["Title"];
$A_min = $_POST["A_min"];
$A_max = $_POST["A_max"];
$X = $_POST["X"];
$Y = $_POST["Y"];
$BarHeight = $_POST["BarHeight"];
$dy = $_POST["dy"];
HorizontalBarChart($Title,$A_min,$A_max,$X,$Y,$BarHeight,
  $dy);
?>
--------------------------
Document 12.6a (Vbar1.php)

<?php
Header ("Content-type: image/gif");
// define default data
$y_labels=array("0","10","20","30","40","50","60","70","80","90","100");
$x_labels=array("0","1","2","3","4","5","6","7","8","9",
"10","11","12","13","14","15","16","17","18","19","20",
"21","22","23","24","25","26","27","28","29","30","31");
$n_x=31; // number of x-axis labels
$n_y=11; // number of y-axis labels
// define image space
$x_max = 800;
$y_max = 300;
// define graphing space in terms of intervals
$x0 = 80; $y0 = 250; // lower left-hand corner of graphing space
$x_gap = 20; $y_gap = 20; // interval between axis labels
// create and color image space background
$im = imageCreate ($x_max, $y_max) or die ("Cannot Initialize new GD image stream");
$background_color = ImageColorAllocate($im,234,234,234);
$title_font_size = 5; // large font for title
$title_color = ImageColorAllocate($im,0,0,0); // black text for title
$chartTitle = "Bar Graph";
$x0_title = 10; $y0_title = 10; // starting coordinates for title
ImageString($im,$title_font_size,$x0_title,$y0_title,$chartTitle,$title_color);
// draw graphing space
// black lines for graphing space borders
$line_color=ImageColorAllocate($im,0,0,0); 
// medium line width for graphing space borders
ImageSetThickness($im,2); 
// draw x-axis
ImageLine($im,$x0,$y0,$x0+$n_x*$x_gap,$y0,$line_color); ImageLine($im,$x0,$y0-($n_y-1)*$y_gap,$x0+$n_x*$x_gap,$y0-($n_y-1)*$y_gap,$line_color); //top border
// y-axis
ImageLine($im,$x0,$y0,$x0,$y0-($n_y-1)*$y_gap,$line_color); // right border ImageLine($im,$x0+$n_x*$x_gap,$y0,$x0+$n_x*$x_gap,
  $y0-($n_y-1)*$y_gap,$line_color); 
// Create GIF image.
ImageGIF($im);
//  Release  allocated  resources. 
ImageDestroy($im);   
?>
--------------------------
Document 12.6b (Vbar2.php)

<?php
Header ("Content-type: image/gif");
// define default data
$chartTitle = "Bar Graph";
$x_title = "X-axis"; $y_title = "Y-axis";
$y_labels=array("0","10","20","30","40","50","60","70","80","90","100");
$x_labels=array("0","1","2","3","4","5","6","7","8","9",
"10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31");
$n_x=31; // number of x-axis labels
$n_y=11; // number of y-axis labels
$ylabel_mask="    "; // Mask for right-justifying y-labels
$ylabel_length=4; // Maximum length of y-axis label
// define image space
$x_max = 800;
$y_max = 300;
// define graphing space in terms of intervals
$x0 = 80; $y0 = 250; // lower left-hand corner of graphing space
$x_gap = 20; $y_gap = 20; // interval between axis labels
// label and title offsets from x-y origin  _  
// trial-and-error
$xlabel_xoffset=-15;
$xlabel_yoffset=10;
$ylabel_xoffset=-28;
$ylabel_yoffset=-8;
$xtitle_xoffset=260;
$xtitle_yoffset=25;
$ytitle_xoffset=-45;
$ytitle_yoffset=-75;
// create and color image space background
$im = imageCreate ($x_max, $y_max) or 
  die ("Cannot Initialize new GD image stream");
$background_color = ImageColorAllocate($im,234,234,234);
$title_font_size = 5; // large font for title
// black text for title
$title_color = ImageColorAllocate($im,0,0,0); 
// starting coordinates for title
$x0_title = 10; $y0_title = 10; // draw graphing space 
ImageString($im,$title_font_size,$x0_title,$y0_title,
  $chartTitle,$title_color);
// black lines for graphing space borders
$line_color=ImageColorAllocate($im,0,0,0); // medium line width for graphing space borders
ImageSetThickness($im,2); 
// draw x-axis
ImageLine($im,$x0,$y0,$x0+$n_x*$x_gap,$y0,$line_color); ImageLine($im,$x0,$y0-($n_y-1)*$y_gap,$x0+$n_x*$x_gap,
//top border 
$y0-($n_y-1)*$y_gap,$line_color); 
// y-axis
ImageLine($im,$x0,$y0,$x0,$y0-($n_y-1)*$y_gap,$line_color); // right border 
ImageLine($im,$x0+$n_x*$x_gap,$y0,$x0+$n_x*$x_gap,
  $y0-($n_y-1)*$y_gap,$line_color); 
// draw x-axis labels and vertical grid
// black lines for grids
$grid_color = ImageColorAllocate($im,0,0,0); $label_color = // black text for labels
ImageColorAllocate($im,0,0,0); ImageSetThickness($im,1);
for ($i=0; $i<=$n_x; $i++) {
  $x=$x0+$xlabel_xoffset+$i*$x_gap;
  $y=$y0+$xlabel_yoffset;
  if ($i>0)
  ImageString($im,2,$x,$y,$x_labels[$i],$label_color);
  ImageLine($im,$x0+$i*$y_gap,$y0,$x0+$i*$y_gap,
    $y0-($n_y-1)*$y_gap,$grid_color);
}
// draw y-axis labels and horizontal grid
$black=ImageColorAllocate($im,0,0,0);
// define a black and transparent dashed line for grid lines 
$style = array(
  $black,$black,$black,$black,$black, 
  IMG_COLOR_TRANSPARENT,IMG_COLOR_TRANSPARENT,
  IMG_COLOR_TRANSPARENT,IMG_COLOR_TRANSPARENT,
  IMG_COLOR_TRANSPARENT
);
ImageSetStyle($im,$style);
for ($i=0; $i<$n_y; $i++) {
  // next two lines right-justify the y-axis labels
  $ylabel=$ylabel_mask . $y_labels[$i];
  $ylabel=substr($ylabel,strlen($ylabel)-$ylabel_length);
  $x=$x0+$ylabel_xoffset;
  $y=$y0+$ylabel_yoffset-$i*$y_gap;
  ImageString($im,2,$x,$y,$ylabel,$label_color);
  ImageLine($im,$x0,$y0-$i*$x_gap,$x0+$n_x*$x_gap,
            $y0-$i*$y_gap,IMG_COLOR_STYLED);
}
// Draw axis titles.
ImageString($im,$title_font_size,$x0+$xtitle_xoffset,
  $y0+$xtitle_yoffset,$x_title,$label_color);
ImageStringUp($im,$title_font_size,$x0+$ytitle_xoffset,
  $y0+$ytitle_yoffset,$y_title,$label_color);
// Create GIF image.
ImageGIF($im);
//  Release allocated resources. 
ImageDestroy($im);   
?>
--------------------------
Document 12.6c (Vbar3.php)

<?php
Header ("Content-type: image/gif");
// define default data
$chartTitle = "Bar Graph";
$x_title = "X-axis"; $y_title = "Y-axis";
$y_labels=array("0","10","20","30","40","50","60","70","80","90","100");
$x_labels=array("0","1","2","3","4","5","6","7","8","9",
"10","11","12","13","14","15","16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31");
$Y=array(23,10,15,100,0,55,60,16,23,33,44,77,88,91,5,85,80,66,49,33,22,25,83,90,44,25,11,9,77,20,15);
$n_x=31; // number of x-axis labels
$n_y=11; // number of y-axis labels
$ylabel_mask="    "; // Mask for right-justifying y-labels
$ylabel_length=4; // Maximum length of y-axis label
// define image space
$x_max = 800;
$y_max = 300;
// define graphing space in terms of intervals
$x0 = 80; $y0 = 250; // lower left-hand corner of graphing space
$x_gap = 20; $y_gap = 20; // interval between axis labels
$bar_width=10;
$bar_offset=5;
// label and title offsets from x-y origin  _  
// trial-and-error
$xlabel_xoffset=-15;
$xlabel_yoffset=10;
$ylabel_xoffset=-28;
$ylabel_yoffset=-8;
$xtitle_xoffset=260;
$xtitle_yoffset=25;
$ytitle_xoffset=-45;
$ytitle_yoffset=-75;
// create and color image space background
$im = imageCreate ($x_max, $y_max) or 
  die ("Cannot Initialize new GD image stream");
$background_color = ImageColorAllocate($im,234,234,234);
$bar_color=ImageColorAllocate($im,255,0,0);
// large font for title
$title_font_size = 5; 
// black text for title
$title_color = ImageColorAllocate($im,0,0,0); 
// starting coordinates for title
$x0_title = 10; $y0_title = 10; // draw graphing space 
ImageString($im,$title_font_size,$x0_title,$y0_title,
  $chartTitle,$title_color);
// black lines for graphing space borders
// medium line width for graphing space borders
$line_color=ImageColorAllocate($im,0,0,0); 
ImageSetThickness($im,2); 
// draw x-axis
ImageLine($im,$x0,$y0,$x0+$n_x*$x_gap,$y0,$line_color); 
ImageLine($im,$x0,$y0-($n_y-1)*$y_gap,$x0+$n_x*$x_gap,
//top border 
$y0-($n_y-1)*$y_gap,$line_color); 
// y-axis
// right border
ImageLine($im,$x0,$y0,$x0,$y0-($n_y-1)*$y_gap,$line_color);
ImageLine($im,$x0+$n_x*$x_gap,$y0,$x0+$n_x*$x_gap,
  $y0-($n_y-1)*$y_gap,$line_color); 
// draw x-axis labels and vertical grid
// black lines for grids
$grid_color = ImageColorAllocate($im,0,0,0); 
$label_color = // black text for labels
ImageColorAllocate($im,0,0,0); ImageSetThickness($im,1);
for ($i=0; $i<=$n_x; $i++) {
  $x=$x0+$xlabel_xoffset+$i*$x_gap;
  $y=$y0+$xlabel_yoffset;
  if ($i>0)
  ImageString($im,2,$x,$y,$x_labels[$i],$label_color);
  ImageLine($im,$x0+$i*$x_gap,$y0,$x0+$i*$x_gap,
    $y0-($n_y-1)*$y_gap,$grid_color);
}
// draw y-axis labels and horizontal grid
$black=ImageColorAllocate($im,0,0,0);
// define a black and transparent dashed line for grid lines 
$style = array(
  $black,$black,$black,$black,$black, 
  IMG_COLOR_TRANSPARENT,IMG_COLOR_TRANSPARENT,
  IMG_COLOR_TRANSPARENT,IMG_COLOR_TRANSPARENT,
  IMG_COLOR_TRANSPARENT
);
ImageSetStyle($im,$style);
for ($i=0; $i<$n_y; $i++) {
  // next two lines right-justify the y-axis labels
  $ylabel=$ylabel_mask . $y_labels[$i];
  $ylabel=substr($ylabel,strlen($ylabel)-$ylabel_length);
  $x=$x0+$ylabel_xoffset;
  $y=$y0+$ylabel_yoffset-$i*$y_gap;
  ImageString($im,2,$x,$y,$ylabel,$label_color);
  ImageLine($im,$x0,$y0-$i*$y_gap,$x0+$n_x*$x_gap,
            $y0-$i*$y_gap,IMG_COLOR_STYLED);
}
// Draw axis titles.
ImageString($im,$title_font_size,$x0+$xtitle_xoffset,
  $y0+$xtitle_yoffset,$x_title,$label_color);
ImageStringUp($im,$title_font_size,$x0+$ytitle_xoffset,
  $y0+$ytitle_yoffset,$y_title,$label_color);
// Draw bars.
$y_range=$y_gap*($n_y-1);
$ymin=0; $ymax=100;
for ($i=0; $i<$n_x; $i++) {
  ImageFilledRectangle($im,$x0+$bar_offset+$x_gap*$i,$y0,$x0+$bar_offset+$bar_width+$x_gap*$i,
    $y0-($Y[$i]-$ymin)/($ymax-$ymin)*$y_range,$bar_color);
}
// Create GIF image.
ImageGIF($im);
//  Release allocated resources. 
ImageDestroy($im);   
?>
----------------------------------
Document 12.7a (VbarChartTest.htm)

<html>
<head>
  <title>Vbar chart interface</title>
</head>
<body>
<h3>Create vertical bar chart</h3>
<form method="post" action="VbarChart.php" >
Data file name: <input type="text" name="FileName" value="VbarChart.dat" /><br />
Chart title: <input type="text" name="title" size="50" value="Daily Rainfall (mm)" /><br />
X-axis title: <input type="text" name="xTitle" value="day, June 2009" /><br />
Y-axis title: <input type="text" name="yTitle" value="rainfall, mm" /><br />
Number of bars: <input type="text" name="nBars" value="31" /><br />
Minimum value: <input type="text" name="min" value="0" 
/><br />
Maximum value: <input type="text" name="max" value="100" /><br />
Y-axis labels: <input type="text" name="yLabels" size="50" value="0,10,20,30,40,50,60,70,80,90,100" /><br />
X-label space (pixels) <input type="text" name="xWidth" value="20"/><br />
Bar width (% of x-label space): <input type="text" name="barWidth" value="50" /><br />
Bar color (RRR,GGG,BBB): <input type="text" name="barColor" value="255,0,0" /><br /> 
Offset for x-axis title (pixels): <input type="text" value="250" name="xTitleOffset" /><br />
Offset for y-axis title (pixels): <input type="text" value="10" name="yTitleOffset" /><br />
<input type="submit" value="Click to draw chart..." /><br />
<input type="reset" value="Click to reset..." ><br />
</form>
</body>
</html>
------------------------------
Document 12.7b (VbarChart.php)

<?php
function drawChart($chartTitle,$barColor,$bar_width,$x_gap,$n_x,
$x_title,$y_title,$xtitle_xoffset,$ytitle_yoffset,$yMin,
$yMax,$x_labels,$y_labels,$Y_lo,$Y_hi){
Header ("Content-type: image/gif");
$n_y=count($y_labels); // number of y-axis labels
$ylabel_mask="    "; // Mask for right-justifying y-labels
$ylabel_length=4; // Maximum length of y-axis label
// define image space
$x_max = 800;
$y_max = 300;
// define graphing space in terms of intervals
$x0=80; $y0=250; // lower left-hand corner of graphing space
$y_range=200; // fixed y-axis graphing space
$y_gap=$y_range/($n_y-1); // interval between y-axis labels
// define bar width as a % of total space for the bar --
$bar_width=$x_gap*$bar_width/100;
$bar_offset=($x_gap-$bar_width)/2;
// label and title offsets from x-y origin  _  
// trial-and-error
$xlabel_xoffset=5;
$xlabel_yoffset=10;
$ylabel_xoffset=-28;
$ylabel_yoffset=-8;
//$xtitle_xoffset=260;
$xtitle_yoffset=25;
$ytitle_xoffset=-45;
$ytitle_yoffset=-$ytitle_yoffset;
// create and color image space background
$im = imageCreate ($x_max, $y_max) or 
  die ("Cannot Initialize new GD image stream");
$background_color = ImageColorAllocate($im,234,234,234);
$colorArray=array();
$colorArray=explode(",",$barColor);
//$bar_color=ImageColorAllocate($im,255,0,0);
$bar_color=ImageColorAllocate($im,$colorArray[0],
  $colorArray[1],$colorArray[2]);
// large font for title
$title_font_size = 5; 
// black text for title
$title_color = ImageColorAllocate($im,0,0,0); 
// starting coordinates for title
$x0_title = 10; $y0_title = 10; // draw graphing space 
ImageString($im,$title_font_size,$x0_title,$y0_title,
  $chartTitle,$title_color);
// black lines for graphing space borders
// medium line width for graphing space borders
$line_color=ImageColorAllocate($im,0,0,0); 
ImageSetThickness($im,2); 
// draw x-axis
ImageLine($im,$x0,$y0,$x0+$n_x*$x_gap,$y0,$line_color); 
ImageLine($im,$x0,$y0-($n_y-1)*$y_gap,$x0+$n_x*$x_gap,
//top border 
$y0-($n_y-1)*$y_gap,$line_color); 
// y-axis
// right border
ImageLine($im,$x0,$y0,$x0,$y0-($n_y-1)*$y_gap,$line_color);
ImageLine($im,$x0+$n_x*$x_gap,$y0,$x0+$n_x*$x_gap,
  $y0-($n_y-1)*$y_gap,$line_color); 
// draw x-axis labels and vertical grid
// black lines for grids
$grid_color = ImageColorAllocate($im,0,0,0); 
$label_color = // black text for labels
ImageColorAllocate($im,0,0,0); ImageSetThickness($im,1);
// draw x-labels
for ($i=0; $i<=$n_x; $i++) {
  $x=$x0+$xlabel_xoffset+$i*$x_gap;
  $y=$y0+$xlabel_yoffset;
  ImageString($im,2,$x,$y,$x_labels[$i],$label_color);
  ImageLine($im,$x0+$i*$x_gap,$y0,$x0+$i*$x_gap,
    $y0-($n_y-1)*$y_gap,$grid_color);
}
// draw y-axis labels and horizontal grid
$black=ImageColorAllocate($im,0,0,0);
// define a black and transparent dashed line for grid lines 
$style = array(
  $black,$black,$black,$black,$black, 
  IMG_COLOR_TRANSPARENT,IMG_COLOR_TRANSPARENT,
  IMG_COLOR_TRANSPARENT,IMG_COLOR_TRANSPARENT,
  IMG_COLOR_TRANSPARENT
);
ImageSetStyle($im,$style);
for ($i=0; $i<$n_y; $i++) {
  // next two lines right-justify the y-axis labels
  $ylabel=$ylabel_mask . $y_labels[$i];
  $ylabel=substr($ylabel,strlen($ylabel)-$ylabel_length);
  $x=$x0+$ylabel_xoffset;
  $y=$y0+$ylabel_yoffset-$i*$y_gap;
  ImageString($im,2,$x,$y,$ylabel,$label_color);
  ImageLine($im,$x0,$y0-$i*$y_gap,$x0+$n_x*$x_gap,
            $y0-$i*$y_gap,IMG_COLOR_STYLED);
}
// Draw axis titles.
ImageString($im,$title_font_size,$x0+$xtitle_xoffset,
  $y0+$xtitle_yoffset,$x_title,$label_color);
ImageStringUp($im,$title_font_size,$x0+$ytitle_xoffset,
  $y0+$ytitle_yoffset,$y_title,$label_color);
// Draw bars.
$y_span=$yMax-$yMin;
for ($i=0; $i<$n_x; $i++) {
  ImageFilledRectangle($im,$x0+$bar_offset+$x_gap*$i,
   $y0-  ($Y_lo[$i]-$yMin)/$y_span*$y_range,
   $x0+$bar_offset+$bar_width+$x_gap*$i,
   $y0-($Y_hi[$i]-$yMin)/$y_span*$y_range,$bar_color);
}
// Create GIF image.
ImageGIF($im);
//  Release allocated resources. 
ImageDestroy($im);  
}
//--------- MAIN PROGRAM ----------------   
$FileName=$_POST["FileName"];
//echo $FileName."<br />";
$title=$_POST["title"];
$xTitle=$_POST["xTitle"];
$yTitle=$_POST["yTitle"];
$nBars=$_POST["nBars"];
$yMin=$_POST["min"];
$yMax=$_POST["max"];
$xWidth=$_POST["xWidth"];
$yLabels=explode(",",$_POST["yLabels"]);
$barWidth=$_POST["barWidth"];
$barColor=$_POST["barColor"];
$xTitleOffset=$_POST["xTitleOffset"];
$yTitleOffset=$_POST["yTitleOffset"];
$in=fopen($FileName,"r");
$xLabels=array(); $Y_lo=array(); $Y_hi=array();
$i=0;
while (!feof($in)) {
  fscanf($in,"%s %f %f",$xLabels[$i],$Y_lo[$i],$Y_hi[$i]);
//echo $xLabels[$i]."  ".$Y_lo[$i]."  ".$Y_hi[$i]."<br />";
  $i++;
}
fclose($in);
drawChart($title,$barColor,$barWidth,$xWidth,$nBars,
  $xTitle,$yTitle,$xTitleOffset,$yTitleOffset,$yMin,
  $yMax,$xLabels,$yLabels,$Y_lo,$Y_hi);
?>
----------------------------------
(data file for vertical bar chart)
CO 10 15
O3_1 15 30
O3_8 0 0
SO2 50 100
NO2 2 25
PM_10 50 70
PM_2.5 80 95
--------------------------
Document 12.8a (line1.php)

<?php
Header ("Content-type: image/gif");	  
$title = "Line graph";
// Define axis labels and titles.
$y_labels=array("0","200","400","600","800","1000","1200","1400");
$x_labels=array("1","2","3","4","5","6","7","8","9");
// Number of axis labels.
$n_x=count($x_labels);
$n_y=count($y_labels);
$x_title="X-axis title";
$y_title="Y-axis title";
$ylabel_mask="    "; // Mask for right-justifying y-labels
$ylabel_length=4; // Maximum length of y-axis label
$x_gap=100; // The gap between each label on y-axis
$y_gap=50; // The gap between each label on x-axis
$x_max=900; // Maximum width of the graphing space
$y_max=450; // Maximum height of the graphing space
$x0=50; // x-axis origin
$y0=400; // y-axis origin
// Label and title offsets from x-y origin.
$xlabel_xoffset=-5;
$xlabel_yoffset=10;
$ylabel_xoffset=-28;
$ylabel_yoffset=-8;
$xtitle_xoffset=150;
$xtitle_yoffset=25;
$ytitle_xoffset=-45;
$ytitle_yoffset=-50;
// Create and color image space background.
$im = imageCreate ($x_max, $y_max) or die ("Cannot Initialize new GD image stream");
$background_color = ImageColorAllocate ($im, 234, 234, 234);
// Define colors.
$text_color = ImageColorAllocate ($im, 233, 14, 91);
$graph_color = ImageColorAllocate ($im,25,25,25);
$line_color=ImageColorAllocate($im,0,0,0);
$black = ImageColorAllocate($im,0,0,0);
// Define font size for titles, and style for dashed lines.
$title_font_size=3;
$style = Array(
  $black,$black,$black,$black,$black, 
  IMG_COLOR_TRANSPARENT,IMG_COLOR_TRANSPARENT,
  IMG_COLOR_TRANSPARENT,IMG_COLOR_TRANSPARENT,
  IMG_COLOR_TRANSPARENT
);
imagesetstyle($im, $style);
// Set thickness (pixels) for drawing lines.
ImageSetThickness($im,3);
// Draw x- and y- axes, top and right border.
ImageLine($im,$x0,$y0,$x0+($n_x-1)*$x_gap,$y0,$line_color); // x-axis
//ImageLine($im,$x0,$y0-($n_y-1)*$y_gap,$x0+($n_x-1)*$x_gap,$y0-($n_y-1)*$y_gap,IMG_COLOR_STYLED); //top border
ImageLine($im,$x0,$y0-($n_y-1)*$y_gap,$x0+($n_x-1)*$x_gap,$y0-($n_y-1)*$y_gap,$line_color); //top border
ImageLine($im,$x0,$y0,$x0,$y0-($n_y-1)*$y_gap,$line_color); // y-axis
ImageLine($im,$x0+($n_x-1)*$x_gap,$y0,$x0+($n_x-1)*$x_gap,$y0-($n_y-1)*$y_gap,$line_color); // right border
// Draw x-axis labels and vertical grid.
ImageSetThickness($im,1);
for ($i=0; $i<$n_x; $i++) {
  $x=$x0+$xlabel_xoffset+$i*$x_gap;
  $y=$y0+$xlabel_yoffset;
  ImageString($im,2,$x,$y,$x_labels[$i],$graph_color);
  ImageLine($im,$x0+$i*$x_gap,$y0,$x0+$i*$x_gap,
    $y0-($n_y-1)*$y_gap,$line_color);
}
// Draw y-axis labels and horizontal grid.
for ($i=0; $i<$n_y; $i++) {
	// Next two lines right-justify the y-axis labels.
  $ylabel=$ylabel_mask . $y_labels[$i];
  $ylabel=substr($ylabel,strlen($ylabel)-$ylabel_length);
  $x=$x0+$ylabel_xoffset;
  $y=$y0+$ylabel_yoffset-$i*$y_gap;
  ImageString($im,2,$x,$y,$ylabel,$graph_color);
  ImageLine($im,$x0,$y0-$i*$y_gap,$x0+($n_x-1)*$x_gap,
    $y0-$i*$y_gap,$line_color);
}
// Draw titles.		  
ImageString($im,$title_font_size,20,20,$title,$line_color);
ImageString($im,$title_font_size,$x0+$xtitle_xoffset,
  $y0+$xtitle_yoffset,$x_title,$graph_color);
ImageStringUp($im,$title_font_size,$x0+$ytitle_xoffset,
  $y0+$ytitle_yoffset,$y_title,$graph_color);
// Create GIF image.
ImageGIF ($im);
//  Release  allocated  resources. 
ImageDestroy($im);   
?>
----------------------------------
(partial data file for Document 12.8)
DRB Worcester, PA							
40.178	-75.3325						
4030							
mon	day	yr	hr	min	sec	EST		V
6	1	2008	0	0	0	1		0.00031
6	1	2008	0	1	0	1.000694444	0.00031
6	1	2008	0	2	0	1.001388889	0.00031
6	1	2008	0	3	0	1.002083333	0.00031
6	1	2008	0	4	0	1.002777778	0.00031
6	1	2008	0	5	0	1.003472222	0.00031
6	1	2008	0	6	0	1.004166667	0.00031
------------------------
Document 12.8b line2.php

<?php
Header ("Content-type: image/gif");	  
$fileName = "pyranometerData/SummerInsolation.txt";
$title = "Insolation, W/m^2";
// Define axis labels and titles.
$y_labels = array("0","200","400","600","800","1000","1200",
"1400","1600");
$x_labels = array("1","2","3","4","5","6","7","8","9");
// Number of axis labels.
$n_x = count($x_labels);
$n_y = count($y_labels);
$x_title = "Days, EST";
$y_title = "Insolation, W/m^2";
$ylabel_mask = "    "; // Mask for right-justifying y-labels
$ylabel_length = 4; // Maximum length of y-axis label
$x_gap = 100; // The gap between each label on y-axis
$y_gap = 50; // The gap between each label on x-axis
$x_max=900; // Maximum width of the graph or horizontal axis
$y_max=500; // Maximum hight of the graph or vertical axis
$x0 = 50; // x-axis origin
$y0 = 450; // y-axis origin
// Label and title offsets from x-y origin.
$xlabel_xoffset = -5;
$xlabel_yoffset = 10;
$ylabel_xoffset = -28;
$ylabel_yoffset = -8;
$xtitle_xoffset = 375;
$xtitle_yoffset = 25;
$ytitle_xoffset = -45;
$ytitle_yoffset = -120;
// Create and color image space background.
$im = imageCreate ($x_max, $y_max) or die ("Cannot Initialize new GD image stream");
$background_color = ImageColorAllocate ($im, 234, 234, 234);
// Define colors.
$text_color = ImageColorAllocate ($im, 233, 14, 91);
$graph_color = ImageColorAllocate ($im,25,25,25);
$line_color=ImageColorAllocate($im,0,0,0);
$black = ImageColorAllocate($im,0,0,0);
// Define font size for titles, and style for dashed lines.
$title_font_size=3;
$style = Array(
  $black,$black,$black,$black,$black, 
  IMG_COLOR_TRANSPARENT,IMG_COLOR_TRANSPARENT,
  IMG_COLOR_TRANSPARENT, 
  IMG_COLOR_TRANSPARENT,IMG_COLOR_TRANSPARENT
);
imagesetstyle($im, $style);
// Set thickness (pixels) for drawing lines.
ImageSetThickness($im,3);
// Draw x- and y- axes, top and right border.
// x-axis
ImageLine($im,$x0,$y0,$x0+($n_x-1)*$x_gap,$y0,$line_color); //top border
ImageLine($im,$x0,$y0-($n_y-1)*$y_gap,
  $x0+($n_x-1)*$x_gap,$y0-($n_y-1)*$y_gap,$line_color); 
// y-axis
ImageLine($im,$x0,$y0,$x0,$y0-($n_y-1)*$y_gap,$line_color); // right border 
ImageLine($im,$x0+($n_x-1)*$x_gap,$y0,$x0+($n_x-1)*$x_gap,
  $y0-($n_y-1)*$y_gap,$line_color); 
// Draw x-axis labels and vertical grid.
ImageSetThickness($im,1);
for ($i=0; $i<$n_x; $i++) {
  $x=$x0+$xlabel_xoffset+$i*$x_gap;
  $y=$y0+$xlabel_yoffset;
  ImageString($im,2,$x,$y,$x_labels[$i],$graph_color);
  ImageLine($im,$x0+$i*$x_gap,$y0,$x0+$i*$x_gap,
    $y0-($n_y-1)*$y_gap,$line_color);
}
// Draw y-axis labels and horizontal grid.
for ($i=0; $i<$n_y; $i++) {
  // Next two lines right-justify the y-axis labels.
  $ylabel=$ylabel_mask . $y_labels[$i];
  $ylabel=substr($ylabel,strlen($ylabel)-$ylabel_length);
  $x=$x0+$ylabel_xoffset;
  $y=$y0+$ylabel_yoffset-$i*$y_gap;
  ImageString($im,2,$x,$y,$ylabel,$graph_color);
  ImageLine($im,$x0,$y0-$i*$y_gap,$x0+($n_x-1)*$x_gap,
    $y0-$i*$y_gap,$line_color);
}
// Draw titles.		  
ImageString($im,$title_font_size,20,20,$title,$line_color);
ImageString($im,$title_font_size,$x0+$xtitle_xoffset,
  $y0+$xtitle_yoffset,$x_title,$graph_color);
ImageStringUp($im,$title_font_size,$x0+$ytitle_xoffset,
  $y0+$ytitle_yoffset,$y_title,$graph_color);	
// Get data from file.
$in = fopen($fileName,"r");
$title = fgets($in);
fscanf($in,"%f %f",$Lat,$Lon);
fscanf($in,"%f",$Cal1);
$labels = fgets($in);					
$i = -1;
while (!feof($in)) {
  $i++;
  fscanf($in,"%f %f %f %f %f %f %f %f",
  $m,$d,$y,$hr,$min,$sec,$EST[$i],$P1[$i]);
} 
fclose($in); 
// Plot data.	
$red = ImageColorAllocate($im,255,0,0);	
$yvalue_max = 1600;	// W/m^2, y min = 0   
$nDays = 8;
$day = 1;
$n_x=count($EST);
$x1 = $x0; $y1 = $y0;	
for ($i=0; $i<$n_x; $i++) {	   
  $x = $x0 + ($EST[$i] - $day)/$nDays*($nDays*$x_gap);	 
  $y = $y0 - ($P1[$i]*$Cal1)/$yvalue_max*($n_y*$y_gap);
  ImageLine($im,$x1,$y1,$x,$y,$red);
  $x1 = $x; $y1 = $y;
} 
// Create GIF image.
ImageGIF ($im);
//  Release  allocated  resources. 
ImageDestroy($im);   
?>
---------------------------------- 
Document 12.9a (lineGraphTest.htm)

<html>
<head>
  <title>Line Graph</title>
</head>
<body>
<h3>Draw a line graph</h3>
<form method="post" action="lineGraph.php">
Chart title: <input type="text" name="title" 
  value="Insolation" /><br />
X-axis title: <input type="text" name="Xaxis_title"
  value="Days, EST" /><br />
Y-axis title: <input type="text" name="Yaxis_title"
  value="Insolation, W/m2" /><br />
X<sub>min</sub>: <input type="text" name="x_min" value="1"
  /><br />
X<sub>max</sub>: <input type="text" name="x_max" value="32"
  /><br />
Y<sub>min</sub>: <input type="text" name="y_min" value="0"
  /><br />
Y<sub>max</sub>: <input type="text" name="y_max" 
  value= "1400" /><br />
X-axis labels: <input type="text" name="X_labels" size="80" 
  value="1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32"/><br />
Y-axis labels: <input type="text" name="Y_labels" size="40" 
  value="0 200 400 600 800 1000 1200 1400"/><br />
X-title offset (pixels): <input type="text"
  name="xTitleOffset" value="300" /><br />
Y-title offset (pixels): <input type="text"
  name="yTitleOffset" value="150" /><br />
Name of file containing X-Y values: 
<input type="text" name="fileName", size="50"
  value="c:/wamp/www/PyranometerData/June2008data.txt" />
<br />
<input type="submit" value="Click here to graph data." />
</form>
</body>
</html>
-----------------------------------
(small test file for Document 12.9)
0.582	0.003	8.2398
0.681	0.003	7.0438
0.746	0.003	6.2972
0.799	0.003	5.6974
0.849	0.003	5.1569
0.896	0.003	4.7228
0.931	0.003	4.4054
0.962	0.003	4.1332
1.006	0.003	3.7906
1.037	0.003	3.5338
1.068	0.003	3.2936
1.091	0.003	3.0758
1.121	0.003	2.8257
1.158	0.003	2.6090
1.176	0.003	2.4870
1.203	0.003	2.3003
1.223	0.003	2.1956
1.24	0.003	2.1043
1.25	0.003	1.9784
1.272	0.003	1.8467

------------------------------
Document 12.9b (lineGraph.php)

<?php
function lineGraph($title,$x_labels,$y_labels,$x_title,
  $y_title,$xtitle_xoffset,$ytitle_yoffset,$xmin,$xmax,
  $ymin,$ymax,$X,$Y) {
Header ("Content-type: image/gif");	  
// Define axis labels and titles.
// Number of axis labels.
  $n_x=count($x_labels);
  $n_y=count($y_labels);
//$x_title="X-axis title";
//$y_title="Y-axis title";
  $ylabel_mask="    "; // Mask for right-justifying y-labels
  $ylabel_length=4; // Maximum length of y-axis label
  $xaxis_length=700;
  $yaxis_length=350;
//$x_gap=100; // The gap between each label on y-axis
//$y_gap=50; // The gap between each label on x-axis
  $x_gap=$xaxis_length/($n_x-1);
  $y_gap=$yaxis_length/($n_y-1);
  $x_max=800; // Maximum width of the graphing space
  $y_max=450; // Maximum hight of the graphing space
  $x0=50; // x-axis origin
  $y0=400; // y-axis origin
// Label and title offsets from x-y origin.
  $xlabel_xoffset=-5;
  $xlabel_yoffset=10;
  $ylabel_xoffset=-28;
  $ylabel_yoffset=-8;
  $xtitle_yoffset=25;
  $ytitle_xoffset=-45;
  $ytitle_yoffset=-$ytitle_yoffset;
  $title_yoffset=380;
// Create and color image space background.
  $im=imageCreate ($x_max, $y_max) or 
        die ("Cannot Initialize new GD image stream");
  $background_color=ImageColorAllocate ($im, 234, 234, 234);
// Define colors.
  $text_color=ImageColorAllocate($im, 233, 14, 91);
  $graph_color=ImageColorAllocate($im,25,25,25);
  $data_color=ImageColorAllocate($im,255,0,0);
  $line_color=ImageColorAllocate($im,0,0,0);
  $black=ImageColorAllocate($im,0,0,0);
// Define font size for titles, and style for dashed lines.
  $title_font_size=3;
  $style=Array(
    $black,$black,$black,$black,$black, 
    IMG_COLOR_TRANSPARENT,IMG_COLOR_TRANSPARENT,
    IMG_COLOR_TRANSPARENT,IMG_COLOR_TRANSPARENT,
    IMG_COLOR_TRANSPARENT
  );
  Imagesetstyle($im, $style);
// Set thickness (pixels) for drawing lines.
  ImageSetThickness($im,3);
// Draw x- and y- axes, top and right border.
  ImageLine($im,$x0,$y0,$x0+
    ($n_x-1)*$x_gap,$y0,$line_color); // x-axis
  ImageLine($im,$x0,$y0-($n_y-1)*$y_gap,
    $x0+($n_x-1)*$x_gap,$y0-($n_y-1)*$y_gap,$line_color); //top border
  ImageLine($im,$x0,$y0,$x0,
    $y0-($n_y-1)*$y_gap,$line_color); // y-axis
  ImageLine($im,$x0+($n_x-1)*$x_gap,$y0,
    $x0+($n_x-1)*$x_gap,$y0-($n_y-1)*$y_gap,$line_color); 
// right border
// Draw x-axis labels and vertical grid.
  ImageSetThickness($im,1);
  for ($i=0; $i<$n_x; $i++) {
    $x=$x0+$xlabel_xoffset+$i*$x_gap;
    $y=$y0+$xlabel_yoffset;
    ImageString($im,2,$x,$y,$x_labels[$i],$graph_color);
    ImageLine($im,$x0+$i*$x_gap,$y0,$x0+$i*$x_gap,
      $y0-($n_y-1)*$y_gap,$line_color);
  }
// Draw y-axis labels and horizontal grid.
  for ($i=0; $i<$n_y; $i++) {
    // Next two lines right-justify the y-axis labels.
    $ylabel=$ylabel_mask . $y_labels[$i];
    $ylabel=substr($ylabel,strlen($ylabel)-$ylabel_length);
    $x=$x0+$ylabel_xoffset;
    $y=$y0+$ylabel_yoffset-$i*$y_gap;
    ImageString($im,2,$x,$y,$ylabel,$graph_color);
    ImageLine($im,$x0,$y0-$i*$y_gap,$x0+($n_x-1)*$x_gap,
      $y0-$i*$y_gap,$line_color);
  }
// Draw titles.		  
  ImageString($im,$title_font_size,20,$y0-$title_yoffset,$title,$line_color); ImageString($im,$title_font_size,$x0+$xtitle_xoffset,
    $y0+$xtitle_yoffset,$x_title,$graph_color);
ImageStringUp($im,$title_font_size,$x0+$ytitle_xoffset,
    $y0+$ytitle_yoffset,$y_title,$graph_color);
// Scale and plot data.
  $n=count($X);
  $x1=($X[0]-$xmin)/($xmax-$xmin)*$xaxis_length; 
  $y1=($Y[0]-$ymin)/($ymax-$ymin)*$yaxis_length;
  for ($i=1; $i<$n; $i++) {
    $x_plot=($X[$i]-$xmin)/($xmax-$xmin)*$xaxis_length;
    $y_plot=($Y[$i]-$ymin)/($ymax-$ymin)*$yaxis_length;
    ImageLine($im,$x0+$x1,$y0-$y1,$x0+$x_plot,
      $y0-$y_plot,$data_color);
    $x1=$x_plot; $y1=$y_plot;
  }
// Create GIF image.
  ImageGIF ($im);
//  Release  allocated  resources.
  ImageDestroy($im);  
} 
// MAIN PROGRAM --------------------------------------------
$S=$_POST["X_labels"];
$X_labels=explode(' ',$S);
$S=$_POST["Y_labels"];
$Y_labels=explode(' ',$S);
$title=$_POST["title"];
$Xaxis_title=$_POST["Xaxis_title"];
$Yaxis_title=$_POST["Yaxis_title"];
$xmin=$_POST["x_min"]; $xmax=$_POST["x_max"];
$ymin=$_POST["y_min"]; $ymax=$_POST["y_max"];
$xTitleOffset=$_POST["xTitleOffset"];
$yTitleOffset=$_POST["yTitleOffset"];
// Read X-Y values from file.
$fileName=$_POST["fileName"];
$in=fopen($fileName,"r");
$X=array(); $Y=array(); $i=-1;
while (!feof($in)) {
  $i++;
  fscanf($in,"%f %f",$X[$i],$Y[$i]);
}
fclose($in);
lineGraph($title,$X_labels,$Y_labels,$Xaxis_title,
  $Yaxis_title,$xTitleOffset,$yTitleOffset,$xmin,$xmax,
  $ymin,$ymax,$X,$Y);
?>

