Location: PHPKode > scripts > UnitConvertor > unitconvertor/unitconv.php
<?php
require_once("UnitConvertor.php");

// Create a new instance of the convertor, override default decimal point and thousand
// separator characters
$conv = new UnitConvertor('.', ',');

// Build up the conversion table by adding conversions. It is better 
// to group all units of one type into one function call. This ensures that conversion
// is always successful.
// weights
$conv->addConversion('kg', array('pound'=>2.20));
// distance
$conv->addConversion('km', array('meter'=>1000, 'dmeter'=>10000,
					'centimeter/cm'=>100000, 'millimeter'=>1000000, 
					'mile'=>0.62137, 'naut.mile'=>0.53996, 'inch/inches/zoll'=>39370,
					'ft/foot/feet'=>3280.8, 'yd/yard'=>1093.6));
// temperature
$conv->addConversion('Centigrade', array(
					'Fahrenheit'=>array('ratio'=>1.8, 'offset'=>32),
					'Kelvin'=>array('ratio'=>1, 'offset'=>273),
					'Reaumur'=>0.8)
					);
 
$centigrade = 20;

print "<pre>";
//print_r($conv->conversion_table);
print '</pre>';

print '<h4><font color="green">UnitConvertor valid direct tests</font></h4>';
print('20 km is '.$conv->convert(20, 'km', 'mile', 2).' mile');
print('<br><br>1 km is '.$conv->convert(1, 'km', 'meter', 1).' m');
print('<br><br>12.5 °C is '.$conv->convert(12.5, 'Centigrade', 'Fahrenheit', 1).' Fahrenheit');
print('<br><br>10 °C is '.$conv->convert(10, 'Centigrade', 'Kelvin', 1).' Kelvin');
print('<br><br>10 °C is '.$conv->convert(10, 'Centigrade', 'Reaumur', 1).' Reaumur');

print '</pre><h4><font color="green">UnitConvertor valid reverse tests</font></h4>';

print('20 miles is '.$conv->convert(20,  'mile','km', 2).' km');
print('<br><br>800 meter is '.$conv->convert(800,  'meter','km', 3).' km');
print('<br><br>30,2 °F is '.$conv->convert(30.2, 'Fahrenheit', 'Centigrade', 1).' Centigrade');
print('<br><br>10 °K is '.$conv->convert(10,  'Kelvin','Centigrade', 1).' Centigrade');
print('<br><br>10 °R is '.$conv->convert(10, 'Reaumur', 'Centigrade', 1).' Centigrade');

print '</pre><h4><font color="green">UnitConvertor valid indirect tests</font></h4>';

print $centigrade.'°C is '.$conv->convert($centigrade, 'Centigrade', 'Centigrade', 2).' Centigrade';
print("<br><br> 50°F is ".$conv->convert(50, 'Fahrenheit', 'Kelvin', 2).' Kelvin');
print("<br><br>".'10 Kelvin is '.$conv->convert(10, 'Kelvin', 'Fahrenheit', 2).' Fahrenheit');
print("<br><br>".'10 Reaumur is '.$conv->convert(10, 'Reaumur', 'Fahrenheit', 2).' Fahrenheit');
print("<br><br>".'10 Reaumur is '.$conv->convert(10, 'Reaumur', 'Centigrade', 2).' Centigrade');
print("<br><br>".'8 C is '.$conv->convert(8, 'Centigrade', 'Fahrenheit', 2).' Fahrenheit');
print('<br><br>32 °F is '.$conv->convert(32, 'Fahrenheit', 'Kelvin', 1).' Kelvin');
print('<br><br>273 °K is '.$conv->convert(273, 'Kelvin', 'Fahrenheit', 1).' Fahrenheit');
print "<hr />";

print '</pre><h4><font color="green">UnitConvertor invalid illogical indirect tests</font></h4>';
print('32 °F is '.$conv->convert(32, 'Fahrenheit', 'km', 1).'km');
?>
Return current item: UnitConvertor