Anda di halaman 1dari 4

i

as
rm
3

fo
In
em

i
st

as
Si

rm
b.

fo
2.4 Switch

La

In
Digunakan untuk menyeleksi satu dari beberpa alternative pilihan.

em
st
Si
i
as
Bentuk Umum

b.

fo
m

La

In
or
switch(ekspresi)

em
In
{

em

st
case nilai_1:

Si
i
st

as
perintah1;

b.
Si

m
break;

La
or
b.

case nilai_2:
La

f
In
perintah2;

em
break;

i
default: perintahn; st

as
Si

b
}

m
i
as

La
or
b.
rm

La

f
Ekspresi adalah variable/pernyataan yang akan diperiksa nilainya.

In
fo

em
In

Perintah adalah, perintah perintah yang akan dilakukan jika ekspresi

i
m

st

as
te

memenuhi nialai tertentu. Peritah break digunakan untuk keluar dari


Si

m
i
as
is

or
b.
S

switch dan melakukan perintah dibawah switch(). Perintah default adalah


m

La

f
In
or
b.

perintah yang akan dijalankan jika ekspresi tidak sama dengan nilai-nilai
La

em
In
m

st
yang telah ditulis diatasnya.Default memiliki fungsi sama dengan perintah
e

Si
i
st

as

else pada if().


b.
Si

rm
i
as

La
b.

fo
La

In

Contoh :
m
te

i
as
is
S

m
i
as

or
b.
rm

La

f
In
fo

m
In

te
m

is
te

.S
i
as
is

b
rm

La
fo
In
m
te

i
as
is
S

rm
b.

fo
La

In
m
te
is
b .S
La
i
as
rm
fo
In
em

i
st

as
Si

rm
b.

fo
<?php

La

In
$Nama_hari=date('l');

em
echo "Hari ini hari =";

st
Si
i
switch($Nama_hari)

as

b.

fo
{

La

In
or
case 'Sunday':

em
In
echo "Minggu";

em
break;

st
Si
case 'Monday':

i
st

as

b.
Si
echo "Senin";

La
break;

or
b.
La

f
case 'Tuesday':

In
echo "Selasa";
break;
em

i
st

as
case 'Wednesday':
Si

b
m
i
as

La
echo "Rabu";

or
b.
rm

break;
La

f
In
fo

case 'Thursday':

em
In

echo "Kamis";

i
m

st

as
break;
te

Si

m
i
as

case 'Friday':
is

or
b.
S

echo "Jumat";
La

f
In
or
b.

break;
La

em
In

default:echo "Sabtu";
m

}
st
e

Si
?>
i
st

as

b.
Si

rm
i
as

La
b.

fo
La

In

Output :
m
te

Hari ini hari=Senin


as
S is

m
i
as

or
b.
rm

La

f
In
fo

m
In

Tuntunan Latihan
te
m

is
te

1. Aktifkan Start Running Apache pada XAMPP.


.S
i
as
is

b
rm

2. Buka Notepad
La
fo

3. Ketikkan program pada lembar kerja Notepad


In
m

4. Simpan di folder XAMPP\htdocs\namafile.php


te

i
as
is

5. Buka browser dan ketik localhost\namafile.php pada address bar untuk


S

rm
b.

menjalankan program.
fo
La

In
m
te
is
b .S
La
i
as
rm
fo
In
em

i
st

as
Si

rm
b.

fo
Latihan

La

In
1. Buatlah file pada htdocs dengan nama latihan2.php?

em
a. Buatlah script untuk menampilkan “Online Shop”?

st
Si
i
as
b. Buatlah variabel barang=baju; jenis=1;harga=20000000;jumlah=5;

b.

fo
m

La

In
or
c. Buatlah rumus kondisi if, apabila jenis barang 1 dibeli lebih dari 3

em
In
mendapatkan potongan 25% dan tampilkan total_harga beserta

em

st
Si
diskon?

i
st

as

b.
Si

La
or
b.
La

f
2. Lengkapilah program dibawah ini

In
em
<?php

i
st

as
$nama="Hibiku";
Si

b
m
i
as

La
or
b.

$warna="Merah";
rm

La

f
In
fo

echo $nama+"&nbsp;";

em
In

switch($warna){

i
m

st

as
te

Si

m
i

case "Merah":echo "<p


as
is

or
b.
S

La

f
style=background:red'>menyukai warna

In
or
b.
La

em
In

merah.</p>";break;
m

st
case "Biru"echo "<p
e

Si
i
st

as

b.

style='background:blue'>menyukai warna
Si

rm
i
as

La
b.

fo

biru</p>";braek;
La

In

case "Kuning’:echo "<p -


m
te

style='background:yellow'>menyukai warna
as
Sis

m
i
as

kuning</p>";break;
or
b.
rm

La

f
In

case "Hijau":echo "<p


fo

m
In

style='background:green'>menyukai warna
te
m

is
te

hijau</p>".break;
.S
i
as
is

b
rm

}
La
fo

?>
In
m
te

i
as
is
S

rm
b.

fo
La

In
m
te
is
b .S
La
is as
te
m i
La In
b. fo La
S rm
is as b.
te i S is
m te
La In
fo m
b.S rm La In
b. fo
is as rm
te
m i Si
st as
In e m i
fo La In
rm f
b. or La
as
i S m b.
is as
te
m i Si
st
La In em
b fo La In
.S rm f
is b. or
te
as
i Si m
m st as
In em i
for La In
m b. for La
as
i Si m b.
st as
em i Si
st
La In em
b. for La In
Si m b. fo
st as rm
em i Si
st as
In em i
for La In
m b. fo
as rm
i Si
st as
em i
La In
b fo

Anda mungkin juga menyukai