PHP 5 while Loops
PHP while loops execute a block of code while the specified condition is true.
PHP Loops
Often when you write code, you want the same block of code to run over and over again in a row. Instead of adding several almost equal code-lines in a script, we can use loops to perform a task like this.
In PHP, we have the following looping statements:
while
- loops through a block of code as long as the specified condition is truedo...while
- loops through a block of code once, and then repeats the loop as long as the specified condition is truefor
- loops through a block of code a specified number of timesforeach
- loops through a block of code for each element in an array
The PHP while Loop
The
while
loop executes a block of code as long as the specified condition is true.Syntax
while (condition is true) {
code to be executed;
}
How about it ? You must waiting for the example 🤓🤓🤓🤓Jadi.... sekarang akan saya beri contohnya
Hmmm… while itu bekerja secara sistematis, runtut, dan keren.
Kenapa keren ? karena kalo penulisannya berbeda tempat, hasilnya juga berbeda.
Gak Paham ? gpp, saya beri contoh sederhana
<?php
$jumlah = 0;
$bil = 2;
while ($bil <= 50)
{
$bil++;
$jumlah = $jumlah + $bil;
}
echo "Hasilnya adalah ".$jumlah;
?>
VS
<?php
$jumlah = 0;
$bil = 2;
while ($bil <= 50)
{
$jumlah = $jumlah + $bil;
$bil++;
}
echo "Hasilnya adalah ".$jumlah;
?>
Vs
Silahkan kalian buka postingan saya sebelumnya tentang For, terus
Bagaimana kalau contoh dari postingan tersebut menggunakan statement While ?!
Contohnya While ? Yuk Mari
1.
Anak AYAM
<html>
<head>
<title>
WHILE </title>
</head>
<body>
<h1> Syair Lagu Anak-Anak </h1>
<form method="POST" action=" ">
Masukkan jumlah Ayam yang diinginkan : <input type="text"
name="ayam" /><br>
<input type="submit" name="submit"
value="tampilkan">
<input type="reset" name="reset"
value="reset"><br>
</form>
Lirik Lagu Anak Ayam:<br>
<?php
$anak=$_POST["ayam"];
$a=$anak;
$z=1;
echo "Anak ayam turun $a <br>";
while ($z<=$a)
{
if($b=$a-1)
{
echo
"Anak ayam turun " . $a . ", mati satu tinggal " . $b .
"<br>";
}
else
{
echo
"Anak ayam turun 1 , mati satu tinggal induknya";
}
$a--;
}
?>
</body>
</html>
2.
Uang ?!?!? Menghitung Saldo ?
<html>
<head>
<title> WHILE </title>
</head>
<body>
<h1> Menetukan Saldo Akhir n-Bulan
</h1>
<form method="POST"
action=" ">
Masukkan nominal : <input type="text"
name="uang" />rupiah<br>
Masukkan jumlah bulan : <input type="text"
name="bln">bulan<br>
<input
type="submit" name="submit" value="Hitung">
</form>
Saldo Terakhir Anda:
<?php
$u=$_POST["uang"];
//$u=$uang;
$bln=$_POST["bln"];
//$b=$bln;
$z=1;
while($z<=$bln)
{
if($u<1100000)
{
$u=((0.03*$u)+($u-9000));
}
else
{
$u=((0.04*$u)+($u-9000));
}
$z++;
}
echo "Rp. $u ";
?>
</body>
</html>
3.
Daftar Perkalian
<html>
<head>
<title> WHILE </title>
</head>
<body>
<h1> Menampilkan Daftar Perkalian
</h1>
DAFTAR PERKALIAN:
<?php
$z=1;
while($z<=10)
{
$b=1;
while($b<=10)
{
$c=$z*$b;
echo "<br>";
echo " $z x $b = $c";
$b++;
}
echo "<br>";
$z++;
}
?>
</body>
</html>
4.
Formasi Bintang Semakin Besar
<html>
<head>
<title> WHILE </title>
</head>
<body>
<h1> Formasi Bintang </h1>
<form method="POST"
action=" ">
Masukkan n-bintang : <input type="text"
name="bil1" /><br>
<input type="submit"
name="submit" value="Tampilkan">
</form>
TAMPILKAN BINTANG:<br>
<?php
$a=$_POST["bil1"];
$z=1;
while($z<=$a)
{
$b=1;
while($b<=$z)
{
echo "*";
$b++;
}
echo "<br>";
$z++;
}
?>
</body>
</html>
5.
to-Serba
25
<html>
<head>
<title> WHILE </title>
</head>
<body>
<h1> Penjumlahan Tiga Variabel
</h1>
<?php
$x=1;
while ($x<=23)
{
$y=1;
while ($y<=23)
{
$z=23;
while ($z>=1)
{
$hasil=$x+$y+$z;
if($hasil==25)
{
echo
"$x+$y+$z<br>";
}
$z--;
}
$y++;
}
$x++;
}
?>
</body>
</html>
6.
Formasi Bintang Semakin Kecil
<html>
<head>
<title> WHILE </title>
</head>
<body>
<h1> Formasi Bintang </h1>
<form method="POST"
action=" ">
Masukkan n-bintang : <input type="text"
name="bil1" /><br>
<input type="submit"
name="submit" value="Tampilkan"><br>
</form>
Tampilkan Bintang: <br>
<?php
$a=$_POST["bil1"];
$z=1;
while($z<=$a)
{
$b=$a;
while($b>=$z)
{
$b--;
echo "*";
}
echo "<br>";
$z++;}
?>
</body>
</html>
7.
Formasi Bintang Pt. II
<html>
<head>
<title> WHILE </title>
</head>
<body>
<h1> Formasi Bintang Part II
</h1>
<form method="POST"
action=" ">
Masukkan n-bintang : <input type="text"
name="bil1" /><br>
<input type="submit"
name="submit" value="Tampilkan"><br>
</form>
Tampilkan Bintang: <br>
<?php
$a=$_POST["bil1"];
$z=1;
while($z<=$a)
{
$b=1;
while($b<=$z)
{
$b++;
echo "*";
}
echo "<br>";
$z++;
}
$z=$a-1;
while($z>=1)
{
$b=$z;
while($b>=1)
{
echo "*";
$b--;
}
echo "<br>";
$z--;
}
?>
</body>
</html>
Saya rasa cukup untuk hari ini, sekian dulu ya, Jangan Lupa Istirahat, Jangan Lupa Bahagia
Tidak ada komentar:
Posting Komentar