Pemrograman WEB. P4 Materi Alur Keputusan Switch Case cth3
<
switch case
ditulis sedemikian rupa sehingga dapat membandingkan nilai yang telah
didefinisikan tanpa mengevaluasi kondisi, dengan karakter tersebut waktu
eksekui switch case
lebih cepat dari pada if else
, contoh:
<?php
@$toyar= $_POST['toyar'];
?>
<html>
<head>
<title>.:: Tugas Absen ::.</title>
</head>
<body>
<center>
<font face=verdana size=2>
<form method="POST">Jumlah Bayar :
<input type=text name="toyar"><br><br>
<input type="submit" value="Hitung Discount">
<br>
<?php
{
$diskon=0;
if ($toyar>=500000)
$diskon=(0.5*$toyar);
else
if ($toyar>=100000)
$diskon=(0.01*$toyar);
else
if ($toyar>=50000)
$diskon=(0.05*$toyar);
printf ("<b><br>Jumlah Bayar = Rp. $toyar</b>");
printf ("<b><br>Diskon = Rp. $diskon</br></b>");
$jumlahbayar=$toyar-$diskon;
printf ("<b>Total Bayar = Rp. $jumlahbayar</b>");
}
?>
</center>
</form>
</body>
</html>
<?php
@$toyar= $_POST['toyar'];
?>
<html>
<head>
<title>.:: Tugas Absen ::.</title>
</head>
<body>
<center>
<font face=verdana size=2>
<form method="POST">Jumlah Bayar :
<input type=text name="toyar"><br><br>
<input type="submit" value="Hitung Discount">
<br>
<?php
$jumlah_bayar = "";
$diskon = "";
$proses = "toyar";
switch($toyar)
{
case (0):
break;
case ($toyar>=500000):
$diskon = 0.5 * $toyar;
break;
case ($toyar>=100000) :
$diskon = 0.1 * $toyar;
break;
case ($toyar>=50000):
$diskon = 0.05 * $toyar;
break;
default:
$diskon = "0";
break;
}
echo "jumlah bayar = " . $toyar . "<br/>";
echo "diskon = " . $diskon . "<br/>";
$jumlah_bayar=$toyar-$diskon;
echo "<b>Total Bayar = RP. $jumlah_bayar </b><br/>";
?>
</center>
</form>
</body>
</html>
Comments
Post a Comment