Chapterبەش 6 · Lectureوانە 6.2
Loops: while, do...while, for, foreach, break and continueخولەکان: while و do...while و for و foreach و break و continue
How to repeat code in PHP: the while, do...while, for and foreach loops, how to stop or skip turns with break and continue, and how to avoid infinite loops.
چۆن کۆد لە PHP دووبارە بکەیتەوە: while و do...while و for و foreach، چۆن بە break و continue ڕابگریت یان بازی بدەیت و چۆن خۆت لە بپارێزیت.
What you will learnچی فێر دەبیت
- Explain why loops are needed and name the four PHP loops.
- Write
whileanddo...whileloops and explain how they differ. - Write a
forloop with its three parts, separated by semicolons. - Go through an array with
foreach, with and without keys. - Use
breakandcontinue, and avoid infinite loops. - Trace a loop by hand and predict its exact output.
- ڕوونی بکەیتەوە بۆچی پێویستە و ناوی چوار PHP بهێنیت.
- و بنووسیت و جیاوازییەکەیان ڕوون بکەیتەوە.
- بە سێ بەشەکەیەوە بنووسیت، بە جیاکراوە.
- بە
foreachبە ناو بڕۆیت، بە و بێ . breakوcontinueبەکاربهێنیت و خۆت لە بپارێزیت.- شوێنی بە دەست بکەویت و ڕێکەکەی پێشبینی بکەیت.
Why we need loopsبۆچی پێویستمان بە خول هەیە
Often you want the same block of code to run again and again. Imagine printing the numbers 1 to 100 with 100 echo lines. Instead of writing many almost equal lines, you write the code once and put it in a . A loop is a block of code that runs repeatedly, based on a condition. Every programming language has loops: they are repetition, one of the three of every (three building blocks).
زۆرجار دەتەوێت هەمان چەند جار لە دوای یەک جێبەجێ بێت. وێنا بکە ژمارەکانی 1 تا 100 بە 100 دێڕی echo چاپ بکەیت. لە جیاتی نووسینی چەندین دێڕی نزیکەی وەک یەک، کۆدەکە یەک جار دەنووسیت و دەیخەیتە ناو . کە بەپێی چەند جار دووبارە جێبەجێ دەبێت. هەموو هەیە: پێیان دەوترێت دووبارەکردنەوە و یەکێکن لە سێ هەموو (سێ بەردی بناغە).
A waiter in a tea house in Sulaymaniyah fills one glass of tea, then the next, then the next. He repeats the same action while there are empty glasses on the tray. When no empty glass is left, he stops. That is a loop: one action, repeated, with a clear rule for when to stop.
چایچییەک لە چایخانەیەکی سلێمانی پەرداخێک چا پڕ دەکات، پاشان پەرداخی دواتر، پاشان ئەوەی دوای ئەویش. هەمان کار دووبارە دەکاتەوە مادام پەرداخی بەتاڵ لەسەر سینییەکە ماوە. کاتێک هیچ پەرداخێکی بەتاڵ نەما، دەوەستێت. ئەمە : یەک کار، دووبارە دەکرێتەوە، لەگەڵ یاسایەکی ڕوون بۆ ئەوەی کەی بوەستێت.
Where the analogy stops workingسنووری ئەم نموونەیە
A waiter never forgets to stop. A program can: if the stop rule never becomes true, the loop runs forever.
چایچی هەرگیز وەستان لەبیر ناکات. بەڵام دەتوانێت: ئەگەر یاسای وەستانەکە هەرگیز نەهاتە دی، بۆ هەمیشە بەردەوام دەبێت.
PHP has four loops:
PHP چوار جۆر هەیە:
while– loops through a block of code as long as the condition is true.do...while– runs the block once, and then repeats it as long as the condition is true.for– loops through a block of code a specified number of times.foreach– loops through a block of code for each element in an array.
- – دووبارە دەکاتەوە مادام ڕاست بێت.
- – بلۆکەکە یەک جار جێبەجێ دەکات، پاشان مادام ڕاست بێت دووبارەی دەکاتەوە.
- – ژمارەیەکی دیاریکراو جار دووبارە دەکاتەوە.
- – بۆ هەر لە جێبەجێ دەکات.
You can also control a loop from inside: break stops it, and continue skips one turn. These two keywords open the course's third module, Loops, Arrays and Functions. The module has three big ideas: control a loop, hold many values in one variable, and package code so you can use it again. Almost every page you write later uses all three. Arrays and functions come in the next chapter.
دەتوانیت لە ناوەوەش کۆنترۆڵ بکەیت: break ڕایدەگرێت و continue یەک بازدەدات. break و continue دەستپێکی مۆدیولی سێیەمی کۆرسەکەن، ، و . ئەم مۆدیولە سێ بیرۆکەی گەورەی هەیە: کۆنترۆڵکردنی ، هەڵگرتنی چەندین لە یەک و کۆکردنەوەی کۆد لە یەک شوێندا بۆ ئەوەی دووبارە بەکاری بهێنیتەوە. نزیکەی هەموو ئەو پەڕانەی دواتر دەینووسیت هەر سێکیان بەکاردەهێنن. و لە بەشی داهاتوودا دێن.
The while loopخولی while
A while loop checks a condition before each turn. If the condition is true, the body runs. Then PHP goes back and checks again. When the condition becomes false, the loop ends and PHP continues after it.
پێش هەموو دەپشکنێت. ئەگەر ڕاست بێت، ناوەڕۆکی جێبەجێ دەبێت. پاشان PHP دەگەڕێتەوە و دیسان دەپشکنێت. کاتێک هەڵە دەبێت، کۆتایی دێت و PHP لە دوایەوە بەردەوام دەبێت.
while (condition is true) {
statement;
}The condition is a Boolean expression: an expression whose answer is true or false. One more rule is very important: something inside the body must change the condition, so that one day it becomes false.
: کە وەڵامەکەی true یان falseـە. یاسایەکی تریش زۆر گرنگە: شتێک لەناو دەبێت بگۆڕێت، تا ڕۆژێک ببێتە هەڵە.
Watch PHP print HTML tags with echo, so the browser shows headings and lines that your PHP code made.
سەیر بکە چۆن بە چاپ دەکات، تا ئەو سەردێڕ و دێڕانە پیشان بدات کە کۆدی خۆت دروستی کردوون.
Plays only the 'While Loops' chapter (3:05:09–3:15:18).
تەنها بەشی While Loops لێدەدات (لە 3:05:09 تا 3:15:18).
The while loopخولی while
Example: count from 0 to 10نموونە: ژماردن لە 0 تا 10
<?php
$count = 0;
while ($count <= 10) {
echo $count . ", ";
$count++;
}
echo "<br /> Count: {$count}";
?>0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, Count: 11
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, <br /> Count: 11Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
The starts at 0.
لە 0ـەوە دەست پێدەکات.
- Lineدێڕی 4
Before every turn: is
$countstill 10 or less?پێش هەموو : ئایا
$countهێشتا 10 یان کەمترە؟- Lineدێڕی 5
Print the number and a comma. The dot
.joins two strings ().ژمارەکە و فاریزەیەک چاپ دەکات. خاڵ
.دوو پێکەوە دەلکێنێت ().- Lineدێڕی 6
$count++adds 1 (). Without this line the loop never ends.$count++1 زیاد دەکات (). بەبێ ئەم دێڕە هەرگیز کۆتایی نایەت.- Lineدێڕی 9
After the loop.
{$count}puts the variable's value inside the double-quoted string ().دوای .
{$count}دەخاتە ناو جووتەگوتەییەکەوە ().
Two details are worth noticing:
دوو وردەکاری شایەنی سەرنجن:
- After the loop,
$countis 11, not 10. The loop stops only when the test11 <= 10is false, so the counter has already gone one step past 10. - The output ends with an extra
,, because every turn prints a comma, also the last one.
- دوای ،
$countیەکسانە بە 11، نەک 10. تەنها کاتێک دەوەستێت کە پشکنینی11 <= 10هەڵە بێت، بۆیە یەک هەنگاو لە 10 تێپەڕیوە. - بە
,ێکی زیادە کۆتایی دێت، چونکە هەموو فاریزەیەک چاپ دەکات، دوایین .
while checks first; do...while runs the body first and checks afterwards.while سەرەتا دەپشکنێت؛ do...while سەرەتا ناوەڕۆکی جێبەجێ دەکات و پاشان دەپشکنێت.CêITThe while loopخولی while
An if statement inside a loopڕستەی if لەناو خولدا
You can put any code inside a loop, also an if...else. This example prints the word FIVE instead of the number 5:
دەتوانیت هەر کۆدێک بخەیتە ناو ، if...elseیش. ئەم نموونەیە لە جیاتی ژمارە 5 وشەی FIVE چاپ دەکات:
<?php
$count = 0;
while ($count <= 10) {
if ($count == 5) {
echo "FIVE";
} else {
echo $count . ", ";
}
$count++;
}
echo "<br /> Count: {$count}";
?>0, 1, 2, 3, 4, FIVE6, 7, 8, 9, 10, Count: 11
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
0, 1, 2, 3, 4, FIVE6, 7, 8, 9, 10, <br /> Count: 11Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 5
In every turn, PHP checks: is this turn the number 5?
لە هەموو PHP دەپشکنێت: ئایا ئەم ژمارە 5ـە؟
- Lineدێڕی 6
Yes: print
FIVE(but no comma after it).بەڵێ:
FIVEچاپ دەکات (بەڵام بێ فاریزە لە دوایەوە).- Lineدێڕی 8
No: print the number and a comma.
نەخێر: ژمارەکە و فاریزەیەک چاپ دەکات.
- Lineدێڕی 10
The counter grows in every turn, inside or outside the
if.لە هەموو زیاد دەبێت، چ لەناو
ifبێت چ نا.
Look at the output: FIVE6. The word FIVE has no comma or space after it, so it sticks to the next number. Fix it by printing "FIVE, ":
سەیری بکە: FIVE6. وشەی FIVE هیچ فاریزە و لە دوا نییە، بۆیە بە ژمارەی دواترەوە دەلکێت. بە چاپکردنی "FIVE, " چاکی بکە:
<?php
$count = 0;
while ($count <= 10) {
if ($count == 5) {
echo "FIVE, ";
} else {
echo $count . ", ";
}
$count++;
}
echo "<br /> Count: {$count}";
?>0, 1, 2, 3, 4, FIVE, 6, 7, 8, 9, 10, Count: 11
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
0, 1, 2, 3, 4, FIVE, 6, 7, 8, 9, 10, <br /> Count: 11The while loopخولی while
Worked example: even and odd numbers from 0 to 30نموونەی چارەسەرکراو: ژمارە جووت و تاکەکان لە 0 تا 30
A student's practice notes contain a nice while exercise: print the even numbers from 0 to 30 and their sum, then the odd numbers and their sum. A number is even when the remainder of dividing it by 2 is 0. The operator % gives that remainder: 7 % 2 is 1, 8 % 2 is 0.
تێبینییەکانی ڕاهێنانی خوێندکارێک ڕاهێنانێکی جوانی whileی تێدایە: ژمارە جووتەکان لە 0 تا 30 و کۆکەیان چاپ بکە، پاشان ژمارە تاکەکان و کۆکەیان. ژمارەیەک جووتە کاتێک دابەشکردنی بەسەر 2دا 0 بێت. % ئەو دەدات: 7 % 2 دەکاتە 1، 8 % 2 دەکاتە 0.
<?php
echo "Even numbers are: <br>";
displayEvenNumbers();
echo "<hr>";
echo "Odd numbers are: <br>";
displayOddNumbers();
function displayEvenNumbers() {
$count = 0;
$sum = 0;
while ($count <= 30) {
if ($count % 2 == 0) {
echo $count . "<br>";
$sum += $count;
}
$count++;
}
echo "Sum of even numbers = " . $sum;
}
function displayOddNumbers() {
$count = 0;
$sum = 0;
while ($count <= 30) {
if ($count % 2 == 1) {
echo $count . "<br>";
$sum += $count;
}
$count++;
}
echo "Sum of odd numbers = " . $sum;
}
?>Even numbers are: 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 Sum of even numbers = 240 ──────────── Odd numbers are: 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 Sum of odd numbers = 225
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Even numbers are: <br>0<br>2<br>4<br>6<br>8<br>10<br>12<br>14<br>16<br>18<br>20<br>22<br>24<br>26<br>28<br>30<br>Sum of even numbers = 240<hr>Odd numbers are: <br>1<br>3<br>5<br>7<br>9<br>11<br>13<br>15<br>17<br>19<br>21<br>23<br>25<br>27<br>29<br>Sum of odd numbers = 225Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3, 6
Call the two functions. They are defined lower in the file; PHP allows that.
بانگی دوو دەکات. لە خوارەوەی پێناسە کراون؛ PHP ڕێگەی پێدەدات.
- Lineدێڕی 8
functiongives a name to a block of code. You will learn this in the Functions lesson.functionناوێک دەدات بە . ئەمە لە وانەی کردارەکان فێر دەبیت.- Lineدێڕی 12
$count % 2 == 0: the number is even. 0 counts as even.$count % 2 == 0: ژمارەکە جووتە. 0 بە جووت دادەنرێت.- Lineدێڕی 14
$sum += $countadds the number to the sum.$sum += $countژمارەکە دەخاتە سەر کۆکە.- Lineدێڕی 25
% 2 == 1: the number is odd.% 2 == 1: ژمارەکە تاکە.
If you only need the two sums, one loop is enough. An if...else inside it decides where each number goes:
ئەگەر تەنها دوو کۆکەکەت پێویستە، یەک بەسە. if...elseێک لەناویدا بڕیار دەدات هەر ژمارەیەک بۆ کوێ بچێت:
<?php
$count = 0;
$evenSum = 0;
$oddSum = 0;
while ($count <= 30) {
if ($count % 2 == 0) {
$evenSum += $count; // 0, 2, 4, ... 30
} else {
$oddSum += $count; // 1, 3, 5, ... 29
}
$count++;
}
echo "Sum of even numbers = $evenSum<br>";
echo "Sum of odd numbers = $oddSum";
?>Sum of even numbers = 240 Sum of odd numbers = 225
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Sum of even numbers = 240<br>Sum of odd numbers = 225The do...while loopخولی do...while
The do...while loop will always run the block of code once. Then it checks the condition, and it repeats the loop while the condition is true.
هەمیشە یەک جار جێبەجێ دەکات. پاشان دەپشکنێت و مادام ڕاست بێت دووبارە دەکاتەوە.
do {
code to be executed;
} while (condition is true);When you cook soup, you always taste it once before you decide anything. Then: while it needs salt, add salt and taste again. The tasting happens at least once, even if the soup was perfect from the start. That is do...while.
کاتێک شۆربا لێدەنێیت، هەمیشە یەک جار تامی دەکەیت پێش ئەوەی بڕیار بدەیت. پاشان: مادام خوێی پێویستە، خوێ تێبکە و دیسان تامی بکە. تامکردن لانیکەم یەک جار ڕوودەدات، تەنانەت ئەگەر شۆرباکە لە سەرەتاوە تەواو بووبێت. ئەمە do...whileـە.
Where the analogy stops workingسنووری ئەم نموونەیە
In real life you may decide not to taste at all. A do...while loop has no choice: the first turn always happens.
لە ژیانی ڕاستەقینەدا لەوانەیە بڕیار بدەیت هەر تامی نەکەیت. بەڵام هیچ هەڵبژاردنێکی نییە: یەکەم هەمیشە ڕوودەدات.
Example: print 1 to 5
نموونە: چاپکردنی 1 تا 5
<?php
$x = 1;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>The number is: 1 The number is: 2 The number is: 3 The number is: 4 The number is: 5
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
The number is: 1 <br>The number is: 2 <br>The number is: 3 <br>The number is: 4 <br>The number is: 5 <br>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 4-7
The body runs first, with
$x = 1.ناوەڕۆکەکە سەرەتا جێبەجێ دەبێت، بە
$x = 1.- Lineدێڕی 5
$xinside double quotes is replaced by its value;<br>makes a new line in the browser.$xلەناو جووتەگوتەدا بە دەگۆڕدرێت؛<br>لە دێڕێکی نوێ دروست دەکات.- Lineدێڕی 7
After each turn: is
$xstill 5 or less? Note the semicolon.دوای هەر : ئایا
$xهێشتا 5 یان کەمترە؟ ئاگاداری بە.
The do...while loopخولی do...while
The body always runs at least onceناوەڕۆکەکە هەمیشە لانیکەم یەک جار جێبەجێ دەبێت
This practice question shows the difference clearly. $x starts at 10, so the condition $x <= 5 is false from the start. What is printed?
ئەم پرسیارەی ڕاهێنان جیاوازییەکە بە ڕوونی پیشان دەدات. $x لە 10ـەوە دەست پێدەکات، بۆیە $x <= 5 هەر لە سەرەتاوە هەڵەیە. چی چاپ دەبێت؟
<!DOCTYPE html>
<html>
<body>
<?php
$x = 10;
do {
echo $x;
$x++;
} while ($x <= 5);
echo "<br />" . $x;
?>
</body>
</html>10 11
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<!DOCTYPE html>
<html>
<body>
10<br />11
</body>
</html>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 8-11
The body runs once anyway: it prints 10 and makes
$x11.ناوەڕۆکەکە بە هەر حاڵ یەک جار جێبەجێ دەبێت: 10 چاپ دەکات و
$xدەکات بە 11.- Lineدێڕی 11
Now the check:
11 <= 5is false, so the loop ends.ئێستا پشکنین:
11 <= 5هەڵەیە، بۆیە کۆتایی دێت.- Lineدێڕی 13
Prints a line break and 11.
دێڕێکی نوێ و 11 چاپ دەکات.
Now the same numbers with a normal while loop. Here the check comes first, so the body never runs and $x stays 10:
ئێستا هەمان ژمارەکان بە ئاسایی. لێرەدا پشکنین سەرەتا دێت، بۆیە ناوەڕۆکەکە هەرگیز جێبەجێ نابێت و $x بە 10 دەمێنێتەوە:
<?php
$x = 10;
while ($x <= 5) {
echo $x;
$x++;
}
echo "<br />" . $x;
?>10
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<br />10Only the line break and 10: the loop body never ran.
تەنها دێڕێکی نوێ و 10: ناوەڕۆکی هەرگیز جێبەجێ نەبوو.
The for loopخولی for
A for loop is used when you know how many times the code should run. All the counting work is written in one line, in three parts:
بەکاردێت کاتێک دەزانیت کۆدەکە چەند جار دەبێت جێبەجێ بێت. هەموو کاری ژماردن لە یەک دێڕدا دەنووسرێت، بە سێ بەش:
for (init counter; test counter; increment counter) {
code to be executed;
}- init counter – gives the loop its first value. It runs once, before everything else.
- test counter – is checked before every turn. If it is
true, the loop continues. If it isfalse, the loop ends. - increment counter – changes the counter after every turn. Usually it increases it (
$i++), but it can also decrease it ($i--) or change it by any step ($i += 2).
- init counter – یەکەم دەداتە . یەک جار جێبەجێ دەبێت، پێش هەموو شتێک.
- test counter – پێش هەموو دەپشکنرێت. ئەگەر
trueبێت، بەردەوام دەبێت. ئەگەرfalseبێت، کۆتایی دێت. - increment counter – دوای هەموو دەگۆڕێت. زۆرجار زیادی دەکات (
$i++)، بەڵام دەتوانێت کەمیشی بکاتەوە ($i--) یان بە هەر هەنگاوێک بیگۆڕێت ($i += 2).
for loop and the order in which PHP runs them.سێ بەشەکەی و ئەو ڕیزبەندییەی PHP جێبەجێیان دەکات.CêITHow to create a function, send values in with arguments, and get a result back with return, with a short practice example.
چۆن دروست بکەیت، بە بنێریتە ناوی و بە return ئەنجامێک لێی وەربگریتەوە، لەگەڵ ڕاهێنانێکی کورت.
Plays 1:29:48–1:33:19: counting up and down. Stops before the form exercise.
لە 1:29:48 تا 1:33:19 پەخش دەبێت: ژماردن بەرەو سەر و بەرەو خوار؛ پێش ڕاهێنانەکەی ڕادەوەستێت.
The for loopخولی for
Example: print 0 to 10نموونە: چاپکردنی 0 تا 10
<?php
for ($count = 0; $count <= 10; $count++) {
echo $count . ", ";
}
?>0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
Start at 0; continue while
$count <= 10; add 1 after each turn.لە 0ـەوە دەست پێدەکات؛ مادام
$count <= 10بێت بەردەوام دەبێت؛ دوای هەر 1 زیاد دەکات.- Lineدێڕی 3
The body: print the number and a comma.
ناوەڕۆکەکە: ژمارەکە و فاریزەیەک چاپ دەکات.
This is the same output as the while example. Both loops do the same three jobs; for just puts them together in one line:
ئەمە هەمان نموونەکەی whileـە. هەردوو هەمان سێ کار دەکەن؛ for تەنها لە یەک دێڕدا کۆیان دەکاتەوە:
| Jobکار | while | for |
|---|---|---|
| 1. start the counter1. دەستپێکردنی | $count = 0; (before the loop) | for ($count = 0; ... |
| 2. test2. پشکنین | while ($count <= 10) | ...; $count <= 10; ... |
| 3. change the counter3. گۆڕینی | $count++; (last line of the body) | ...; $count++) |
The for loopخولی for
Counting down and counting in stepsژماردن بەرەو خوار و ژماردن بە هەنگاو
<?php
for ($i = 10; $i > 7; $i--) {
echo $i . " ";
}
echo "<br>";
for ($i = 0; $i <= 10; $i += 2) {
echo $i . " ";
}
?>10 9 8 0 2 4 6 8 10
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
10 9 8 <br>0 2 4 6 8 10Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
Start at 10 and decrease with
$i--() while$i > 7.لە 10ـەوە دەست پێدەکات و بە
$i--کەم دەکاتەوە () مادام$i > 7بێت.- Lineدێڕی 7
$i += 2adds 2 each time: the even numbers.$i += 2هەر جارێک 2 زیاد دەکات: ژمارە جووتەکان.
Each of the three parts may also be empty. Here the counter was created before the loop, so the first part is left empty (but its semicolon stays):
هەر یەکێک لە سێ بەشەکە دەتوانێت بەتاڵیش بێت. لێرەدا پێش دروست کراوە، بۆیە بەشی یەکەم بەتاڵ ماوەتەوە (بەڵام دەمێنێتەوە):
<?php
$i = 10;
for (; $i > 7; $i--) {
echo $i . " ";
}
?>10 9 8
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
10 9 8The for loopخولی for
What happens with commasبە فاریزە چی ڕوودەدات
<?php
for ($count = 0, $count <= 10, $count++) {
echo $count . ", ";
}
?>Parse error: syntax error, unexpected token ")", expecting ";" in C:\xampp\htdocs\loops\for-commas.php on line 2
The for loopخولی for
A loop inside a loopخولێک لەناو خولێکدا
The body of a loop can contain another loop. This is a . For every turn of the outer loop, the inner loop runs completely. Here the outer loop makes 3 rows, and the inner loop prints 4 numbers in each row: a small multiplication table.
ناوەڕۆکی دەتوانێت تر لەخۆ بگرێت. ئەمە . بۆ هەر دەرەوە، ناوەوە بە تەواوی جێبەجێ دەبێت. لێرەدا دەرەوە 3 ڕیز دروست دەکات و ناوەوە لە هەر ڕیزێکدا 4 ژمارە چاپ دەکات: خشتەیەکی بچووکی لێکدان.
<?php
for ($row = 1; $row <= 3; $row++) {
for ($col = 1; $col <= 4; $col++) {
echo $row * $col . " ";
}
echo "<br>";
}
?>1 2 3 4 2 4 6 8 3 6 9 12
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
1 2 3 4 <br>2 4 6 8 <br>3 6 9 12 <br>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
Outer loop:
$row= 1, 2, 3.دەرەوە:
$row= 1، 2، 3.- Lineدێڕی 3-5
Inner loop:
$col= 1 to 4 for each row.ناوەوە:
$col= 1 تا 4 بۆ هەر ڕیزێک.- Lineدێڕی 6
After the inner loop ends, start a new line.
دوای کۆتاییهاتنی ناوەوە، ڕیزێکی نوێ دەست پێدەکات.
Nested loops are used a lot with tables of data, for example . You will meet them again in the Multidimensional arrays lesson.
زۆر لەگەڵ خشتەی بەکاردێن، بۆ نموونە . لە وانەی ڕیزەکە فرەڕەهەندەکان دیسان دەیانبینیتەوە.
The foreach loopخولی foreach
The foreach loop is made for (lists of values). It loops through the array and takes each element in turn. You do not need a counter or a test: foreach starts at the first element and stops by itself after the last one.
بۆ (لیستی ) دروست کراوە. بە ناو دەڕوات و یەک لە دوای یەک وەردەگرێت. پێویستت بە یان پشکنین نییە: foreach لە یەکەم دەست پێدەکات و دوای دوایین خۆی دەوەستێت.
foreach ($array as $value) {
code to be executed;
}Read it like an English sentence: "For each item in $array, make the value available as $value." In every turn, the value of the current element is copied into $value, and then PHP moves on to the next element. You can choose any name instead of $value.
وەک قسەیەکی ئینگلیزی بیخوێنەوە: «بۆ هەر لە $arrayدا، لە $valueدا دابنێ.» لە هەموو ئێستا دەخرێتە ناو $valueـەوە، پاشان PHP دەچێتە سەر دواتر. دەتوانیت لە جیاتی $value هەر ناوێک هەڵبژێریت.
<?php
$array1 = array(1, 2, 3);
foreach ($array1 as $num) {
echo $num;
}
?>123
The foreach loopخولی foreach
Example: colours inside an HTML pageنموونە: ڕەنگەکان لەناو لاپەڕەیەکی HTMLـدا
<html>
<body>
<?php
$colors = array("Red", "Green", "Blue", "Yellow");
foreach ($colors as $value) {
echo "$value <br>";
}
?>
</body>
</html>Red Green Blue Yellow
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<html>
<body>
Red <br>Green <br>Blue <br>Yellow <br>
</body>
</html>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 5
array()creates an indexed array with four strings.array()بە چوار دروست دەکات.- Lineدێڕی 7
Four elements, so four turns. In turn 1
$valueis"Red", in turn 2"Green", …چوار ، کەواتە چوار . لە 1دا
$valueیەکسانە بە"Red"، لە 2دا"Green"، …- Lineدێڕی 8
Print the value and a line break.
و دێڕێکی نوێ چاپ دەکات.
foreach gives you the elements one by one, in order.foreach یەک لە دوای یەک و بە ڕیزبەندی دەداتە دەستت.CêITThe name after as is your choice. The summary notes write the same loop as foreach($colors as $color) and print "$color <br>"; the result is identical.
ئەو ناوەی دوای as دێت هەڵبژاردنی خۆتە. تێبینییەکانی پوختە هەمان بە foreach($colors as $color) دەنووسن و "$color <br>" چاپ دەکەن؛ ئەنجامەکە هەمان شتە.
The foreach loopخولی foreach
Keys and values: $key => $valueکلیل و نرخ: $key => $value
The lecture says that foreach goes through each key/value pair, but it shows only the value form. With => you get the too. In an indexed array the key is the (0, 1, 2, …); in an it is the name you gave:
وانەکە دەڵێت foreach بە ناو هەموو دەڕوات، بەڵام تەنها شێوەی پیشان دەدات. بە => وەردەگریت. لە (0، 1، 2، …)؛ لە ئەو ناوەیە کە خۆت داتناوە:
<?php
$ages = array("Aram" => 20, "Shilan" => 22, "Karwan" => 19);
foreach ($ages as $name => $age) {
echo "$name is $age years old.<br>";
}
$colors = array("Red", "Green", "Blue");
foreach ($colors as $index => $color) {
echo "Index $index: $color<br>";
}
?>Aram is 20 years old. Shilan is 22 years old. Karwan is 19 years old. Index 0: Red Index 1: Green Index 2: Blue
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Aram is 20 years old.<br>Shilan is 22 years old.<br>Karwan is 19 years old.<br>Index 0: Red<br>Index 1: Green<br>Index 2: Blue<br>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
An associative array: names are the keys, ages are the values.
: ناوەکان ، تەمەنەکان .
- Lineدێڕی 4
$namegets the key,$agegets the value.$nameوەردەگرێت،$age.- Lineدێڕی 10
In an indexed array the keys are 0, 1, 2.
لە 0، 1، 2ن.
The foreach loopخولی foreach
Two old statements about foreachدوو قسەی کۆن دەربارەی foreach
break and continuebreak و continue
Sometimes you want to stop a loop early, or skip just one turn. Two do this from inside the loop body: break stops the loop, and continue skips one turn.
هەندێک جار دەتەوێت زووتر ڕابگریت، یان تەنها یەک باز بدەیت. دوو ئەم کارە لە ناوەوەی دەکەن: break ڕادەگرێت و continue یەک بازدەدات.
break and continuebreak و continue
break: leave the loop right nowbreak: ئێستا لە خولەکە دەربچە
The break keyword ends the loop early. You put it inside the loop body, usually inside an if. When break runs, the loop stops at once and PHP continues after the loop.
break کە پێش وەخت کۆتایی پێدێنێت. دەیخەیتە ناو ناوەڕۆکی ، زۆرجار لەناو ifێکدا. کاتێک break جێبەجێ دەبێت، دەستبەجێ دەوەستێت و PHP لە دوای بەردەوام دەبێت.
In this example the condition $i == 3 becomes true when the counter reaches 3, and the loop ends:
لەم نموونەیەدا $i == 3 کاتێک ڕاست دەبێت کە دەگاتە 3 و کۆتایی دێت:
<?php
for ($i = 0; $i < 10; $i++) {
if ($i == 3) {
break;
}
echo "The value is $i <br>";
}
?>The value is 0 The value is 1 The value is 2
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
The value is 0 <br>The value is 1 <br>The value is 2 <br>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3-5
When
$iis 3,breakruns before theecho.کاتێک
$iیەکسانە بە 3،breakپێشechoجێبەجێ دەبێت.- Lineدێڕی 6
So 3 to 9 are never printed.
بۆیە 3 تا 9 هەرگیز چاپ نابن.
break works in every loop, not only in for. The same example with while:
break لە هەموو کار دەکات، نەک تەنها لە for. هەمان نموونە بە while:
<?php
$i = 0;
while ($i < 10) {
if ($i == 3) {
break;
}
echo "The value is $i <br>";
$i++;
}
?>The value is 0 The value is 1 The value is 2
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
The value is 0 <br>The value is 1 <br>The value is 2 <br>break and continuebreak و continue
continue: skip this turn, keep goingcontinue: ئەم خولانەوەیە بازبدە، بەردەوام بە
The continue keyword stops only the current turn of the loop, not the whole loop. The rest of the body is skipped for this turn, and the next turn starts.
continue کە تەنها ئێستا ڕادەگرێت، نەک هەموو . باقی ناوەڕۆکەکە بۆ ئەم بازی بەسەردا دەدرێت و داهاتوو دەست پێدەکات.
<?php
for ($i = 0; $i < 10; $i++) {
if ($i == 3) {
continue;
}
echo "The value is $i <br>";
}
?>The value is 0 The value is 1 The value is 2 The value is 4 The value is 5 The value is 6 The value is 7 The value is 8 The value is 9
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
The value is 0 <br>The value is 1 <br>The value is 2 <br>The value is 4 <br>The value is 5 <br>The value is 6 <br>The value is 7 <br>The value is 8 <br>The value is 9 <br>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3-5
When
$iis 3,continuejumps straight to the next turn ($i++, then the test).کاتێک
$iیەکسانە بە 3،continueڕاستەوخۆ بازدەدات بۆ داهاتوو ($i++، پاشان پشکنین).- Lineدێڕی 6
So only 3 is missing. All ten turns still happen.
بۆیە تەنها 3 دیار نییە. هەر دە هەر ڕوودەدەن.
break and continuebreak و continue
break or continue?break یان continue؟
break leaves the loop for good; continue skips only one turn.break بە یەکجاری لە دەردەچێت؛ continue تەنها یەک بازدەدات.CêIT| Keywordوشە | What happens when it runsکاتێک جێبەجێ دەبێت چی ڕوودەدات | Turns that still runچەند هێشتا جێبەجێ دەبن |
|---|---|---|
break | leaves the loop for goodبە یەکجاری لە دەردەچێت | none after itهیچ لە دوایەوە |
continue | skips the rest of this turn and jumps to the next turnباقی ئەم بازدەدات و دەچێتە داهاتوو | all the remaining turnsهەموو ماوەکان |
break and continuebreak و continue
A loop that only break can stopخولێک کە تەنها break دەتوانێت ڕایبگرێت
A practice note asks: what does this page show? while (true) would run forever, but its body is if (true) break;, so the loop ends in its very first turn. Nothing is printed, and there is no error. Note also that neither the loop nor the if has braces: each controls just the one statement after it.
تێبینییەکی ڕاهێنان دەپرسێت: ئەم پەڕەیە چی پیشان دەدات؟ while (true) بۆ هەمیشە بەردەوام دەبوو، بەڵام ناوەڕۆکەکەی if (true) break;ـە، بۆیە هەر لە یەکەم کۆتایی دێت. هیچ شتێک چاپ نابێت و هیچ هەڵەیەکیش نییە. هەروەها ئاگاداربە کە نە و نە if کەوانەی لوولییان نییە: هەر یەکەیان تەنها ئەو یەک کۆنترۆڵ دەکات کە لە دوایەوە دێت.
<!DOCTYPE html>
<html>
<body>
<?php
while (true)
if (true)
break;
?>
</body>
</html>HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<!DOCTYPE html>
<html>
<body>
</body>
</html>The page is empty: the loop ended before anything was printed.
پەڕەکە بەتاڵە: پێش چاپکردنی هەر شتێک کۆتایی هات.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 6
trueis always true: this loop has no normal end.trueهەمیشە ڕاستە: ئەم هیچ کۆتاییەکی ئاسایی نییە.- Lineدێڕی 7-8
The body: one
ifstatement, whose body is onebreakstatement.ناوەڕۆکەکە: یەک
if، کە ناوەڕۆکەکەی یەکbreakـە.
while (true) with a break inside is a real pattern. It is useful when the stop rule is easier to check in the middle of the body:
while (true) لەگەڵ breakێک لە ناویدا شێوازێکی ڕاستەقینەیە. بەسوودە کاتێک پشکنینی یاسای وەستان لە ناوەڕاستی ناوەڕۆکەکەدا ئاسانترە:
<?php
$n = 1;
while (true) {
if ($n > 3) {
break; // the only way out of this loop
}
echo "Turn $n<br>";
$n++;
}
echo "Finished";
?>Turn 1 Turn 2 Turn 3 Finished
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Turn 1<br>Turn 2<br>Turn 3<br>Finishedbreak and continuebreak و continue
Extra: break 2 and continue 2زیادە: break 2 و continue 2
Which loop should I use?کام خول بەکاربهێنم؟
| Loop | Checks the condition… دەپشکنێت… | Minimum turnsکەمترین | Best forباشترینە بۆ |
|---|---|---|---|
while | before each turnپێش هەر | 0 | repeating while something is true, when you do not know how many turnsدووبارەکردنەوە مادام شتێک ڕاستە، کاتێک نازانیت چەند |
do...while | after each turnدوای هەر | 1 | when the body must run at least onceکاتێک ناوەڕۆکەکە دەبێت لانیکەم یەک جار جێبەجێ بێت |
for | before each turnپێش هەر | 0 | counting: a known number of turnsژماردن: ژمارەیەکی زانراوی |
foreach | no condition: one turn per elementهیچ نییە: یەک بۆ هەر | 0 | going through every element of an arrayڕۆیشتن بە ناو هەموو |
Common mistakesهەڵە باوەکان
- Forgetting to change the counter in a
whileloop: an . See the warning. - Off by one:
$i <= 10runs one more turn than$i < 10. Count the turns by hand for the first and last values. - Commas instead of semicolons in the
forheader: parse error. See the example. - A semicolon right after the header, as in
for ($i = 0; $i < 3; $i++);orwhile ($x < 3);. The;is an empty body. Theforversion runs its empty body 3 times and then runs the block below only once; thewhileversion never ends, because nothing changes$x. - Forgetting the semicolon after
do...while:} while ($x <= 5);needs it. - Using
continuein awhileloop before the counter changes: an infinite loop. See the warning. - Giving
foreachsomething that is not an array: PHP shows a warning and skips the loop. See the example. - Curly quotes copied from slides: most loop examples in Lecture 3 and Lab 3 used
“ ”, which causes a parse error. All code here uses straight quotes. - The missing space:
echo "The number is" . $i . "<br>";prints The number is1, The number is2 … The dot joins the strings exactly as they are and never adds a space. (The lecture slide's screenshot shows a space that its code does not print.) Put the space inside the quotes,"The number is ", or write"The number is $i<br>".
- لەبیرکردنی گۆڕینی لە
whileدا: . سەیری بەشەکە بکە. - یەک زیاتر یان کەمتر:
$i <= 10یەک زیاتر لە$i < 10جێبەجێ دەکات. بۆ یەکەم و دوایین بە دەست بژمێرە. - فاریزە لە جیاتی لە سەرەوەی
forدا: . سەیری نموونەکە بکە. - ڕاستەوخۆ دوای سەرەوەی ، وەک
for ($i = 0; $i < 3; $i++);یانwhile ($x < 3);.;ـەکە ناوەڕۆکێکی بەتاڵە. وەشانیforناوەڕۆکە بەتاڵەکەی 3 جار جێبەجێ دەکات و پاشان بلۆکەکەی خوارەوە تەنها یەک جار؛ وەشانیwhileهەرگیز کۆتایی نایەت، چونکە هیچ شتێک$xناگۆڕێت. - لەبیرکردنی دوای
do...while:} while ($x <= 5);پێویستی پێیەتی. - بەکارهێنانی
continueلەwhileدا پێش گۆڕینی : . سەیری بەشەکە بکە. - دانی شتێک بە
foreachکە نییە: PHP پیشان دەدات و بازدەدات. سەیری نموونەکە بکە. - جووتەگوتەی خوار کە لە کۆپی کراوە: زۆربەی نموونەکانی لە وانەی 3 و تاقیگەی 3دا
“ ”یان بەکارهێنابوو، کە دەبێتە هۆی . هەموو کۆدەکانی ئێرە جووتەگوتەی ئاسایی بەکاردەهێنن. - :
echo "The number is" . $i . "<br>";دەنووسێت The number is1، The number is2 … خاڵەکە ڕێک وەک خۆیان پێکەوە دەلکێنێت و هەرگیز زیاد ناکات. (وێنەی وانەکە پیشان دەدات کە کۆدەکەی چاپی ناکات.) بخە ناو جووتەگوتەکانەوە،"The number is "، یان بنووسە"The number is $i<br>".
Lab exercisesڕاهێنانەکانی تاقیگە
These are the loop tasks of Lab 3 and the course's practice slide, rewritten clearly, plus a few extra exercises. Save them in a folder such as htdocs/loops and try each one before you open the answer.
ئەمانە ئەرکەکانی تاقیگەی 3 و ڕاهێنانی کۆرسەکەن، بە ڕوونی دووبارە نووسراونەتەوە، لەگەڵ چەند ڕاهێنانێکی زیادە. لە وەک htdocs/loops پاشەکەوتیان بکە و پێش کردنەوەی چارەسەر هەر یەکێکیان تاقی بکەرەوە.
Use a while loop to print the numbers 0 to 10, separated by commas. After the loop, print Count: and the final value of the counter on a new line.
بە ژمارەکانی 0 تا 10 بە فاریزە جیاکراوە چاپ بکە. دوای ، لەسەر دێڕێکی نوێ Count: و دوایین چاپ بکە.
Show answerوەڵامەکە پیشان بدە
<?php
$count = 0;
while ($count <= 10) {
echo $count . ", ";
$count++;
}
echo "<br /> Count: {$count}";
?>0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, Count: 11
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, <br /> Count: 11The final value is 11: the loop stops when 11 <= 10 is false.
دوایین 11ـە: کاتێک دەوەستێت کە 11 <= 10 هەڵە بێت.
Change the previous loop so that it prints the word FIVE instead of the number 5.
پێشوو بگۆڕە تا لە جیاتی ژمارە 5 وشەی FIVE چاپ بکات.
Show answerوەڵامەکە پیشان بدە
<?php
$count = 0;
while ($count <= 10) {
if ($count == 5) {
echo "FIVE, ";
} else {
echo $count . ", ";
}
$count++;
}
echo "<br /> Count: {$count}";
?>0, 1, 2, 3, 4, FIVE, 6, 7, 8, 9, 10, Count: 11
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
0, 1, 2, 3, 4, FIVE, 6, 7, 8, 9, 10, <br /> Count: 11If you print just "FIVE" (as the lab did), the output shows FIVE6.
ئەگەر تەنها "FIVE" چاپ بکەیت (وەک تاقیگەکە)، FIVE6 پیشان دەدات.
Print The number is: 1 up to The number is: 5, one per line, using a do...while loop.
بە ، لە The number is: 1 تا The number is: 5 چاپ بکە، هەر یەکەیان لەسەر دێڕێک.
Show answerوەڵامەکە پیشان بدە
<?php
$x = 1;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 5);
?>The number is: 1 The number is: 2 The number is: 3 The number is: 4 The number is: 5
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
The number is: 1 <br>The number is: 2 <br>The number is: 3 <br>The number is: 4 <br>The number is: 5 <br>Print 0 to 10 using a for loop.
بە 0 تا 10 چاپ بکە.
Show answerوەڵامەکە پیشان بدە
<?php
for ($count = 0; $count <= 10; $count++) {
echo $count . ", ";
}
?>0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,Store the colours red, green, blue and yellow in an array and print each one on its own line with foreach.
ڕەنگەکانی red و green و blue و yellow لە هەڵبگرە و بە foreach هەر یەکەیان لەسەر دێڕێکی جیا چاپ بکە.
Show answerوەڵامەکە پیشان بدە
<?php
$colors = array("red", "green", "blue", "yellow");
foreach ($colors as $value) {
echo "$value <br>";
}
?>red green blue yellow
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
red <br>green <br>blue <br>yellow <br>Write three small loops: (a) print 0 to 9; (b) print 0 to 9 but skip 3 with continue; (c) print 0 to 9 but stop at 3 with break.
سێ بچووک بنووسە: (أ) 0 تا 9 چاپ بکە؛ (ب) 0 تا 9 چاپ بکە بەڵام 3 بە continue بازبدە؛ (ج) 0 تا 9 چاپ بکە بەڵام لە 3 بە break بوەستە.
Hintئاماژە
Use the same for header three times; only the if inside changes.
هەمان سەرەوەی for سێ جار بەکاربهێنە؛ تەنها ifـەکەی ناوەوە دەگۆڕێت.
Show answerوەڵامەکە پیشان بدە
<?php
// a) print 0 to 9
for ($i = 0; $i <= 9; $i++) {
echo $i . " ";
}
echo "<br>";
// b) print 0 to 9, but skip 3 with continue
for ($i = 0; $i <= 9; $i++) {
if ($i == 3) {
continue;
}
echo $i . " ";
}
echo "<br>";
// c) print 0 to 9, but stop at 3 with break
for ($i = 0; $i <= 9; $i++) {
if ($i == 3) {
break;
}
echo $i . " ";
}
?>0 1 2 3 4 5 6 7 8 9 0 1 2 4 5 6 7 8 9 0 1 2
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
0 1 2 3 4 5 6 7 8 9 <br>0 1 2 4 5 6 7 8 9 <br>0 1 2What is the difference between break and continue?
جیاوازیی نێوان break و continue چییە؟
Show answerوەڵامەکە پیشان بدە
break ends the loop completely; continue skips only the current turn and the loop goes on with the next one.
break بە تەواوی کۆتایی پێدێنێت؛ continue تەنها ئێستا بازدەدات و لەگەڵ داهاتوو بەردەوام دەبێت.
Print all even numbers from 0 to 30 with their sum, then all odd numbers with their sum. Use while loops and the % operator.
هەموو ژمارە جووتەکانی 0 تا 30 لەگەڵ کۆکەیان چاپ بکە، پاشان هەموو ژمارە تاکەکان لەگەڵ کۆکەیان. و % بەکاربهێنە.
Show answerوەڵامەکە پیشان بدە
See the worked example. The sums are 240 (even) and 225 (odd).
سەیری نموونە چارەسەرکراوەکە بکە. کۆکەکان 240 (جووت) و 225 (تاک)ن.
Print the multiplication table of 7, from 7 x 1 = 7 to 7 x 10 = 70, one line each.
خشتەی لێکدانی 7 چاپ بکە، لە 7 x 1 = 7 تا 7 x 10 = 70، هەر یەکەیان لەسەر دێڕێک.
Show answerوەڵامەکە پیشان بدە
<?php
$n = 7;
for ($i = 1; $i <= 10; $i++) {
echo "$n x $i = " . ($n * $i) . "<br>";
}
?>7 x 1 = 7 7 x 2 = 14 7 x 3 = 21 7 x 4 = 28 7 x 5 = 35 7 x 6 = 42 7 x 7 = 49 7 x 8 = 56 7 x 9 = 63 7 x 10 = 70
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
7 x 1 = 7<br>7 x 2 = 14<br>7 x 3 = 21<br>7 x 4 = 28<br>7 x 5 = 35<br>7 x 6 = 42<br>7 x 7 = 49<br>7 x 8 = 56<br>7 x 9 = 63<br>7 x 10 = 70<br>Add up all whole numbers from 1 to 100 with a while loop and print the result.
بە هەموو 1 تا 100 کۆ بکەرەوە و ئەنجامەکە چاپ بکە.
Hintئاماژە
Keep a $sum variable that starts at 0, and add $i to it in every turn.
$sum دابنێ کە لە 0ـەوە دەست پێدەکات و لە هەموو $iی بخەرە سەر.
Show answerوەڵامەکە پیشان بدە
<?php
$i = 1;
$sum = 0;
while ($i <= 100) {
$sum += $i;
$i++;
}
echo "1 + 2 + ... + 100 = $sum";
?>1 + 2 + ... + 100 = 5050
Store five names in an array and print them one under the other with foreach.
پێنج ناو لە هەڵبگرە و بە foreach یەک لە ژێر یەکتری چاپیان بکە.
Show answerوەڵامەکە پیشان بدە
<?php
$names = array("Aram", "Shilan", "Karwan", "Dilan", "Rezan");
foreach ($names as $name) {
echo $name . "<br>";
}
?>Aram Shilan Karwan Dilan Rezan
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Aram<br>Shilan<br>Karwan<br>Dilan<br>Rezan<br>Summary and reviewپوختە و پێداچوونەوە
- A loop runs a block of code again and again, based on a condition.
whilechecks the condition before each turn; the body may run 0 times. Something in the body must change the condition.do...whileruns the body first and checks afterwards, so it always runs at least once. It ends withwhile (...);.for (init; test; increment)puts the three counter jobs in one line, separated by semicolons.foreach ($array as $value)andforeach ($array as $key => $value)go through every element of an array (or object).breakleaves the loop;continueskips to the next turn.
- بەپێی چەند جار دووبارە جێبەجێ دەکات.
whileپێش هەر دەپشکنێت؛ لەوانەیە ناوەڕۆکەکە 0 جار جێبەجێ بێت. شتێک لە ناوەڕۆکەکەدا دەبێت بگۆڕێت.do...whileسەرەتا ناوەڕۆکەکە جێبەجێ دەکات و پاشان دەپشکنێت، بۆیە هەمیشە لانیکەم یەک جار جێبەجێ دەبێت. بەwhile (...);کۆتایی دێت.for (init; test; increment)سێ کاری لە یەک دێڕدا کۆ دەکاتەوە، بە جیاکراوە.foreach ($array as $value)وforeach ($array as $key => $value)بە ناو هەموو (یان ) دەڕۆن.breakلە دەردەچێت؛continueبازدەدات بۆ داهاتوو.
What are the four loops in PHP?چوار خولەکەی PHP کامانەن؟
while, do...while, for and foreach.
while، do...while، for و foreach.
What is the value of $count after $count = 0; while ($count <= 10) { $count++; }?دوای $count = 0; while ($count <= 10) { $count++; } نرخی $count چەندە؟
11. The loop stops only when the test 11 <= 10 is false.
11. تەنها کاتێک دەوەستێت کە پشکنینی 11 <= 10 هەڵە بێت.
What is the main difference between while and do...while?جیاوازیی سەرەکیی نێوان while و do...while چییە؟
while checks before the body (0 or more turns); do...while checks after the body (1 or more turns).
while پێش ناوەڕۆکەکە دەپشکنێت (0 یان زیاتر )؛ do...while دوای ناوەڕۆکەکە دەپشکنێت (1 یان زیاتر ).
Name and explain the three parts of a for loop header.سێ بەشەکەی سەرەوەی خولی for ناو ببە و ڕوونیان بکەرەوە.
init counter (runs once, first), test counter (checked before every turn; false ends the loop), increment counter (runs after every turn). They are separated by semicolons.
init counter (یەک جار، یەکەم شت)، test counter (پێش هەموو دەپشکنرێت؛ ئەگەر false بێت کۆتایی دێت)، increment counter (دوای هەموو ). بە لە یەک جیا دەکرێنەوە.
Does foreach work only on arrays?ئایا foreach تەنها لەسەر ڕیزەک کار دەکات؟
No. It works on arrays and objects. For any other value PHP shows a warning.
نەخێر. لەسەر و کار دەکات. بۆ هەر تر PHP پیشان دەدات.
What is the difference between break and continue?جیاوازیی نێوان break و continue چییە؟
break ends the loop completely; continue skips only the current turn.
break بە تەواوی کۆتایی پێدێنێت؛ continue تەنها ئێستا بازدەدات.
What is an infinite loop, and how do you avoid it?خولی بێکۆتا چییە و چۆن خۆتی لێ دەپارێزیت؟
A loop whose condition never becomes false. Make sure the body changes the condition (for example $i++), also when continue is used.
کە هەرگیز ناگاتە false. دڵنیابە کە ناوەڕۆکەکە دەگۆڕێت (بۆ نموونە $i++)، تەنانەت کاتێک continue بەکاردێت.
Videoڤیدیۆ
Watch a videoسەیری ڤیدیۆیەک بکە
A video that viewers liked a lot and praised in their comments, chosen for this lesson.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.
One video for the whole lesson: while, do-while, for and foreach loops, plus break, continue, and how to avoid infinite loops.
یەک ڤیدیۆ بۆ هەموو وانەکە: while، do-while، for و foreach، لەگەڵ break و continue و چۆن خۆت لە بپارێزیت.
Overview of all four loops plus break and continue.
تێڕوانینێکی گشتی بۆ هەر چوار ، لەگەڵ break و continue.
Lesson complete!وانەکە تەواو بوو!
Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.
Key termsزاراوە سەرەکییەکان
Functionsکردارەکان
Related lecturesوانە پەیوەندیدارەکان
References and further readingسەرچاوەکان و خوێندنەوەی زیاتر
This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.