Pemrograman WEB. P4 Materi Alur Keputusan Switch Case cth3

<

Tutorial PHP : Contoh Penggunaan IF ELSE dan SWITCH CASE pada Diskon Script PHP

Pada kesempatan kali ini kita akan membahas salah satu control structure yaitu switch case pada PHP. Kegunaan Switch Case sama dengan If Else dan Elseif pada PHP, perbedaan utama adalah 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:

CONTOH : PROGRAM IF ELSE

 
<?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> 
 



CONTOH : SWITCH CASE
 
 <?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

Popular posts from this blog

P CRUD

Pemrograman WEB. P4 Materi Logika Percabangan IF cth3

Pemrograman WEB. P4 Materi Logika Percabangan IF cth1