as
rm
fo
In
em
i
st
as
Si
rm
4
b.
fo
PERULANGAN
La
In
em
st
Si
i
as
OBJEKTIF
b.
fo
m
La
In
or
1. Mahasiswa memahami perulangan yang ada pada PHP
em
In
2. Mahasiswa memahami bentuk-bentuk perulangan yang ada pada
em
st
Si
i
PHP st
as
b.
Si
rm
La
3. Mahasiswa dapat membuat program dengan menggunakan
b.
fo
La
In
perulangan
m
te
i
as
s
Si
Perintah perulangan / loop statement adalah perintah untuk
b
m
i
as
La
or
b.
rm
f
In
fo
i
m
st
as
statement berulang-ulang. Perulangan pada PHP yaitu: for(),while() dan
te
Si
m
i
as
is
do while().
or
b.
S
La
f
In
r
b.
fo
La
em
In
3.1. For
em
st
Si
Bentuk umum dari for().
i
st
as
b.
Si
rm
i
as
La
fo
La
{
In
}
te
i
as
S is
m
i
as
La
m
In
is
.S
i
as
is
La
fo
Contoh :
te
i
as
Sis
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
for($n=1;$n<=5;$n++){
em
echo "1+",$n,"=",1+$n,"<br>";
st
}
Si
i
?>
as
b.
fo
m
La
In
or
Output :
em
In
1+1=2
em
st
Si
i
1+2=3 st
as
b.
Si
rm
La
1+3=4
b.
fo
La
In
1+4=5
m
1+5=6
te
i
as
s
Si
b
m
i
as
La
or
b.
rm
3.2. While
La
f
In
fo
Bentuk umum :
em
In
i
m
st
as
while(syarat pengulangan)
te
Si
m
i
as
is
or
b.
S
f
In
r
b.
fo
}
La
em
In
em
st
Berbeda dengan for(), while() tidak memiliki inisialisasi. Oleh karena itu
Si
i
st
as
rm
i
as
La
b.
In
yang akan diulang. Hal ini perlu dilakukan unruk menjamin bahwa perintah
m
te
m
i
as
or
b.
m
Contoh :
La
f
In
r
fo
<?php
In
te
$n=5;
m
is
te
while($n>0)
.S
i
as
is
{
b
rm
La
$n--;
In
}
m
?>
te
i
as
Sis
rm
b.
fo
La
In
m
te
is
b .S
La
i
as
rm
fo
In
em
i
st
as
Si
rm
b.
fo
Output :
La
In
5
em
st
4
Si
i
as
3
b.
fo
m
La
In
or
2
em
In
1
em
st
Si
i
st
as
b.
Si
3.3. Do-While
rm
La
b.
fo
La
In
Bentuk umum dari do while() :
m
te
i
while(syarat pengulangan)
as
s
Si
b
{
m
i
as
La
or
b.
La
f
In
}
fo
em
In
i
m
st
as
te
Si
m
i
or
b.
S
La
f
Perintah do while() akan mengulang statement miliknya selama syarat
In
r
b.
fo
La
em
In
st
perintahnya terlebih dahulu. Setelah itu, do while() memeriksa apakah
Si
i
st
as
b.
Si
La
b.
fo
In
i
as
m
i
as
or
b.
<?php
m
La
$n=5;
In
r
fo
do{
m
In
te
is
$n--;
te
.S
i
as
is
}while($n<0);
b
rm
?>
La
fo
In
m
te
Output :
i
as
Sis
rm
5
b.
fo
La
In
m
te
is
b .S
La
i
as
rm
fo
In
em
i
st
as
Si
rm
b.
fo
Tuntunan Latihan
La
In
1. Aktifkan Start Running Apache pada XAMPP.
em
st
2. Buka Notepad
Si
i
as
3. Ketikkan program pada lembar kerja Notepad
b.
fo
m
La
In
or
4. Simpan di folder XAMPP\htdocs\namafile.php
em
In
5. Buka browser dan ketik localhost\namafile.php pada address bar untuk
em
st
Si
menjalankan program.
i
st
as
b.
Si
rm
La
b.
fo
Latihan
La
In
1. Buatlah file pada htdocs dengan nama latihan3.php?
m
te
i
as
a. Tuliskan script dibawah ini:
s
Si
b
m
i
as
La
<?php
or
b.
rm
La
f
In
for($a=1;$a<7;$a++){
fo
em
In
for($b=7;$b>=$a;$b--)
i
m
st
as
te
echo " ";
Si
m
i
as
is
or
b.
S
for($c=1;$c<=$a;$c++)
m
La
f
In
r
b.
fo
echo "* ";
La
em
In
em
echo"<br>";
st
Si
i
st
as
b.
Si
rm
i
as
La
}
b.
fo
La
In
?>
m
te
i
as
Sis
m
i
Output:
as
or
b.
m
La
f
In
r
fo
m
In
te
m
is
te
.S
i
as
is
b
rm
La
fo
In
m
te
i
as
Sis
rm
b.
fo
La
In
m
te
is
b .S
La
i
as
rm
fo
In
em
i
st
as
Si
rm
b.
fo
La
In
2. Lengkapilah program dibawah ini:
em
$harga_baju = 5;
st
Si
i
as
Echo “Harga Baju PT. Sejahtera”;
b.
fo
m
La
echo "<table border=\"1" align=\"center\">";
In
or
f
em
In
echo "<tr><tb>kualitas baju</th>";
em
st
echo <th>harga</th></tr>";
Si
i
st
as
b.
Si
$conter = 10;
rm
La
b.
fo
while ($counter <= 100){
La
In
m
echo "<tr><td>";
te
i
as
echo $counter
s
Si
b
m
i
as
La
echo "</td><td>";
or
b.
rm
La
f
In
echo $harga_baju * $counter;
fo
em
In
echo "</td></tr>";
i
m
st
as
te
Si
$counter += 10
m
i
as
is
or
b.
S
}
La
f
In
r
b.
fo
La
em
echo "</table>";
In
em
st
Si
i
st
as
b.
Si
Output:
rm
i
as
La
b.
fo
La
In
m
te
i
as
S is
m
i
as
or
b.
m
La
f
In
r
fo
m
In
te
m
is
te
.S
i
as
is
b
rm
La
fo
In
m
te
i
as
Sis
rm
b.
fo
La
In
m
te
is
b .S
La