Skip to content

Instantly share code, notes, and snippets.

@xmoonlight
Last active September 17, 2017 14:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xmoonlight/cabd04bff6506724ff946fef7e868624 to your computer and use it in GitHub Desktop.
Save xmoonlight/cabd04bff6506724ff946fef7e868624 to your computer and use it in GitHub Desktop.
Small currency exchange function (PHP, stand-alone)
<?php
function exchange($amount,$give,$get) {
$exchanges=[
'USD:RUB'=>60,
'RUB:USD'=>1/60,
];
$in=[$give.':'.$get=>$amount];
$key=key($in);
if ($amount>0 && $exchanges[$key]) {
$out=$exchanges[$key]*$in[$key];
return $out.' '.explode(':',$key,2)[1];
} else return false;
}
///MAIN////
if ($out=exchange(2,'USD','RUB')) echo $out;
else echo 'Exchange is not possible!';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment