Convert Liters Per 100 Kilometers To Miles Per Gallon With PHP
Like the wobbly wheel on a supermarket trolley, there are still countries in the world which refuse to move to the sanity of the metric system.
To aid these impoverished nations, this function will convert liters (litres) per 100 kilometers to Miles Per Gallon.
<?php
/**
*
* Convert liters per 100 kilometers to miles per gallon
*
* @param float $liters per 100km
* @param bool $imperial
* @return float
*
*/
function convert( $lp100km, $imperial=true ){
$uspg = 3.785; // liters per US gallon
$ukpg = 4.54609188; // liters per imperial (UK) gallon
$kmpm = 1.609344; // kilometers per mile
// choose UK or US miles per gallon
$lpg = $imperial == true ? $ukpg : $uspg;
// get miles per gallon
return (100 * $lpg) / ($kmpm * $lp100km);
}
?>
Usage
<?php
// get UK miles per gallon
$uk = convert( 10 );
// get US miles per gallon
$us = convert( 10, false );
echo "10 kilometers is $uk UK miles per gallon\n";
echo "10 kilometers is $us US miles per gallon\n";
?>