PHP Script for Metric Conversion Calculator

About the conversion script

This is the free php script for the metric conversion calculator on this site. The script was created March 2002. You can use the script on your site for free, as long as you keep the credit and link at the bottom of the page.

The metric conversion script

Copy everything below, save it to a text file, and upload it to an Apache server with PHP installed.

<?
// if you've got a PHP image, put the path here. Otherwise, you can just use the words PHP.
$php_link = "<img border=\"0\" src=\"../images/php-small-white.gif\" align=\"texttop\" hspace=\"30\" width=\"88\" height=\"31\" alt=\"PHP\">";
 
 
?>
<html>
<head>
<title>conversion scripts</title>
<META Name="description" Content="Converts degrees Celsius and Fahrenheit; centimeters and inches; meters and feet; kilometers and miles; kilograms and pounds; milliliters to ounces; and for fun, regular beers and tall beers. ">
<META Name="keywords" Content="convert, converstion, degrees Celsius, Fahrenheit, centimeters, inches meters, feet; kilometers, miles, kilograms, pounds, milliliters, ounces, PHP, PHP4, metric conversion">
</head>
<body bgcolor=white>
<! -- Convert v 1.1, Bill Pellowe -->
<!-- What this does: some conversions, such as metric conversions. Added a conversion between different beer sizes just for fun. -->
<!-- Notes on error checking: Distinguishes singular and plural in the first value. Reloads if no values are given. Tidied up, faster than 1.0 -->
 
<?
// array of key (what to convert) and value (description of it)
$convert_array[celfar] = "degrees Celsius to Fahrenheit";
$convert_array[farcel] = "degrees Fahrenheit to Celsius";
$convert_array[cmin] = "centimeters to inches";
$convert_array[incm] = "inches to centimeters";
$convert_array[mefe] = "meters to feet";
$convert_array[feme] = "feet to meters";
$convert_array[kmmi] = "kilometers to miles";
$convert_array[mikm] = "miles to kilometers";
$convert_array[kglb] = "kilograms to pounds";
$convert_array[lbkg] = "pounds to kilograms";
$convert_array[mloz] = "milliliters to ounces";
$convert_array[ozml] = "ounces to milliliters";
$convert_array[rbtb] = "regular beers to tall beers";
$convert_array[tbrb] = "tall beers to regular beers";
 
$form_block = "
<form method  = \"post\" action = \"$PHP_SELF\">
<p>Enter a number:&nbsp;&nbsp;<input type = \"text\" name=\"value1\"  size=10></p>
<p>Change that number from...<br>
<select name =\"convert\">";
 
if(!($convert)) {
  $con_sel = "celfar";
  }
  else{
  $con_sel = $convert;
  }
// build the menu
foreach($convert_array as $key=>$desc)
{
  if($key=="$con_sel") {
  $selected = " selected";
  }
  else{
  $selected = "";
  }
$form_block .= "<option value=\""
            . $key
            . "\""
            . $selected
            . ">$desc</option>";
} // end of for each
 
$form_block .= "</select></p>
<p><input type=\"submit\" name=\"submit\" value=\"Do the conversion\"></p>
</form>
<p>&nbsp;</p>
<p>&nbsp;<font size=\"-2\" face=\"verdana, sans-serif\">&copy; 2002 <a href=\"index.php\">PHP Stuff by Bill Pellowe</a></p>
";
 
// remove commas and spaces
trim($value1);
$valuetemp = str_replace(",", "", $value1);
$value = str_replace(" ", "", $valuetemp);
if(!(is_numeric($value)))
{
  $value="";
  $msg_warn = "<h2>Type a number, no letters.</h2>";
}
 
// get the first character in the value to  later check  that it's not a letter
$check = substr($value, 0, 1);
 
