Php,支付硬币

时间:2013-02-28 13:37:24

标签: php

如果要制作一台“自动售货机”并且你会写下你要付多少钱,如果你为一块2英镑的物品支付100英镑,那么让它回应你收到的金额,然后回复金额在硬币(1p,5p等等。如果你得到我的漂移。)你会怎么做硬币的回声?

$valg = $_POST['select'];
$pris = $_POST['pris'];

$r_pris = explode(" ", $valg);
$resultat = $r_pris['1']-$pris; 

if ($resultat<0){
    $slut_pris = "du skal have " . $resultat . " kroner tilbage";
} elseif($resultat==0){
    $slut_pris = "lige og plet på.";
}
else {
    $slut_pris = "du mangler " . $resultat . " kroner";
}

1 个答案:

答案 0 :(得分:1)

你可以这样做

<?php

$amt_paid = 100; //Fetch the user input
$real_amount = 2; //Pre-Defined Amount

if($amt_paid > $real_amount) { //Check whether the paid amount is more than defined amount
    $amount_paybck = $amt_paid - $real_amount;
    echo '£'.$amount_paybck; //You can divide this value to get a output in coins
    echo '10P'.ceil($amount_paybck/10).'<br />'; //Coins of 10
    echo '5P'.ceil($amount_paybck/5).'<br />'; //Coins of 5
    echo '2P'.ceil($amount_paybck/2).'<br />'; //Coins of 2
} else {
    echo 'Amount paid is in-sufficient';
}

?>