Conversion Calculator
// 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 = "
© 2003 PHP Stuff by Bill Pellowe
";
// remove commas and spaces
trim($value1);
$valuetemp = str_replace(",", "", $value1);
$value = str_replace(" ", "", $valuetemp);
if(!(is_numeric($value)))
{
$value="";
$msg_warn = "Type a number, no letters.
";
}
// 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 "";
echo $msg_warn;
echo "This does a variety of conversions, such as centimeters to inches as so on.
";
echo "First, put a number in the box, then choose a calculation, then press the button at the bottom.
";
echo "Use a period . for decimal points.
";
echo "
";
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 "";
echo "This does a variety of conversions, such as centimeters to inches as so on.
";
echo "First, put a number in the box, then choose a calculation, then press the button at the bottom.
";
echo "Use a period . for decimal points. It can't start with a letter.
";
echo "
";
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 .= " celsius (C)";
$unit2 = "degrees fahrenheit (F)";
break;
case "farcel":
$result = ($value - 32) * 0.5556;
$unit1 = "degree";
$unit1 .= $plural;
$unit1 .= " 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 .= " (km)";
$unit2 = "miles (mi)";
break;
case "mikm":
$result = $value * 1.60934;
$unit1 = "mile";
$unit1 .= $plural;
$unit1 .= " (mi)";
$unit2 = "kilometers (km)";
break;
case "kglb":
$result = $value * 2.20462;
$unit1 = "kilogram";
$unit1 .= $plural;
$unit1 .= " (kg)";
$unit2 = "pounds (lb)";
break;
case "lbkg":
$result = $value * 0.453592;
$unit1 = "pound";
$unit1 .= $plural;
$unit1 .= " (lb)";
$unit2 = "kilograms (kg)";
break;
case "mloz":
$result = $value * 0.0338146;
$resultUK = $value * 0.035195083;
$unit1 = "milliliter";
$unit1 .= $plural;
$unit1 .= " (ml)";
$unit2 = "U.S. ounces (oz), or ";
$unit2 .= number_format($resultUK, "3", ".", ",");
$unit2 .= " ounces in British measurement.
(1 US oz is 1.0408 UK oz)";
break;
case "ozml":
$result = $value * 29.57303;
$resultUK = $value * 28.41306;
$unit1 = "ounce";
$unit1 .= $plural;
$unit1 .= " (oz)";
$unit2 = "milliliters, but only if you're using US ounces.
If you're using British ounces, the answer is ";
$unit2 .= number_format($resultUK, "2", ".", ",");
$unit2 .= " milliliters (because one US oz is 1.0408 UK oz)";
break;
case "rbtb":
$result = $value * 0.75;
$unit1 = "regular-size can";
$unit1 .= $plural;
$unit1 .= " (350 ml, or 12 oz) of beer";
$unit2 = "tall cans (500 ml, or 16 oz) of beer.
(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 .= " (500 ml, or 16 oz) of beer";
$unit2 = "regular cans (350 ml, or 12 oz) of beer.
(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 "";
echo "Result of your conversion:";
echo "$value $unit1 is equal to ";
// format the result (number value, decimal places, decimal separator, thousands separator)
echo number_format($result, "2", ".", ",");
echo " $unit2.
";
echo "
";
echo "You can do another conversion if you'd like:
";
echo "$form_block";
}
?>
Script: PHP Stuff by Bill Pellowe