// in case no number is entered, the page refreshes
if ($value  == "") {
        echo "<p><h1>Conversion Tool<img border=\"0\" src=\"../images/php-small-white.gif\" align=\"texttop\" hspace=\"30\" width=\"88\" height=\"31\" alt=\"PHP\"></h1></p>";
        echo $msg_warn;
        echo "<ul>This does a variety of conversions, such as centimeters to inches as so on.<br>";
        echo "First, put a number in the box, then choose a calculation, then press the button at the bottom.<br>";
        echo "Use a period . for decimal points.</ul>";
        echo "<hr>";
        echo "$form_block";
 
} else if (($check != "0") && ($check != "1") && ($check != "2") && ($check != "3")  && ($check != "4") && ($check != "5") && ($check != "6") && ($check != "7") && ($check != "8") && ($check != "9") && ($check != ".") && ($check != "-")) {
// oddly the quotes were needed above around 0 etc for absolute values otherwise letters parsed through
        echo "<h1>Conversion Tool<img border=\"0\" src=\"../images/php-small-white.gif\" align=\"texttop\" hspace=\"30\" width=\"88\" height=\"31\" alt=\"PHP\"></h1></p>";
        echo "<ul>This does a variety of conversions, such as centimeters to inches as so on.<br>";
        echo "First, put a number in the box, then choose a calculation, then press the button at the bottom.<br>";
        echo "Use a period . for decimal points. <strong>It can't start with a letter.</strong></ul>";
        echo "<hr>";
        echo "$form_block";
 
} else {
        {
                if ($value == 1) {
                        $plural = "";
                        } else
                $plural = "s";
         }
 
   switch($convert)
   {
   case "celfar":
        $result = ($value*1.8) +32;
        $unit1 = "degree";
        $unit1 .= $plural;
        $unit1 .= "&nbsp;celsius (C)";
        $unit2 = "degrees fahrenheit (F)";
   break;
   case "farcel":
        $result = ($value - 32) * 0.5556;
        $unit1 = "degree";
        $unit1 .= $plural;
        $unit1 .= "&nbsp;fahrenheit (F)";
        $unit2 = "degrees celsius (C)";
   break;
   case "cmin":
        $result = $value * 0.393701;
        $unit1 = "centimeter";
        $unit1 .= $plural;
        $unit2 = "inches";
   break;
   case "incm":
        $result = $value * 2.54;
        $unit1 = "inch";
        if ($plural == "s") {
                $plural = "es";
                }
        $unit1 .= $plural;
        $unit2 = "centimeters";
   break;
   case "mefe":
        $result = $value * 3.28084;
        $unit1 = "meter";
        $unit1 .= $plural;
        $unit2 = "feet";
   break;
   case "feme":
        $result = $value * 0.3048;
        if ($plural != "s") {
                $unit1 = "foot";
                } else {
                $unit1 = "feet";
                }
        $unit2 = "meters";
   break;
   case "kmmi":
        $result = $value * 0.621371;
        $unit1 = "kilometer";
        $unit1 .= $plural;
        $unit1 .= "&nbsp;(km)";
        $unit2 = "miles (mi)";
  break;
  case "mikm":
        $result = $value * 1.60934;
        $unit1 = "mile";
        $unit1 .= $plural;
        $unit1 .= "&nbsp;(mi)";
        $unit2 = "kilometers (km)";
  break;
  case "kglb":
        $result = $value * 2.20462;
        $unit1 = "kilogram";
        $unit1 .= $plural;
        $unit1 .= "&nbsp;(kg)";
        $unit2 = "pounds (lb)";
  break;
  case "lbkg":
        $result = $value * 0.453592;
        $unit1 = "pound";
        $unit1 .= $plural;
        $unit1 .= "&nbsp;(lb)";
        $unit2 = "kilograms (kg)";
  break;
  case "mloz":
        $result = $value * 0.0338146;
        $resultUK = $value * 0.035195083;
        $unit1 = "milliliter";
        $unit1 .= $plural;
        $unit1 .= "&nbsp;(ml)";
        $unit2 = "U.S. ounces (oz), or&nbsp;";
        $unit2 .= number_format($resultUK, "3", ".", ",");
        $unit2 .= "&nbsp;ounces in British measurement. <br><i>(1 US oz is 1.0408 UK oz)</i>";
  break;
  case "ozml":
        $result = $value * 29.57303;
        $resultUK = $value * 28.41306;
        $unit1 = "ounce";
        $unit1 .= $plural;
        $unit1 .= "&nbsp;(oz)";
        $unit2 = "milliliters, but only if you're using US ounces. <br>If you're using British ounces, the answer is&nbsp;";
        $unit2 .= number_format($resultUK, "2", ".", ",");
        $unit2 .= "&nbsp;milliliters (because one US oz is 1.0408 UK oz)";
  break;
  case "rbtb":
        $result = $value * 0.75;
        $unit1 = "regular-size can";
        $unit1 .= $plural;
        $unit1 .= "&nbsp;(350 ml, or 12 oz) of beer";
        $unit2 = "tall cans (500 ml, or 16 oz) of beer.<br>(It doesn't matter if you have ml or oz beers; the difference in the ratio between ml and oz beers is only 0.000005 cans of beer.)";
  break;
  case "tbrb":
        $result = $value * 1.33;
        $unit1 = "tall can";
        $unit1 .= $plural;
        $unit1 .= "&nbsp;(500 ml, or 16 oz) of beer";
        $unit2 = "regular cans (350 ml, or 12 oz) of beer.<br>(It doesn't matter if you have ml or oz beers; the difference in the ratio between ml and oz beers is only 0.000005 cans of beer.)";
  break;
}
 
echo "<h1>Conversion Tool<a href=\"http://www.php.net\">$php_link</a></p></h1>";
echo "<strong>Result of your conversion:</strong></p>";
echo "<ul><strong>$value</strong> $unit1 is equal to <strong>";
// format the result (number value, decimal places, decimal separator, thousands separator)
echo number_format($result, "2", ".", ",");
echo "</strong> $unit2.</ul>";
echo "<hr>";
echo "<p>You can do another conversion if you'd like:</p>";
echo "$form_block";
}
 
?>

<p> <font size="-2" face="verdana, sans-serif">Script: <a href="http://www.eltcalendar.com/stuff/">PHP Stuff by Bill Pellowe</a></p>
</body>
</html>