4
This chapter provides an introduction to setting up a PHP environment for solving computational problems. An example introduces PHP’s ability to read external files.
The solution to this problem is to use two documents—an HTML document into which a user enters radon values and a PHP application that processes those values.
Document 4.1a (radonTable1.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>
<formmethod="post"action="radonTable1.php">
<tableborder>
<trbgcolor="silver">
<th>Location</th><th>Value, pCi/L</th>
</tr>
<tr>
<td><inputtype="text"name="site1_name"
value="basement" /></td><td><inputtype="text"size="10"
name="site1"/></td>
<tr>
<td><inputtype="text"name="site2_name"
value="1st floor" /></td><td><inputtype="text"size="10"
name="site2"/></td>
<tr>
<td><inputtype="text"name="site3_name"
value="2nd floor" /></td><td><inputtype="text"size="10"
name="site3"/></td>
<tr>
<td><inputtype="text"name="site4_name"
value="master_bedroom" /></td><td><input type="text"size="10"name="site4"/></td>
</table>
<inputtype="submit"value="Click to generate table…" />
</form>
</body>
</html>
66 4 Creating a PHP Environment
Document 4.1b (radonTable1.php)
<?php
// Transfer the values.
$site1=$_POST["site1"];
$site2=$_POST["site2"];
$site3=$_POST["site3"];
$site4=$_POST["site4"];
$site1_name=$_POST["site1_name"];
$site2_name=$_POST["site2_name"];
$site3_name=$_POST["site3_name"];
$site4_name=$_POST["site4_name"];
// Select colors for a color-coded table.
$bg1=pickColor($site1);
$bg2=pickColor($site2);
$bg3=pickColor($site3);
$bg4=pickColor($site4);
echo"<h1>Results of radon testing</h1><p>";
echo"The table below shows some radon levels measured in residences.<br />";
echo"For values greater than or equal to 4 pCi/L, action should be taken<br />";
echo"to reduce the concentration of radon gas. For values greater than or<br />";
echo"equal to 3 pCi/L, retesting is recommended.</p>"; // Create the color-coded table.
echo"<table border><tr><th>Site name</th><th>Value</th></tr>"; echo"<tr bgcolor=$bg1>
<td>$site1_name</td><td>$site1</td></tr>"; echo"<tr bgcolor=$bg2>
<td>$site2_name</td><td>$site2</td></tr>"; echo"<tr bgcolor=$bg3>
<td>$site3_name</td><td>$site3</td></tr>"; echo"<tr bgcolor=$bg4>
<td>$site4_name</td><td>$site4</td></tr>"; echo"</table>";
function pickColor($value) { if($value>=4)$bg="pink"; elseif($value>=3)$bg="yellow"; else $bg="lightgreen";
return $bg; }
?>
Document 4.1a contains nothing new except for the line
<formmethod="post" action="radonTable1.php">
that includes two attributes of theformelement:methodandaction. This single line is all that is required to send the value ofeveryinputfield inside a form element to the PHP application radonTable1.php!
Document 4.1b contains a lot of new information, with syntax details that will be explained later in more detail. For now, consider these lines:
$site1=$_POST["site1"];
$site2=$_POST["site2"];
$site3=$_POST["site3"];
$site4=$_POST["site4"];
$site1_name=$_POST["site1_name"];
$site2_name=$_POST["site2_name"];
$site3_name=$_POST["site3_name"];
$site4_name=$_POST["site4_name"];
These statements “receive” the values entered into the corresponding input fields from Document 4.1a, sent as a result of clicking on the
“submit” button. These values are stored in variable names defined in the PHP application, indicated by the$in front of each name. In this case, the variable names are the same as the names defined in the input fields, but they don’t have to be. You can give the PHP variables any names you like, but it often makes sense to use the same names in both documents.
Recall that, previously, it was noted that values entered ininputfields are treated by HTML as “text” values; HTML does not “know” anything about numerical values. In this case, the first four variables, the radon values, are supposed to be numbers, while the last four, the site names, are text values. But, no problem! PHP will automatically interpret text that looks like a number as a numerical value. (If you pass a value to PHP that is supposed to be a number, but isn’t entered as something that looks like a number, PHP will not be able tofigure that out.1)
Next, the lines
$bg1=pickColor($site1);
$bg2=pickColor($site2);
$bg3=pickColor($site3);
$bg4=pickColor($site4);
1For example, entering an amount of money with a $, like $17.30, will not work.
68 4 Creating a PHP Environment
call a user-defined function (much more about that in Chap. 7) that calculates the appropriate color value for each radon value.
Finally, a series of PHP echo commands generate the output.
Optionally, the explanatory text from the HTML document, along with its formatting, is duplicated. A table is created in which the background colors are chosen based on the values of$bg1, $bg2, $bg3, and$bg4. Note that in a command like
echo "<tr bgcolor=$bg1>
the text string“value”of the variable $bg1is“pasted in”to the command;
it is not obvious that this should work so easily!