Tsallaka zuwa abun ciki
Justice Tech School

Loops

while and do...while repeat on a condition, for counts, and foreach walks through arrays. break leaves a loop and continue skips a round. foreach can give you keys and values.

Example
for ($i = 1; $i <= 3; $i++) {
  echo "i = $i\n";
}
$names = ["Ada", "Tunde", "Zainab"];
foreach ($names as $i => $n) {
  echo "$i: $n\n";
}
$n = 3;
while ($n > 0) {
  echo $n--, " ";
}
echo "\n";

Result

i = 1
i = 2
i = 3
0: Ada
1: Tunde
2: Zainab
3 2 1 

Exercise

Walk through an array.

 ($items as $item) {}

Quiz Test

Which loop is made for arrays?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.