What will you do if you are told to list the title of the article with PHP?
Will print one by one with a command
echo
like this:<?php
echo "<h2>Belajar Pemrograman PHP untuk Pemula</h2>";
echo "<h2>Cara Menggunakan Perulangan di PHP</h2>";
echo "<h2>Memahami Struktur Kondisi IF di PHP</h2>";
echo "<h2>Memahami Perulangan di PHP</h2>";
echo "<h2>Prosedur dan Fungsi di PHP</h2>";
?>
It could be done.
But the problem is ...
What if there are 100 or 1000 data?
Are we able to type everything?
Must be tired.
Therefore, we must use repetition.
There are two types of repetitions in programming:
- Counted loop;
- Uncounted loop.
What's the difference?
Counted loop is a loop that has a lot of repetition. Whereas Uncounted loop is uncertain how many times he will repeat.
In PHP there are 4 types of repetitions that we can use:
- Repeat For
- While repetition
- Do / While repetition
- Foreach loop
Can you guess?
Which of the four loops is included in counted loops and uncounted loops ?
To find out, let's just discuss them one by one.
1. Repetition For
Repetition For is a loop that is included in a counted loop , because we can determine the number of repetitions.
Basic loop form for:
<?php
for ($i = 0; $i < 10; $i++){
// blok kode yang akan diulang di sini!
}
?>
Veriabel
$i
in repetition For serves as a counter that counts the times he will repeat.
The count will start from zero (
0
), because we give a value $i = 0
.
Then, the loop will be repeated as long as the value
$i
is smaller than 10
. That is, this loop will repeat as much 10x
.
The purpose of
$i++
the value $i
will be added 1
every time you do the loop.
Example:
<?php
for($i = 0; $i < 10; $i++){
echo "<h2>Ini perulangan ke-$i</h2>";
}
?>
The result:
2. While repetition
A while loop is a loop that is included in an uncounted loop . Because it is usually used to repeat something the number of repetitions is unclear.
However, recurrence while also can be used as a looping for a counted loop .
Basic form:
<?php
while (<kondisi>){
// blok kode yang akan diulang di sini
}
?>
Example:
<?php
$ulangi = 0;
while($ulangi < 10){
echo "<p>Ini adalah perulangan ke-$ulangi</p>";
$ulangi++;
}
?>
The result:
The while loop will continue to repeat as long as the value
$ulangi
is smaller than 10
.
Then in the loop we increment the value
$ulangi
with $ulangi++
. Meaning: Add 1 to each repetition.
Be careful, don't forget to add increments , or codes that will affect repetition. Because if not, the repetition will never stop and will make our computer hang .
3. Do / While Repetition
Recurrence Do / While as looping while . It is also classified as an uncounted loop .
The difference between Do / While and while lies in the way it starts repetition.
Recurrence Do / While there will always perform as many repetitions 1 time, then checks the condition.
While the while loop will check the condition first, then repeat.
The forms of repetition Do / While :
<?php
do {
// blok kode yang akan diulang
} while (<kondisi>);
?>
Example:
<?php
$ulangi = 10;
do {
echo "<p>ini adalah perulangan ke-$ulangi</p>";
$ulangi--;
} while ($ulangi > 0);
?>
The result:
4. Foreach repetition
The foreach loop is the same as the loop for . However, it is more specifically used to print arrays.
The form of the foreach loop :
<?php
foreach($array as $data){
echo $data;
}
Example:
<?php
$books = [
"Panduan Belajar PHP untuk Pemula",
"Membangun Aplikasi Web dengan PHP",
"Tutorial PHP dan MySQL",
"Membuat Chat Bot dengan PHP"
];
echo "<h5>Judul Buku PHP:</h5>";
echo "<ul>";
foreach($books as $buku){
echo "<li>$buku</li>";
}
echo "</ul>";
?>
The result:
Bonus: Nesting Repetition
Nesting repetition is the term for repetition in repetition. In English, nesting loops are called nested loops .
Examples of nesting loops:
<?php
for($i = 0; $i < 5; $i++){
for($j = 0; $j < 10; $j++){
echo "Ini perulangan ke ($i, $j)<br>";
}
}
?>
Another example:
<?php
$i = 0;
while($i < 10){
for($j = 0; $j < 10; $j++){
echo "Ini perulangan ke ($i, $j)<br>";
}
$i++;
}
The result:
Reference: https://www.petanikode.com/php-perulangan/
0 Komentar untuk "Learning PHP: Understanding 4 Types of Repetitions in PHP Programming"
Silahkan berkomentar sesuai artikel