Home >> Snippet List >> Snippet

Name
Convert miles to feet, feet to miles, or inches.
Description
Handy function to convert miles to feet, feet to miles, miles to inches, etc
PHP Snippet
<?PHP
// position 1 = distance numeric numbers only no commas
// position 2 = current length type of position 1
// position 3 = convert length type of position 1 and 2
// position 4 = FT,MI,IN extentions on or off :: 1=on : 2=off
// m = miles
// f = feet
// i = inches
 
FUNCTION distance($curlen,$type,$totype,$on){
 
    // Check to see if the first value is an integer
    IF(!IS_INT($curlen)){
        RETURN 'Wrong input type for first value';
        EXIT;
    }
 
    // Make sure second value is either f,i, or m
    IF($type!="f"&&$type!="i"&&$type!="m"){
        RETURN 'Wrong input type for second value';
        EXIT;
    }
 
    // Make sure third value is either f,i, or m
    IF($totype!="f"&&$totype!="i"&&$totype!="m"){
        RETURN 'Wrong input type for third value';
        EXIT;
    }
 
    // Make sure fourth value is either 1 or 2
    IF($on!=1&&$on!=2){
        RETURN 'Wrong input type for fourth value';
        EXIT;
    }
 
    // If it validate the tests above, get the current type
    SWITCH($type){
        // if it is an i do the math
        CASE "i":
            IF($totype=="f"){
                $len = $curlen / 12;
                IF($on==1){
                    RETURN NUMBER_FORMAT(ROUND($len,1)).' FT';
                }ELSE{
                    RETURN NUMBER_FORMAT(ROUND($len,1));
                }
            }ELSEIF($totype=="m"){
                $len = $curlen / 63360;
                IF($on==1){
                    RETURN NUMBER_FORMAT(ROUND($len,1)).' MI';
                }ELSE{
                    RETURN NUMBER_FORMAT(ROUND($len,1));
                }
            }
        BREAK;
        // if it is an f do the math
        CASE "f":
            IF($totype=="i"){
                $len = $curlen * 12;
                IF($on==1){
                    RETURN NUMBER_FORMAT(ROUND($len,1)).' IN';
                }ELSE{
                    RETURN NUMBER_FORMAT(ROUND($len,1));
                }
            }ELSEIF($totype=="m"){
                $len = $curlen / 5280;
                IF($on==1){
                    RETURN NUMBER_FORMAT(ROUND($len,1)).' MI';
                }ELSE{
                    RETURN NUMBER_FORMAT(ROUND($len,1));
                }
            }
        BREAK;
        // if it is an m do the math
        CASE "m":
            IF($totype=="i"){
                $len = $curlen * 63360;
                IF($on==1){
                    RETURN NUMBER_FORMAT(ROUND($len,1)).' IN';
                }ELSE{
                    RETURN NUMBER_FORMAT(ROUND($len,1));
                }
            }ELSEIF($totype=="f"){
                $len = $curlen * 5280;
                IF($on==1){
                    RETURN NUMBER_FORMAT(ROUND($len,1)).' FT';
                }ELSE{
                    RETURN NUMBER_FORMAT(ROUND($len,1));
                }
            }
        BREAK;
    }
}
 
 
ECHO distance(123,"m","f",1);
 
?>