Chapterبەش 5 · Lectureوانە 5.8
Operatorsکارپێکەرەکان
Arithmetic, assignment, increment and decrement, comparison, logical, string and array operators, and the precedence rules that decide which one runs first. Every course example is run and corrected.
، ، و ، ، ، و و یاساکانی کە دیاری دەکەن کامیان یەکەم کار دەکات. هەموو نموونەیەکی کۆرسەکە تاقی کراوەتەوە و ڕاست کراوەتەوە.
What you will learnچی فێر دەبیت
- Name the parts of an expression: operators and operands.
- Calculate with
+ - * / % **and predict when the result is a float. - Use
=and the compound assignment operators such as+=and.=. - Explain the difference between
++$xand$x++. - Compare values with
==,===,!=,!==,<>,<,>,<=,>=and<=>, and know when to prefer===. - Combine conditions with
&&,||,!andxor, and avoid theand/orprecedence trap. - Use operator precedence and parentheses to predict the value of an expression.
- بەشەکانی بناسیت: و .
- بە
+ - * / % **هەژمار بکەیت و پێشبینی بکەیت کەی ئەنجامەکە . -
=و هاوشێوە لێکدراوەکانی، وەک+=و.=، بەکاربهێنیت. - جیاوازیی نێوان
++$xو$x++ڕوون بکەیتەوە. - بە
==،===،!=،!==،<>،<،>،<=،>=و<=>بەراورد بکەیت و بزانیت کەی===باشترە. - بە
&&،||،!وxorپێکەوە ببەستیت و خۆت لە تەڵەی پێشینەییand/orبپارێزیت. - بە و کەوانە پێشبینی بکەیت.
What operators doکارپێکەرەکان چی دەکەن
Programs calculate all the time: a shop adds prices, a school averages marks, a login page compares a typed password. In PHP you do this work with . An operator is a symbol, such as +, = or ==, that does something with one or more values. The values it works on are called . Operators and operands together form an , and every expression produces a value.
هەمیشە هەژمار دەکەن: دوکانێک کۆی پارەی کڕینەکان دەردەهێنێت، قوتابخانەیەک تێکڕای نمرەکان دەردەهێنێت، لاپەڕەی نووسراو بەراورد دەکات. لە PHPـدا ئەم کارانە بە دەکەیت. هێمایەکە، وەک +، = یان ==، کە کارێک لەسەر یەک یان چەند دەکات. ئەو کاریان لەسەر دەکرێت پێیان دەوترێت . و پێکەوە پێک دەهێنن و هەموو بەرهەم دەهێنێت.
$total = $price * 3;Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
*multiplies its two operands,$priceand3. Then=stores the result in$total.*دوو ($priceو3) لێک دەدات. پاشان=ئەنجامەکە لە$totalـدا هەڵدەگرێت.
A symbol that does something with one or more values, such as + (add), == (compare) or . (join strings). The values it works on are its .
هێمایەکە کە کارێک لەسەر یەک یان چەند نرخێک ئەنجام دەدات، وەک + (کۆکردنەوە)، == (بەراوردکردن) یان . (لکاندنی دەق). ئەو نرخانەی کاریان لەسەر دەکات پێیان دەگوترێت .
A value that an works on. In 5 * 2, the operands are 5 and 2.
ئەو نرخەیە کە کاری لەسەر دەکات. لە 5 * 2 دا، 5 و 2 کارپێکراون.
Any piece of code that produces a value, such as 5 + 3, $price * 2 or $age >= 18. Expressions are parts of .
هەر پارچە کۆدێکە کە بەرهەم دەهێنێت، وەک 5 + 3 یان $price * 2 یان $age >= 18. دەربڕینەکان بەشێکن لە .
Think of the calculator on a shopkeeper's counter in the bazaar. The numbers you type are the operands, and the keys +, -, × and ÷ are the operators. PHP has the same kind of keys, plus keys that store results, compare two things and make yes/no decisions.
بیر لە ئامێری حیسابی سەر مێزی دوکاندارێک لە بازاڕدا بکەرەوە. ئەو ژمارانەی دەیاننووسیت و دوگمەکانی +، -، × و ÷ . PHP هەمان جۆرە دوگمەی هەیە، لەگەڵ دوگمەی تر بۆ هەڵگرتنی ئەنجام، بەراوردکردنی دوو شت و بڕیاردانی بەڵێ/نەخێر.
Where the analogy stops workingسنووری ئەم نموونەیە
A calculator works left to right as you press the keys. PHP reads the whole expression first and follows fixed rules about which operator goes first.
ئامێری حیساب بە پێی ڕیزبەندیی پەنجەنانت لە چەپەوە بۆ ڕاست کار دەکات. PHP سەرەتا هەموو دەخوێنێتەوە و یاسای جێگیر پەیڕەو دەکات کە کام یەکەم کار دەکات.
| Familyخێزان | Operators | Sectionبەش |
|---|---|---|
| Arithmeticژمێرەیی | + - * / % ** | Arithmeticژمێرەیی |
| Assignment | = += -= *= /= %= .= | Assignmentبەشی دانانی نرخ |
| Increment / decrementزیاد و کەمکردن بە یەک | ++ -- | Increment and decrementزیادکردن و کەمکردنەوە |
| Comparisonبەراوردکردن | == === != <> !== < > <= >= <=> | Comparisonبەراوردکردن |
| Logicalلۆجیکی | && || ! and or xor | Logicalلۆجیکی |
| String | . .= | String and array operatorsکارپێکەرەکانی زنجیرەنووسە و ڕیزەک |
| Array | + == === != <> !== | String and array operatorsکارپێکەرەکانی زنجیرەنووسە و ڕیزەک |
Arithmetic operatorsکارپێکەرە ژمێرەییەکان
PHP's do school mathematics with numbers. The table uses $x = 10 and $y = 4.
بیرکاریی قوتابخانە لەسەر ژمارەکان دەکەن. ئەم خشتەیە $x = 10 و $y = 4 بەکاردەهێنێت.
| Operator | Nameناو | Exampleنموونە | Resultئەنجام |
|---|---|---|---|
+ | Additionکۆکردنەوە | $x + $y | 14 |
- | Subtractionلێدەرکردن | $x - $y | 6 |
* | Multiplicationلێکدان | $x * $y | 40 |
/ | Divisionدابەشکردن | $x / $y | 2.5 |
% | Modulus (remainder) (ئەوەی دوای دابەشکردن دەمێنێتەوە) | $x % $y | 2 |
** | Exponent (power)توان | $x ** $y | 10000 |
- | Negation (one operand)نەرێکردن (یەک ) | -$x | -10 |
/gives a whole number when the division is exact (10 / 5is2) and a decimal number () when it is not (10 / 4is2.5).%gives the remainder: 10 divided by 4 is 2, remainder 2. It is perfect for checking even and odd numbers:$n % 2is 0 for even numbers.**raises to a power:2 ** 3is 2 × 2 × 2 = 8.- Lab 2 writes
echo($x + $y);. The parentheses only group the expression;echois not a function, soecho $x + $y;works the same.
/کاتێک دابەشکردنەکە تەواو بێت دەدات (10 / 5دەکاتە2) و کاتێک تەواو نەبێت ژمارەیەکی دەیدەیی () دەدات (10 / 4دەکاتە2.5).%باقیی دابەشکردن دەدات: 10 دابەش 4 دەکاتە 2 و 2 دەمێنێتەوە. بۆ زانینی ژمارەی جووت و تاک زۆر گونجاوە:$n % 2بۆ ژمارەی جووت 0ـە.**توان دەکات:2 ** 3دەکاتە 2 × 2 × 2 = 8.- تاقیگەی 2 دەنووسێت
echo($x + $y);. کەوانەکان تەنها کۆ دەکەنەوە؛echoنییە، بۆیەecho $x + $y;ـیش هەمان کار دەکات.
The remainder left after dividing one whole number by another, given by the % operator. 10 % 3 is 1, and $n % 2 == 0 tests whether a number is even.
ئەو پاشماوەیەیە کە دوای دابەشکردنی بەسەر ژمارەیەکی تردا دەمێنێتەوە و کارپێکەری % دەیدات. 10 % 3 دەکاتە 1 و $n % 2 == 0 دەپشکنێت ئایا ژمارەکە جووتە.
<?php
echo 2 ** 3; // exponent: 2 × 2 × 2
echo "<br>";
echo 10 / 5; // exact division gives an integer
echo "<br>";
echo intdiv(10, 4); // whole-number division
echo "<br>";
echo -7 % 3; // the sign comes from the left number
echo "<br>";
echo 7 % -3;
echo "<br>";
$x = 5;
echo -$x; // negation
?>8 2 2 -1 1 -5
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
8<br>2<br>2<br>-1<br>1<br>-5Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
Exponent: 2 to the power 3.
توان: 2 بە توانی 3.
- Lineدێڕی 4
An exact division gives an integer.
دابەشکردنی تەواو دەدات.
- Lineدێڕی 6
intdiv()divides and drops the decimal part.intdiv()دابەش دەکات و بەشی دەیدەیی فڕێ دەدات.- Lineدێڕی 8-10
With
%, the sign of the result follows the left number.لە
%ـدا، نیشانەی سالب یان موجەبی ئەنجامەکە بە پێی ژمارەی لای چەپە.- Lineدێڕی 13
A minus sign before one value changes its sign.
نیشانەی کەم لە پێش یەک نیشانەکەی دەگۆڕێت.
How to create a function, send values in with arguments, and get a result back with return, with a short practice example.
چۆن دروست بکەیت، بە بنێریتە ناوی و بە return ئەنجامێک لێی وەربگریتەوە، لەگەڵ ڕاهێنانێکی کورت.
Plays only the 'arithmetic' chapter: operators, ++/--, precedence (28:02–34:38).
تەنها بەشی arithmetic پەخش دەبێت (28:02 تا 34:38): ، ++/-- و .
Arithmetic operatorsکارپێکەرە ژمێرەییەکان
The lecture's "Basic Math" exampleنموونەی "Basic Math"ی وانەکە
The lecture mixes HTML text and PHP. It calculates ((1 + 2 + $var1) * $var2) / 2 - 5 with $var1 = 3 and $var2 = 4:
وانەکە دەقی HTML و PHP تێکەڵ دەکات. ((1 + 2 + $var1) * $var2) / 2 - 5 هەژمار دەکات بە $var1 = 3 و $var2 = 4:
<?php
$var1 = 3;
$var2 = 4;
?>
Basic Math:
<?php echo ((1 + 2 + $var1) * $var2) / 2 - 5; ?>
<br />Basic Math: 7
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Basic Math:
7<br />Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2-3
Two variables, set in the first PHP block.
دوو ، لە یەکەم بلۆکی PHPـدا دانراون.
- Lineدێڕی 6
Brackets first, then
*and/, then-. The result is 7.سەرەتا کەوانەکان، پاشان
*و/، پاشان-. ئەنجامەکە 7ـە.
Arithmetic operatorsکارپێکەرە ژمێرەییەکان
Two things that stop a scriptدوو شت کە دەسنووسێک دەوەستێنن
Dividing by zero is not allowed. PHP 8 throws a DivisionByZeroError for both / and %:
دابەشکردن بە سفر ڕێگەپێدراو نییە. PHP 8 بۆ هەردوو / و % DivisionByZeroError دەدات:
<?php
$apples = 10;
$children = 0;
echo $apples / $children;
?>Fatal error: Uncaught DivisionByZeroError: Division by zero in C:\xampp\htdocs\divide.php:4
Stack trace:
#0 {main}
thrown in C:\xampp\htdocs\divide.php on line 4Arithmetic on strings works only when the string looks like a number. PHP converts "5" to 5 (). Text such as "abc" cannot be converted, and PHP 8 throws a TypeError:
هەژمارکردن لەسەر تەنها کاتێک کار دەکات کە لە ژمارە بچێت. PHP "5" دەکات بە 5 (). دەقێکی وەک "abc" ناگۆڕدرێت و PHP 8 TypeError دەدات:
<?php
echo "5" + "5";
echo "<br>";
echo "10" * "2";
echo "<br>";
echo "7" % "4";
echo "<br>";
echo "abc" + 1;
?>10
20
3
Fatal error: Uncaught TypeError: Unsupported operand types: string + int in C:\xampp\htdocs\numbers.php:8
Stack trace:
#0 {main}
thrown in C:\xampp\htdocs\numbers.php on line 8Each <br> is shown here as a new line.
لێرەدا هەر <br>ـێک وەک دێڕێکی نوێ پیشان دراوە.
Assignment operatorsکارپێکەرەکانی دانانی نرخ
= is the basic . It does not mean "is equal to". It means: work out the value on the right, then store it in the variable on the left. So $x = $x + 1; is not a broken equation; it means "take $x, add 1, and put the result back into $x".
= بنەڕەتیترین . واتای "یەکسانە بە" نییە. واتاکەی ئەمەیە: لای ڕاست هەژمار بکە، پاشان لە لای چەپدا هەڵیبگرە. کەواتە $x = $x + 1; هاوکێشەیەکی نادروست نییە؛ واتە "$x وەربگرە، یەکی بخە سەر و ئەنجامەکە بگەڕێنەوە ناو $x".
Storing a value in a variable with the =. The right side is worked out first, then the result is put into the variable on the left.
خەزنکردنی لە بە (=). سەرەتا لای ڕاست هەژمار دەکرێت، پاشان ئەنجامەکەی دەخرێتە ناو ئەو گۆڕاوەی لای چەپە.
A compound assignment operator does a calculation and the assignment in one step. The lecture shows +=: "you can add any value and change the value of the variable".
جۆرێکی لێکدراوی هەژمارکردن و بە یەک هەنگاو دەکات. وانەکە += پیشان دەدات: "دەتوانیت هەر زیاد بکەیت و بگۆڕیت".
<?php
$var1 = 3;
$var2 = 4;
?>
+=: <?php
$var2 += 4;
echo $var2;
?>+=: 8
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
+=: 8Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2-3
$var2starts at 4.$var2بە 4 دەست پێ دەکات.- Lineدێڕی 6
$var2 += 4;is short for$var2 = $var2 + 4;, so$var2becomes 8.$var2 += 4;کورتکراوەی$var2 = $var2 + 4;ـە، بۆیە$var2دەبێت بە 8.
| Operator | Exampleنموونە | Same asهەمان |
|---|---|---|
= | $x = 5; | store 5 in $x5 لە $xـدا هەڵبگرە |
+= | $x += 30; | $x = $x + 30; |
-= | $x -= 20; | $x = $x - 20; |
*= | $x *= 25; | $x = $x * 25; |
/= | $x /= 10; | $x = $x / 10; |
%= | $x %= 15; | $x = $x % 15; |
**= | $x **= 3; | $x = $x ** 3; |
.= | $s .= "!"; | $s = $s . "!"; |
<?php
echo "<pre>";
$x = 10;
$x /= 4;
var_dump($x);
$p = 2;
$p **= 3;
var_dump($p);
$greeting = "Hello";
$greeting .= ", Hana";
var_dump($greeting);
echo "</pre>";
?>float(2.5) int(8) string(11) "Hello, Hana"
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<pre>float(2.5)
int(8)
string(11) "Hello, Hana"
</pre>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3-4
/=can turn an integer into a float: 10 / 4 = 2.5./=دەتوانێت بکات بە : 10 / 4 = 2.5.- Lineدێڕی 7-8
**=: 2 to the power 3 = 8.**=: 2 بە توانی 3 = 8.- Lineدێڕی 11-12
.=adds text to the end of a string..=دەق دەخاتە کۆتایی .
Assignment operatorsکارپێکەرەکانی دانانی نرخ
An assignment is also a valueدانانی نرخ خۆی نرخێکیشە
An assignment is an expression too: its value is the value that was stored. That is why echo $a = 5; prints 5, why $b = $c = 0; sets both variables, and why the course notes' row $var = 4; print($var = 5); prints 5.
خۆی : هەمان ئەو کە هەڵگیرا. لەبەر ئەوەیە echo $a = 5; ژمارە 5 چاپ دەکات، $b = $c = 0; هەردوو دادەنێت و ڕیزەکەی تێبینییەکانی کۆرسەکە $var = 4; print($var = 5); ژمارە 5 چاپ دەکات.
<?php
echo $a = 5;
echo "<br>";
$b = $c = 0;
echo $b . $c;
echo "<br>";
$var = 4;
print($var = 5);
?>5 00 5
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
5<br>00<br>5Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
The assignment stores 5 and also gives 5 to
echo.5 هەڵدەگرێت و هەروەها 5 دەدات بە
echo.- Lineدێڕی 4
Right to left: first
$c = 0, then$bgets that value.لە ڕاستەوە بۆ چەپ: سەرەتا
$c = 0، پاشان$bهەمان وەردەگرێت.- Lineدێڕی 7-8
From the course notes: prints 5.
لە تێبینییەکانی کۆرسەکەوە: 5 چاپ دەکات.
Increment and decrementزیادکردن و کەمکردنەوە
Adding 1 to a variable is so common that PHP has a special operator for it: ++ (increment). -- (decrement) takes 1 away. $x++; does the same as $x += 1; and $x = $x + 1;. You will use this a lot in loops to count.
1 بۆ ئەوەندە باوە کە PHP تایبەتی بۆ هەیە: ++ (). -- () یەک کەم دەکاتەوە. $x++; هەمان کاری $x += 1; و $x = $x + 1; دەکات. ئەمە زۆر لە خولەکان بۆ ژماردن بەکاردەهێنیت.
Adding 1 to a variable with the ++ operator. $i++ does the same as $i = $i + 1 and is used a lot in loops.
زیادکردنی 1 بۆ سەر گۆڕاوێک بە کارپێکەری ++. $i++ وەک $i = $i + 1 وایە و زۆر لەناو بەکار دێت.
Subtracting 1 from a variable with the -- operator. $i-- does the same as $i = $i - 1.
کەمکردنەوەی 1 لە گۆڕاوێک بە کارپێکەری --. $i-- وەک $i = $i - 1 وایە.
<?php $var2 = 5; ?>
Increment: <?php $var2++; echo $var2; ?>
<br>
Decrement: <?php $var2--; echo $var2; ?>Increment: 6 Decrement: 5
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Increment: 6<br>
Decrement: 5Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
$var2starts at 5. This must be inside<?php ... ?>.$var2بە 5 دەست پێ دەکات. دەبێت لەناو<?php ... ?>ـدا بێت.- Lineدێڕی 2
Increment by 1: 5 becomes 6.
زیاد کردن بە 1: 5 دەبێت بە 6.
- Lineدێڕی 4
Decrement by 1: 6 becomes 5 again.
کەم کردنەوە بە 1: 6 دیسان دەبێتەوە بە 5.
Increment and decrementزیادکردن و کەمکردنەوە
Prefix or postfix: before or after?پێشگر و پاشگر: پێش یان دوا؟
You can write the operator before the variable (prefix: ++$x) or after it (postfix: $x++). On its own line there is no difference. The difference appears when the value is used at the same moment, for example inside echo:
دەتوانیت پێش بنووسیت (پێشگر: ++$x) یان دوای (پاشگر: $x++). ئەگەر لەسەر دێڕی خۆی بێت هیچ جیاوازییەک نییە. جیاوازییەکە کاتێک دەردەکەوێت کە لە هەمان ساتدا بەکاربهێنرێت، بۆ نموونە لەناو echoـدا:
| Codeکۆد | Nameناو | What happensچی ڕوودەدات |
|---|---|---|
++$x | Pre-incrementپێش- | Adds 1 first, then gives the new valueسەرەتا 1 زیاد دەکات، پاشان دەدات |
$x++ | Post-incrementپاش- | Gives the old value first, then adds 1سەرەتا دەدات، پاشان 1 زیاد دەکات |
--$x | Pre-decrementپێش- | Takes 1 away first, then gives the new valueسەرەتا 1 کەم دەکاتەوە، پاشان دەدات |
$x-- | Post-decrementپاش- | Gives the old value first, then takes 1 awayسەرەتا دەدات، پاشان 1 کەم دەکاتەوە |
The two positions of ++ or --. Prefix (++$i) changes the variable first and then gives the new value; postfix ($i++) gives the old value first and then changes the variable.
دوو شوێنی ++ یان --ـن. لە شێوەی پێشگردا (++$i) سەرەتا دەگۆڕێت و پاشان نرخە نوێیەکە دەدات؛ لە شێوەی پاشگردا ($i++) سەرەتا نرخە کۆنەکە دەدات و پاشان دەگۆڕێت.
Increment and decrementزیادکردن و کەمکردنەوە
The four forms in the course notesچوار شێوەکە لە تێبینییەکانی کۆرسەکەدا
The course notes show all four forms, each starting from $a = 8. We added <br> so that each pair of numbers (the value echo printed, then the variable afterwards) is on its own line:
تێبینییەکانی کۆرسەکە هەر چوار شێوەکە پیشان دەدەن، هەر یەکەیان بە $a = 8 دەست پێ دەکات. <br>مان زیاد کرد بۆ ئەوەی هەر جووتە ژمارەیەک ( echo چاپی کرد، پاشان دوای ئەوە) لەسەر دێڕی خۆی بێت:
<?php
$a = 8;
echo $a++; // Output: 8
echo $a; // Output: 9
echo "<br>";
$a = 8;
echo ++$a; // Output: 9
echo $a; // Output: 9
echo "<br>";
$a = 8;
echo $a--; // Output: 8
echo $a; // Output: 7
echo "<br>";
$a = 8;
echo --$a; // Output: 7
echo $a; // Output: 7
?>89 99 87 77
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
89<br>99<br>87<br>77Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2-4
Post-increment: prints the old value 8, then
$ais 9.پاش-: 8 چاپ دەکات، پاشان
$aدەبێت بە 9.- Lineدێڕی 7-9
Pre-increment:
$abecomes 9 first, so both prints show 9.پێش-: سەرەتا
$aدەبێت بە 9، بۆیە هەردوو چاپکردن 9 پیشان دەدەن.- Lineدێڕی 12-14
Post-decrement: prints 8, then
$ais 7.پاش-: 8 چاپ دەکات، پاشان
$aدەبێت بە 7.- Lineدێڕی 17-19
Pre-decrement:
$abecomes 7 first.پێش-: سەرەتا
$aدەبێت بە 7.
Comparison operatorsکارپێکەرەکانی بەراوردکردن
A compares two values and answers with a : true or false. These answers are what if statements and loops use to make decisions (see Conditional statements).
دوو بەراورد دەکات و وەڵامەکەی : true یان false. و ئەم وەڵامانە بۆ بڕیاردان بەکاردەهێنن (بڕوانە ڕستە مەرجییەکان).
An operator that compares two values and gives true or false: ==, !=, <, >, <= and >=. PHP also has === and !==, which check that the types are the same too.
کە دوو نرخ بەراورد دەکات و ڕاست یان هەڵە دەدات: ==، !=، <، >، <= و >=. PHP هەروەها === و !==ـی هەیە کە دەپشکنن جۆرەکانیش وەک یەک بن.
The lecture and the course summary list six of them under the heading "Boolean Operators": >, >=, <, <=, == and !=. Here is the complete list:
وانەکە و پوختەی کۆرسەکە شەشیان لە ژێر ناونیشانی "Boolean Operators"ـدا ڕیز دەکەن: >، >=، <، <=، == و !=. ئەمەش لیستە تەواوەکەیە:
| Operator | Nameناو | true when…کەی trueـە… | In the course list?لە لیستەکەی کۆرسدا؟ |
|---|---|---|---|
$a == $b | Equalیەکسان | the values are equal (after PHP converts types if needed) یەکسانن (ئەگەر پێویست بێت، PHP سەرەتا جۆرەکان دەگۆڕێت) | Yesبەڵێ |
$a === $b | Identicalوەک یەک | the values are equal and the types are the same یەکسانن و جۆرەکانیشیان وەک یەکن | No (added)نەخێر (زیاد کراوە) |
$a != $b | Not equalیەکسان نییە | the values are not equal یەکسان نین | Yesبەڵێ |
$a <> $b | Not equal (another spelling)یەکسان نییە (نووسینێکی تر) | same as !=وەک != | No (added)نەخێر (زیاد کراوە) |
$a !== $b | Not identicalوەک یەک نییە | the values or the types are different یان جۆرەکان جیاوازن | No (added)نەخێر (زیاد کراوە) |
$a < $b | Less thanبچووکترە لە | $a is smaller$a بچووکترە | Yesبەڵێ |
$a > $b | Greater thanگەورەترە لە | $a is bigger$a گەورەترە | Yesبەڵێ |
$a <= $b | Less than or equal toبچووکترە یان یەکسانە | $a is smaller or equal$a بچووکترە یان یەکسانە | Yesبەڵێ |
$a >= $b | Greater than or equal toگەورەترە یان یەکسانە | $a is bigger or equal$a گەورەترە یان یەکسانە | Yesبەڵێ |
$a <=> $b | Spaceshipکەشتیی ئاسمانی (spaceship) | not true/false: gives -1, 0 or 1true/false نادات: -1، 0 یان 1 دەدات | No (added)نەخێر (زیاد کراوە) |
<?php
$a = 5;
$b = "5";
echo "<pre>";
var_dump($a == $b);
var_dump($a === $b);
var_dump($a != $b);
var_dump($a <> $b);
var_dump($a !== $b);
var_dump($a > 3);
var_dump($a >= 5);
var_dump($a < 5);
var_dump($a <= 4);
echo "</pre>";
?>bool(true) bool(false) bool(false) bool(false) bool(true) bool(true) bool(true) bool(false) bool(false)
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<pre>bool(true)
bool(false)
bool(false)
bool(false)
bool(true)
bool(true)
bool(true)
bool(false)
bool(false)
</pre>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2-3
An integer 5 and a string
"5".5 و
"5".- Lineدێڕی 5
==: PHP turns"5"into 5, so they are equal.==: PHP"5"دەکات بە 5، بۆیە یەکسانن.- Lineدێڕی 6
===: an int and a string are different types, so false.===: int و string دوو جۆری جیاوازن، بۆیە false.- Lineدێڕی 7-8
!=and<>are the same operator.!=و<>هەمان .- Lineدێڕی 9
!==: different types, so true.!==: جۆری جیاواز، بۆیە true.
The comparison part explains == and ===, != and !==, and greater-than and less-than, with clear PHP 8 examples.
بەشی == و ===، != و !== و گەورەتر (>) و بچووکتر (<) ڕوون دەکاتەوە، بە نموونەی ڕوونی 8.
Watch from 7:39 to the end: the comparison operators chapter.
لە 7:39 تا کۆتایی سەیر بکە: بەشی .
Comparison operatorsکارپێکەرەکانی بەراوردکردن
== or ===?== یان ===؟
Imagine a 5,000-dinar note and a piece of paper with "5000" written on it. If you only ask "is the amount the same?" the answer is yes: that is ==. If you ask "is it the same amount and the same kind of thing?" the answer is no: that is ===.
وای دابنێ پارەیەکی 5,000 دیناری و پارچە کاغەزێکت هەیە کە "5000"ی لەسەر نووسراوە. ئەگەر تەنها بپرسیت "بڕەکەیان وەک یەکە؟" وەڵامەکە بەڵێیە: ئەمە ==ـە. ئەگەر بپرسیت "هەم بڕەکە و هەم جۆری شتەکە وەک یەکن؟" وەڵامەکە نەخێرە: ئەمە ===ـە.
Where the analogy stops workingسنووری ئەم نموونەیە
PHP's rules for converting types with == are more complicated than this, and sometimes surprising (see below).
یاساکانی PHP بۆ لە ==ـدا لەمە ئاڵۆزترن و هەندێک جار سەرسوڕهێنەرن (لە خوارەوە ببینە).
== converts types before comparing (). PHP 8 made these rules safer: a number and a non-numeric string are now compared as strings. Some results still surprise beginners:
== پێش بەراوردکردن جۆرەکان دەگۆڕێت (). PHP 8 ئەم یاسایانەی سەلامەتتر کرد: ئێستا ژمارەیەک و کە ژمارە نەبێت وەک بەراورد دەکرێن. هێشتا هەندێک ئەنجام سەری فێرخوازان سوڕ دەهێنن:
<?php
echo "<pre>";
var_dump(0 == "abc");
var_dump("1" == "01");
var_dump("10" == "1e1");
var_dump(null == false);
var_dump("abc" == "ABC");
var_dump(1.5 == "1.5");
echo "</pre>";
?>bool(false) bool(true) bool(true) bool(true) bool(false) bool(true)
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<pre>bool(false)
bool(true)
bool(true)
bool(true)
bool(false)
bool(true)
</pre>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
falsein PHP 8. (PHP 7 saidtruehere, a famous trap.)لە PHP 8دا
false. (PHP 7 لێرەدا دەیگوتtrue، تەڵەیەکی بەناوبانگ.)- Lineدێڕی 4-5
Two numeric strings are compared as numbers:
"01"is 1 and"1e1"is 10.دوو ژمارەیی وەک ژمارە بەراورد دەکرێن:
"01"دەکاتە 1 و"1e1"دەکاتە 10.- Lineدێڕی 6
nullandfalseare both "empty", so they are equal.nullوfalseهەردووکیان "بەتاڵ"ن، بۆیە یەکسانن.- Lineدێڕی 7
String comparison is .
بەراوردکردنی .
Comparison operatorsکارپێکەرەکانی بەراوردکردن
Comparing stringsبەراوردکردنی زنجیرەنووسەکان
Strings are compared character by character, like words in a dictionary. Capital and small letters are different characters. When both strings look like numbers, PHP compares them as numbers.
پیت بە پیت بەراورد دەکرێن، وەک وشەکان لە فەرهەنگدا. پیتی گەورە و بچووک دوو جیاوازن. کاتێک هەردوو لە ژمارە دەچن، PHP وەک ژمارە بەراوردیان دەکات.
<?php
echo "<pre>";
var_dump("No" == "no");
var_dump("apple" < "banana");
var_dump("10" == "10.0");
echo "</pre>";
?>bool(false) bool(true) bool(true)
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<pre>bool(false)
bool(true)
bool(true)
</pre>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
"No" and "no" are different, because capital and small letters are different characters.
"No" و "no" جیاوازن، چونکە پیتی گەورە و بچووک دوو جیاوازن.
- Lineدێڕی 4
Strings are compared letter by letter, like in a dictionary: "apple" comes before "banana".
پیت بە پیت بەراورد دەکرێن، وەک لە فەرهەنگدا: "apple" پێش "banana" دێت.
- Lineدێڕی 5
Two numeric strings are compared as numbers: 10 equals 10.0.
دوو ژمارەیی وەک ژمارە بەراورد دەکرێن: 10 یەکسانە بە 10.0.
Comparison operatorsکارپێکەرەکانی بەراوردکردن
The spaceship operator <=>کارپێکەری spaceship <=>
$a <=> $b gives -1 if $a is smaller, 0 if they are equal and 1 if $a is bigger. It is used mostly for sorting.
$a <=> $b ئەگەر $a بچووکتر بێت -1 دەدات، ئەگەر یەکسان بن 0 و ئەگەر $a گەورەتر بێت 1. زیاتر بۆ ڕیزکردن بەکاردێت.
<?php
echo 1 <=> 2;
echo "<br>";
echo 2 <=> 2;
echo "<br>";
echo 3 <=> 2;
echo "<br>";
echo "apple" <=> "banana";
?>-1 0 1 -1
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
-1<br>0<br>1<br>-1Logical operatorsکارپێکەرە لۆجیکییەکان
PHP's combine true/false values. You use them to join several into one, for example "the user is an adult and has a ticket".
true/false پێکەوە دەبەستن. بۆ پێکەوەبەستنی چەند لە یەکێکدا بەکاریان دەهێنیت، بۆ نموونە " گەورەیە و بلیتی هەیە".
An operator that combines true/false values: and, or and not. Python writes them as words (and, or, not); PHP, Java and C# use &&, || and ! (PHP also has and, or and xor with lower precedence).
کە نرخەکانی ڕاست و هەڵە پێکەوە دەبەستێت: and، or و not. Python بە وشە دەیاننووسێت (and، or، not)؛ PHP، Java و C# &&، || و ! بەکار دەهێنن (PHP هەروەها and، or و xorـی هەیە بە پێشینەیی نزمتر).
| Operator | Nameناو | true when…کەی trueـە… |
|---|---|---|
$a && $b | Andو | both are trueهەردووکیان ڕاستن |
$a and $b | And (low precedence)و (پێشینەیی نزم) | both are trueهەردووکیان ڕاستن |
$a || $b | Orیان | at least one is trueلانیکەم یەکێکیان ڕاستە |
$a or $b | Or (low precedence)یان (پێشینەیی نزم) | at least one is trueلانیکەم یەکێکیان ڕاستە |
$a xor $b | Exclusive orیان-ی تایبەت (xor) | exactly one is true, not bothتەنها یەکێکیان ڕاستە، نەک هەردووکیان |
!$a | Notنا | $a is false$a ناڕاستە |
$a | $b | $a && $b | $a || $b | $a xor $b | !$a |
|---|---|---|---|---|---|
true | true | true | true | false | false |
true | false | false | true | true | false |
false | true | false | true | true | true |
false | false | false | false | false | true |
AND, OR and NOT are also the of digital circuits, and they follow the same laws. Logic Design teaches those laws in Boolean Expressions and Laws.
AND، OR و NOT هەروەها بازنە دیجیتاڵەکانیشن و هەمان یاساکان پەیڕەو دەکەن. دیزاینی ئەو یاسایانە لە دەربڕینە بوولییەکان و یاساکانی جەبری بوول فێر دەکات.
<?php
$age = 20;
$hasTicket = true;
echo "<pre>";
var_dump($age >= 18 && $hasTicket);
var_dump($age < 18 || $hasTicket);
var_dump(!$hasTicket);
var_dump($age >= 18 and $hasTicket);
var_dump(true xor true);
var_dump(true xor false);
echo "</pre>";
?>bool(true) bool(true) bool(false) bool(true) bool(false) bool(true)
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<pre>bool(true)
bool(true)
bool(false)
bool(true)
bool(false)
bool(true)
</pre>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 5
Both parts are true, so
&&gives true.هەردوو بەش ڕاستن، بۆیە
&&ئەنجامی true دەدات.- Lineدێڕی 6
The first part is false, but
||needs only one true part.بەشی یەکەم ناڕاستە، بەڵام
||تەنها یەک بەشی ڕاستی پێویستە.- Lineدێڕی 7
!turns true into false.!true دەکات بە false.- Lineدێڕی 9-10
xoris true only when exactly one side is true.xorتەنها کاتێک ڕاستە کە ڕێک یەک لایەن ڕاست بێت.
Explains &&, || and !, the word forms and, or and xor, and how PHP stops checking early (short-circuit).
&&، || و ! ڕوون دەکاتەوە، هەروەها شێوە وشەییەکانی and، or و xor و چۆن زوو لە پشکنین دەوەستێت ().
Watch 3:17–7:55: logical operators and short-circuiting only.
لە 3:17 تا 7:55 سەیر بکە: تەنها و .
Logical operatorsکارپێکەرە لۆجیکییەکان
PHP stops as soon as it knows the answerPHP هەر کە وەڵامەکەی زانی دەوەستێت
With &&, if the left side is false the whole answer must be false, so PHP does not even look at the right side. With ||, if the left side is true PHP skips the right side. This is called . It lets you protect a risky test with a safe one placed first:
لە &&ـدا ئەگەر لای چەپ ناڕاست بێت، دەبێت وەڵامە گشتییەکە ناڕاست بێت، بۆیە PHP تەنانەت سەیری لای ڕاست ناکات. لە ||ـدا ئەگەر لای چەپ ڕاست بێت، PHP لای ڕاست تێدەپەڕێنێت. ئەمە پێی دەوترێت . ڕێگەت دەدات تاقیکردنەوەیەکی مەترسیدار بە تاقیکردنەوەیەکی سەلامەت بپارێزیت کە پێشتر دانراوە:
<?php
$a = 10;
$b = 0;
if ($b != 0 && $a / $b > 2) {
echo "Big ratio";
} else {
echo "Cannot divide by zero, so PHP skipped the second test.";
}
?>Cannot divide by zero, so PHP skipped the second test.
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Cannot divide by zero, so PHP skipped the second test.Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 4
$b != 0is false, so PHP never runs$a / $b, and no division by zero happens.$b != 0ناڕاستە، بۆیە PHP هەرگیز$a / $bجێبەجێ ناکات و هیچ دابەشکردنێک بە سفر ڕوونادات.
Logical operatorsکارپێکەرە لۆجیکییەکان
&& versus and&& بەرامبەر and
and and or do the same job as && and ||, but they have a much lower : lower even than =. This causes a classic bug:
and و or هەمان کاری && و || دەکەن، بەڵام زۆر نزمترە: تەنانەت لە =ـیش نزمترە. ئەمە بەناوبانگ دروست دەکات:
<?php
echo "<pre>";
$x = true and false;
var_dump($x);
$y = true && false;
var_dump($y);
$z = (true and false);
var_dump($z);
echo "</pre>";
?>bool(true) bool(false) bool(false)
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<pre>bool(true)
bool(false)
bool(false)
</pre>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
=runs beforeand: PHP reads($x = true) and false, so$xis true.=پێشandکار دەکات: PHP دەیخوێنێتەوە بە($x = true) and false، بۆیە$xدەبێت بە true.- Lineدێڕی 6
&&runs before=, so$ygetstrue && false, which is false.&&پێش=کار دەکات، بۆیە$yئەنجامیtrue && falseوەردەگرێت کە false ـە.- Lineدێڕی 9
Parentheses make
andwork first.کەوانەکان وا دەکەن
andیەکەم کار بکات.
You will use logical operators inside if statements in Conditional statements, with the lecture's examples ($a > $b) && ($c > $d) and !isset($a).
لەناو لە ڕستە مەرجییەکان بەکاردەهێنیت، لەگەڵ نموونەکانی وانەکە ($a > $b) && ($c > $d) و !isset($a).
String and array operatorsکارپێکەرەکانی زنجیرەنووسە و ڕیزەک
PHP has two operators: the dot . joins strings (), and .= adds a string to the end of a variable. They are explained with many examples in Strings.
PHP دوو هەیە: خاڵ . پێکەوە دەلکێنێت () و .= دەخاتە کۆتایی . بە نموونەی زۆرەوە لە زنجیرەنووسەکان ڕوون کراونەتەوە.
<?php
$first = "Karwan";
$last = "Omar";
$full = $first . " " . $last;
echo $full;
echo "<br>";
$full .= "!";
echo $full;
?>Karwan Omar Karwan Omar!
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Karwan Omar<br>Karwan Omar!Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 4
Three strings joined: first name, a space, last name.
سێ پێکەوە دەلکێنرێن: ناوی یەکەم، ، ناوی کۆتایی.
- Lineدێڕی 7
.=adds!to the end..=نیشانەی!دەخاتە کۆتاییەوە.
Some operators also work on . + makes the union of two arrays: it keeps every of the left array and adds only the keys of the right array that are new. == checks whether two arrays have the same ; === also requires the same order and the same types.
هەندێک لەسەر کار دەکەن. + یەکگرتنی دوو دروست دەکات: هەموو لای چەپ دەهێڵێتەوە و تەنها ئەو لای ڕاست زیاد دەکات کە نوێن. == دەپشکنێت ئایا دوو هەمان هەیە؛ === هەروەها هەمان ڕیزبەندی و هەمان جۆریشی پێویستە.
| Operator | Nameناو | Resultئەنجام |
|---|---|---|
$a + $b | Unionیەکگرتن | $a plus the keys of $b that $a does not have$a لەگەڵ ئەو $b کە لە $aـدا نین |
$a == $b | Equalیەکسان | true if they have the same key/value pairsڕاستە ئەگەر هەمان هەبێت |
$a === $b | Identicalوەک یەک | true if same pairs, same order and same typesڕاستە ئەگەر هەمان جووت، هەمان ڕیزبەندی و هەمان جۆریان هەبێت |
$a != $b | Not equalیەکسان نییە | opposite of ==پێچەوانەی == |
$a <> $b | Not equalیەکسان نییە | same as !=وەک != |
$a !== $b | Not identicalوەک یەک نییە | opposite of ===پێچەوانەی === |
<?php
$a = ["x" => 1, "y" => 2];
$b = ["y" => 20, "z" => 30];
$c = ["y" => 2, "x" => 1];
echo "<pre>";
print_r($a + $b);
var_dump($a == $c);
var_dump($a === $c);
var_dump($a != $b);
var_dump($a <> $b);
var_dump($a !== $c);
echo "</pre>";
?>Array
(
[x] => 1
[y] => 2
[z] => 30
)
bool(true)
bool(false)
bool(true)
bool(true)
bool(true)HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<pre>Array
(
[x] => 1
[y] => 2
[z] => 30
)
bool(true)
bool(false)
bool(true)
bool(true)
bool(true)
</pre>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 7
Union:
"y"exists in both, so the left value (2) is kept;"z"is new and is added.یەکگرتن:
"y"لە هەردووکیاندا هەیە، بۆیە لای چەپ (2) دەمێنێتەوە؛"z"نوێیە و زیاد دەکرێت.- Lineدێڕی 8
Same pairs in a different order:
==is true.هەمان جووت بە ڕیزبەندییەکی جیاواز:
==ڕاستە.- Lineدێڕی 9
===also checks the order, so it is false.===ڕیزبەندیش دەپشکنێت، بۆیە ناڕاستە.
Two shortcut operators: ?: and ??دوو کارپێکەری کورتکراوە: ?: و ??
The condition ? a : b gives a when the condition is true and b when it is false. The $x ?? "default" gives $x if it exists and is not null, otherwise the default. Both are short forms of an if ... else and are explained further in Conditional statements.
condition ? a : b کاتێک ڕاست بێت a دەدات و کاتێک ناڕاست بێت b. $x ?? "default" ئەگەر $x هەبێت و null نەبێت $x دەدات، ئەگەر نا . هەردووکیان شێوەی کورتی if ... elseن و زیاتر لە ڕستە مەرجییەکان ڕوون دەکرێنەوە.
<?php
$age = 16;
echo $age >= 18 ? "Adult" : "Minor";
echo "<br>";
$name = null;
echo $name ?? "Guest";
?>Minor Guest
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Minor<br>GuestExplain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
16 >= 18 is false, so the second value
"Minor"is printed.16 >= 18 ناڕاستە، بۆیە دووەم
"Minor"چاپ دەکرێت.- Lineدێڕی 6
$nameis null, so the default"Guest"is used.$nameبریتییە لەnull، بۆیە"Guest"بەکاردێت.
Operator precedence: who goes first?پێشینەیی کارپێکەرەکان: کێ یەکەم کار دەکات؟
When an expression has several operators, PHP does not simply work from left to right. Each operator has a rank called its . Higher-ranked operators run first, just like in school maths, where multiplication comes before addition: 2 + 3 * 4 is 14, not 20. When two operators have the same rank, their associativity decides: most go left to right, but ** and the assignment operators go right to left.
کاتێک چەند تێدایە، PHP بە سادەیی لە چەپەوە بۆ ڕاست کار ناکات. هەر پلەیەکی هەیە کە پێی دەوترێت . ئەو پلەیان بەرزترە یەکەم کار دەکەن، ڕێک وەک بیرکاریی قوتابخانە کە لێکدان پێش کۆکردنەوە دێت: 2 + 3 * 4 دەکاتە 14، نەک 20. کاتێک دوو هەمان پلەیان هەبێت، ئاراستەی جێبەجێکردن بڕیار دەدات: زۆربەیان لە چەپەوە بۆ ڕاست دەڕۆن، بەڵام ** و لە ڕاستەوە بۆ چەپ.
The rules that decide which operator runs first when an expression has several. * and / come before + and -, so 2 + 3 * 4 is 14; use parentheses to change the order.
ئەو یاسایانەن کە دیاری دەکەن کام یەکەم جێبەجێ دەکرێت کاتێک چەند کارپێکەرێکی تێدایە. * و / پێش + و - دێن، بۆیە 2 + 3 * 4 دەکاتە 14؛ بۆ گۆڕینی ڕیزبەندییەکە کەوانە بەکاربهێنە.
| Operators (highest first) (بەرزترین لە سەرەوە) | Meaningواتا | Directionئاراستە | Exampleنموونە |
|---|---|---|---|
** | exponentتوان | right to leftلە ڕاستەوە بۆ چەپ | 2 ** 3 ** 2 → 512 |
++ -- - (int) … | increment, decrement, negation, type casts، ، نەرێکردن، | — | -3 ** 2 → -9 |
! | logical notنای لۆجیکی | — | !true → false |
* / % | multiply, divide, remainderلێکدان، دابەشکردن، | left to rightلە چەپەوە بۆ ڕاست | 100 / 10 / 2 → 5 |
+ - | add, subtractکۆکردنەوە، لێدەرکردن | left to rightلە چەپەوە بۆ ڕاست | 10 - 4 - 3 → 3 |
. | string concatenation | left to rightلە چەپەوە بۆ ڕاست | "Sum: " . 1 + 2 → "Sum: 3" |
< <= > >= | comparisonبەراوردکردن | cannot be chainedناکرێت بە زنجیر بنووسرێن | 5 + 3 > 7 → true |
== != === !== <> <=> | equalityیەکسانی | cannot be chainedناکرێت بە زنجیر بنووسرێن | — |
&& | logical andو-ی لۆجیکی | left to rightلە چەپەوە بۆ ڕاست | — |
|| | logical orیان-ی لۆجیکی | left to rightلە چەپەوە بۆ ڕاست | — |
?? | null coalescing | right to leftلە ڕاستەوە بۆ چەپ | — |
? : | ternary | cannot be chained without bracketsبەبێ کەوانە ناکرێت بە زنجیر بنووسرێت | — |
= += -= *= /= .= %= **= ??= | assignment | right to leftلە ڕاستەوە بۆ چەپ | $b = $c = 0 |
and | logical and (low)و-ی لۆجیکی (نزم) | left to rightلە چەپەوە بۆ ڕاست | — |
xor | logical xorxor-ی لۆجیکی | left to rightلە چەپەوە بۆ ڕاست | — |
or | logical or (low)یان-ی لۆجیکی (نزم) | left to rightلە چەپەوە بۆ ڕاست | — |
<?php
echo 2 + 3 * 4;
echo "<br>";
echo (2 + 3) * 4;
echo "<br>";
echo 10 - 4 - 3;
echo "<br>";
echo 2 ** 3 ** 2;
echo "<br>";
echo -3 ** 2;
echo "<br>";
echo "Sum: " . 1 + 2;
echo "<br>";
var_dump(5 + 3 > 7);
?>14 20 3 512 -9 Sum: 3 bool(true)
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
14<br>20<br>3<br>512<br>-9<br>Sum: 3<br>bool(true)Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
*before+: 2 + 12 = 14.*پێش+: 2 + 12 = 14.- Lineدێڕی 4
Brackets first: 5 * 4 = 20.
سەرەتا کەوانەکان: 5 * 4 = 20.
- Lineدێڕی 6
Same rank, left to right: (10 - 4) - 3 = 3.
هەمان پلە، لە چەپەوە بۆ ڕاست: (10 - 4) - 3 = 3.
- Lineدێڕی 8
**goes right to left: 2 (3 2) = 2 ** 9 = 512.**لە ڕاستەوە بۆ چەپ دەڕوات: 2 (3 2) = 2 ** 9 = 512.- Lineدێڕی 10
**is stronger than the minus sign: -(3 ** 2) = -9.**لە نیشانەی کەم بەهێزترە: -(3 ** 2) = -9.- Lineدێڕی 12
In PHP 8,
+runs before.: "Sum: " . (1 + 2).لە PHP 8دا
+پێش.کار دەکات: "Sum: " . (1 + 2).- Lineدێڕی 14
Arithmetic runs before comparison: (5 + 3) > 7.
هەژمارکردنی ژمێرەیی پێش بەراوردکردن کار دەکات: (5 + 3) > 7.
Common mistakesئەو هەڵانەی زۆر دەکرێن
=instead of==.if ($x = 5)stores 5 and is always true. Compare with==or===.==when you meant===.5 == "5"is true. Use===when the type matters.- A missing
$.* var2makes PHP look for a constant and stop. Every variable needs its$. $--aor$++a. The operator goes before the$:--$a,++$a.- Forgetting that postfix gives the old value.
echo $x++;prints the value before adding 1. - Expecting
/to give a whole number.10 / 4is 2.5. Useintdiv()for whole-number division. - Dividing by zero.
/ 0and% 0stop the script. Check the divisor first, for example with$b != 0 && .... - Using
%with decimals.%works on integers;7.5 % 2loses the.5(and PHP shows a deprecation notice). Usefmod(7.5, 2)for decimals. - Writing
18 <= $age <= 30. Comparisons cannot be chained. Write18 <= $age && $age <= 30. and/orwith=.$x = true and false;sets$xto true. Use&&/||or parentheses.- Using
+to join strings. That is the dot:"a" . "b".
=لە جیاتی==.if ($x = 5)ژمارە 5 هەڵدەگرێت و هەمیشە ڕاستە. بە==یان===بەراورد بکە.==کاتێک مەبەستت===بوو.5 == "5"ڕاستە. کاتێک جۆرەکە گرنگە===بەکاربهێنە.$ـی ونبوو.* var2وا دەکات PHP بەدوای بگەڕێت و بوەستێت. هەموو$ـی خۆی پێویستە.$--aیان$++a. پێش$دێت:--$a،++$a.- لەبیرچوونی ئەوەی پاشگر دەدات.
echo $x++;پێش ئەوەی 1 زیاد بکات چاپ دەکات. - چاوەڕوانی لە
/.10 / 4دەکاتە 2.5. بۆ دابەشکردنی تەواوintdiv()بەکاربهێنە. - دابەشکردن بە سفر.
/ 0و% 0دەوەستێنن. سەرەتا دابەشکەرەکە بپشکنە، بۆ نموونە بە$b != 0 && .... - بەکارهێنانی
%لەگەڵ ژمارەی دەیدەیی.%لەسەر کار دەکات؛7.5 % 2بەشی.5لەدەست دەدات (و PHP ئاگادارییەکی پیشان دەدات). بۆ ژمارەی دەیدەییfmod(7.5, 2)بەکاربهێنە. - نووسینی
18 <= $age <= 30. بەراوردەکان ناکرێت بە زنجیر بنووسرێن. بنووسە18 <= $age && $age <= 30. and/orلەگەڵ=.$x = true and false;$xدەکات بە true.&&/||یان کەوانە بەکاربهێنە.- بەکارهێنانی
+بۆ لکاندنی . ئەوە کاری خاڵە:"a" . "b".
Exercises and lab tasksڕاهێنان و ئەرکەکانی تاقیگە
Predict every output on paper first, then type the code and run it with XAMPP.
سەرەتا هەموو لەسەر کاغەز پێشبینی بکە، پاشان کۆدەکە بنووسە و بە XAMPP کاری پێ بکە.
Predict the five numbers this program prints. Then change $y to 3 and predict again.
ئەو پێنج ژمارەیەی ئەم چاپیان دەکات پێشبینی بکە. پاشان $y بگۆڕە بۆ 3 و دیسان پێشبینی بکە.
<?php
$x = 10;
$y = 4;
echo($x + $y);
echo "<br>";
echo($x - $y);
echo "<br>";
echo($x * $y);
echo "<br>";
echo($x / $y);
echo "<br>";
echo($x % $y);
?>Hintئاماژە
/ can give a decimal number. % gives what is left after dividing.
/ دەتوانێت ژمارەی دەیدەیی بدات. % ئەوە دەدات کە دوای دابەشکردن دەمێنێتەوە.
Show answerوەڵامەکە پیشان بدە
<?php
$x = 10;
$y = 4;
echo($x + $y);
echo "<br>";
echo($x - $y);
echo "<br>";
echo($x * $y);
echo "<br>";
echo($x / $y);
echo "<br>";
echo($x % $y);
?>14 6 40 2.5 2
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
14<br>6<br>40<br>2.5<br>2Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 10
10 / 4 = 2.5 (a float).
10 / 4 دەکاتە 2.5 ().
- Lineدێڕی 12
10 = 2 × 4 + 2, so the remainder is 2.
10 = 2 × 4 + 2، بۆیە 2ـە.
With $y = 3:
بە $y = 3:
<?php
$x = 10;
$y = 3;
echo($x + $y);
echo "<br>";
echo($x - $y);
echo "<br>";
echo($x * $y);
echo "<br>";
echo($x / $y);
echo "<br>";
echo($x % $y);
?>13 7 30 3.3333333333333 1
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
13<br>7<br>30<br>3.3333333333333<br>1Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 10
10 / 3 never ends; PHP prints 14 significant digits.
10 / 3 هەرگیز کۆتایی نایەت؛ PHP 14 ژمارەی گرنگ چاپ دەکات.
- Lineدێڕی 12
10 = 3 × 3 + 1, so the remainder is 1.
10 = 3 × 3 + 1، بۆیە 1ـە.
Predict the six numbers. Then rewrite every compound operator in its long form (for example $x += 30; becomes $x = $x + 30;) and check that the output does not change.
شەش ژمارەکە پێشبینی بکە. پاشان هەموو لێکدراو بە شێوە درێژەکەی بنووسەرەوە (بۆ نموونە $x += 30; دەبێت بە $x = $x + 30;) و دڵنیا ببەوە کە ناگۆڕێت.
<?php
$x = 10;
echo $x;
echo "<br>";
$x = 20;
$x += 30;
echo $x;
echo "<br>";
$x = 50;
$x -= 20;
echo $x;
echo "<br>";
$x = 5;
$x *= 25;
echo $x;
echo "<br>";
$x = 50;
$x /= 10;
echo $x;
echo "<br>";
$x = 100;
$x %= 15;
echo $x;
?>Show answerوەڵامەکە پیشان بدە
<?php
$x = 10;
echo $x;
echo "<br>";
$x = 20;
$x += 30;
echo $x;
echo "<br>";
$x = 50;
$x -= 20;
echo $x;
echo "<br>";
$x = 5;
$x *= 25;
echo $x;
echo "<br>";
$x = 50;
$x /= 10;
echo $x;
echo "<br>";
$x = 100;
$x %= 15;
echo $x;
?>10 50 30 125 5 10
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
10<br>50<br>30<br>125<br>5<br>10Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 7
20 + 30 = 50
20 + 30 دەکاتە 50
- Lineدێڕی 12
50 - 20 = 30
50 - 20 دەکاتە 30
- Lineدێڕی 17
5 × 25 = 125
5 × 25 دەکاتە 125
- Lineدێڕی 22
50 / 10 = 5 (exact, so an integer)
50 / 10 دەکاتە 5 (تەواوە، بۆیە )
- Lineدێڕی 27
100 = 6 × 15 + 10, remainder 10
100 = 6 × 15 + 10، 10
The long form gives exactly the same output:
شێوە درێژەکە ڕێک هەمان دەدات:
<?php
$x = 10;
echo $x;
echo "<br>";
$x = 20;
$x = $x + 30;
echo $x;
echo "<br>";
$x = 50;
$x = $x - 20;
echo $x;
echo "<br>";
$x = 5;
$x = $x * 25;
echo $x;
echo "<br>";
$x = 50;
$x = $x / 10;
echo $x;
echo "<br>";
$x = 100;
$x = $x % 15;
echo $x;
?>10 50 30 125 5 10
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
10<br>50<br>30<br>125<br>5<br>10Each block starts again with $x = 10. Predict the eight numbers. Which blocks print the old value first?
هەر بلۆکێک دیسان بە $x = 10 دەست پێ دەکات. هەشت ژمارەکە پێشبینی بکە. کام بلۆکانە سەرەتا چاپ دەکەن؟
<?php
$x = 10;
echo ++$x;
echo "<br>";
echo $x;
echo "<hr>";
$x = 10;
echo $x++;
echo "<br>";
echo $x;
echo "<hr>";
$x = 10;
echo --$x;
echo "<br>";
echo $x;
echo "<hr>";
$x = 10;
echo $x--;
echo "<br>";
echo $x;
?>Hintئاماژە
Look where the ++ or -- is: before the variable (change first) or after it (use first).
سەیر بکە ++ یان -- لە کوێیە: پێش (سەرەتا گۆڕین) یان دوای (سەرەتا بەکارهێنان).
Show answerوەڵامەکە پیشان بدە
<?php
$x = 10;
echo ++$x;
echo "<br>";
echo $x;
echo "<hr>";
$x = 10;
echo $x++;
echo "<br>";
echo $x;
echo "<hr>";
$x = 10;
echo --$x;
echo "<br>";
echo $x;
echo "<hr>";
$x = 10;
echo $x--;
echo "<br>";
echo $x;
?>11 11 ────────── 10 11 ────────── 9 9 ────────── 10 9
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
11<br>11<hr>10<br>11<hr>9<br>9<hr>10<br>9Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
Pre-increment: 11, then
$xis 11.پێش-: 11، پاشان
$xدەکاتە 11.- Lineدێڕی 9
Post-increment: prints the old value 10, then
$xis 11.پاش-: 10 چاپ دەکات، پاشان
$xدەکاتە 11.- Lineدێڕی 15
Pre-decrement: 9, then
$xis 9.پێش-: 9، پاشان
$xدەکاتە 9.- Lineدێڕی 21
Post-decrement: prints 10, then
$xis 9.پاش-: 10 چاپ دەکات، پاشان
$xدەکاتە 9.
The second and fourth blocks (postfix $x++ and $x--) print the old value first. After every block the variable has changed by exactly 1.
بلۆکی دووەم و چوارەم (پاشگری $x++ و $x--) سەرەتا چاپ دەکەن. دوای هەر بلۆکێک ڕێک بە 1 گۆڕاوە.
This line from the course notes does not run: echo $--a;. Fix it so that it prints 7 when $a is 8, and explain why the original is wrong.
ئەم دێڕەی تێبینییەکانی کۆرسەکە کار ناکات: echo $--a;. چاکی بکەرەوە بۆ ئەوەی کاتێک $a یەکسان بێت بە 8 ژمارە 7 چاپ بکات و ڕوونی بکەرەوە بۆچی ئەسڵەکەی نادروستە.
Show answerوەڵامەکە پیشان بدە
<?php
$a = 8;
echo --$a;
echo "<br>";
echo $a;
?>7 7
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
7<br>7-- must be written before the whole variable name, including the $. Pre-decrement (--$a) takes 1 away first, so echo prints 7.
-- دەبێت پێش هەموو ناوی بنووسرێت، $ـیشی لەگەڵدا. پێش- (--$a) سەرەتا 1 کەم دەکاتەوە، بۆیە echo ژمارە 7 چاپ دەکات.
Exercises and lab tasksڕاهێنان و ئەرکەکانی تاقیگە
More practiceڕاهێنانی زیاتر
Write a program that prints whether the number in $n is even or odd. Test it with 7 and with 10.
بنووسە کە چاپ بکات ئایا ژمارەکەی ناو $n جووتە یان تاک. بە 7 و 10 تاقی بکەرەوە.
Hintئاماژە
An even number divided by 2 leaves no remainder.
ژمارەی جووت کاتێک دابەش 2 دەکرێت هیچی لێ نامێنێتەوە.
Show answerوەڵامەکە پیشان بدە
<?php
$n = 7;
if ($n % 2 == 0) {
echo "$n is even";
} else {
echo "$n is odd";
}
?>7 is odd
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
7 is oddExplain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
$n % 2is 0 for even numbers and 1 for odd ones.$n % 2بۆ ژمارەی جووت 0ـە و بۆ تاک 1ـە.
With $n = 10 the program prints 10 is even.
بە $n = 10 10 is even چاپ دەکات.
Karwan buys 3 notebooks at 1,500 dinars and 2 pens at 750 dinars. The shop gives a 10% discount. Use += and *= to calculate the total before and after the discount.
کاروان 3 دەفتەر بە 1,500 دینار و 2 پێنووس بە 750 دینار دەکڕێت. دوکانەکە 10% داشکاندن دەکات. بە += و *= کۆی گشتی پێش و دوای داشکاندن هەژمار بکە.
Show answerوەڵامەکە پیشان بدە
<?php
$total = 0;
$total += 3 * 1500; // three notebooks
$total += 2 * 750; // two pens
echo "Before discount: $total dinars<br>";
$total *= 0.9; // 10% discount
echo "After discount: $total dinars";
?>Before discount: 6000 dinars After discount: 5400 dinars
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Before discount: 6000 dinars<br>After discount: 5400 dinarsExplain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3-4
+=adds each item to the running total: 4,500 + 1,500 = 6,000.+=هەر شتێک دەخاتە سەر کۆکە: 4,500 + 1,500 = 6,000.- Lineدێڕی 6
Multiplying by 0.9 keeps 90%: 6,000 × 0.9 = 5,400.
لێکدان لە 0.9، 90%ی کۆکە دەهێڵێتەوە: 6,000 × 0.9 = 5,400.
Summary and reviewپوختە و پێداچوونەوە
- Arithmetic:
+ - * / % **./can give a float;%gives the remainder; dividing by zero stops the script. - Assignment:
=stores a value.+=,-=,*=,/=,%=,**=and.=calculate and store in one step. ++and--add or take away 1. Prefix (++$x) changes first; postfix ($x++) gives the old value first.- Comparison operators give
true/false.==compares values after conversion;===also compares types.<=>gives -1, 0 or 1. - Logical operators
&&,||,!,xor(and the low-precedenceand,or) combine conditions; PHP stops as soon as it knows the answer. - Precedence decides the order:
**, then* / %, then+ -, then., comparisons,&&,||, assignment, thenand,xor,or. When unsure, use parentheses.
- ژمێرەیی:
+ - * / % **./دەتوانێت بدات؛%دەدات؛ دابەشکردن بە سفر دەوەستێنێت. - :
=هەڵدەگرێت.+=،-=،*=،/=،%=،**=و.=بە یەک هەنگاو هەژمار دەکەن و هەڵدەگرن. ++و--یەک زیاد یان کەم دەکەن. پێشگر (++$x) سەرەتا دەگۆڕێت؛ پاشگر ($x++) سەرەتا دەدات.-
true/falseدەدەن.==دوای بەراورد دەکات؛===جۆرەکانیش بەراورد دەکات.<=>یەکێک لە -1، 0 یان 1 دەدات. -
&&،||،!،xor(وand،orبە پێشینەیی نزم) پێکەوە دەبەستن؛ PHP هەر کە وەڵامەکەی زانی دەوەستێت. - ڕیزبەندییەکە دیاری دەکات:
**، پاشان* / %، پاشان+ -، پاشان.، بەراوردەکان،&&،||، ، پاشانand،xor،or. کاتێک دڵنیا نیت، کەوانە بەکاربهێنە.
What is the difference between an operator and an operand?جیاوازیی نێوان کارپێکەر و کارپێکراو چییە؟
The operator is the symbol that does the work (+); the operands are the values it works on (2 and 3 in 2 + 3).
ئەو هێمایەیە کە کارەکە دەکات (+)؛ ئەو کە کاریان لەسەر دەکرێت (2 و 3 لە 2 + 3ـدا).
Why does echo 10 / 4; print 2.5 but echo 10 / 5; print 2?بۆچی echo 10 / 4; ژمارە 2.5 چاپ دەکات بەڵام echo 10 / 5; ژمارە 2؟
/ returns an integer when the division is exact and a float when it is not.
/ کاتێک دابەشکردنەکە تەواو بێت دەگەڕێنێتەوە و کاتێک تەواو نەبێت .
What does $x .= "!"; do, and which family does .= belong to?$x .= "!"; چی دەکات و .= سەر بە کام خێزانە؟
It adds ! to the end of $x. It is an assignment operator for strings.
! دەخاتە کۆتایی $xـەوە. بۆ .
If $a = 3, what do echo ++$a; and echo $a++; print (each starting from 3)?ئەگەر $a = 3 بێت، echo ++$a; و echo $a++; چی چاپ دەکەن (هەریەکەیان لە 3ـەوە دەست پێ بکات)؟
++$a prints 4; $a++ prints 3. In both cases $a is 4 afterwards.
++$a ژمارە 4 چاپ دەکات؛ $a++ ژمارە 3. لە هەردوو حاڵەتدا دواتر $a دەبێت بە 4.
Why is echo $--a; an error?بۆچی echo $--a; هەڵەیە؟
The -- must come before the whole variable (--$a) or after it ($a--); it cannot sit between $ and the name. PHP reports a parse error.
-- دەبێت پێش هەموو (--$a) یان دوای ($a--) بێت؛ ناتوانێت لە نێوان $ و ناوەکەدا بێت. PHP ڕادەگەیەنێت.
What do 5 == "5" and 5 === "5" return, and why?5 == "5" و 5 === "5" چی دەگەڕێننەوە و بۆچی؟
true and false. == converts the string to a number first; === also requires the same type.
true و false. == سەرەتا دەکات بە ژمارە؛ === هەمان جۆریشی پێویستە.
Are >, <, == and != boolean (logical) operators?ئایا >، <، == و != کارپێکەرە لۆجیکییەکانن؟
No. They are comparison operators that produce booleans. The logical operators are &&, ||, !, and, or and xor.
نەخێر. ئەمانە کە بەرهەم دەهێنن. بریتین لە &&، ||، !، and، or و xor.
What is the value of ((1 + 2 + 3) * 4) / 2 - 5?نرخی ((1 + 2 + 3) * 4) / 2 - 5 چەندە؟
7: brackets give 6, then 6 × 4 = 24, 24 / 2 = 12, and 12 - 5 = 7.
7: کەوانەکان 6 دەدەن، پاشان 6 × 4 = 24، 24 / 2 = 12 و 12 - 5 = 7.
Why can if ($b != 0 && $a / $b > 2) never divide by zero?بۆچی if ($b != 0 && $a / $b > 2) هەرگیز بە سفر دابەش ناکات؟
Because of short-circuit evaluation: when $b != 0 is false, PHP does not run the right side of &&.
بەهۆی : کاتێک $b != 0 ناڕاست بێت، PHP لای ڕاستی && جێبەجێ ناکات.
Videoڤیدیۆ
Watch a videoسەیری ڤیدیۆیەک بکە
A video that viewers liked a lot and praised in their comments, chosen for this lesson.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.
A calm overview of PHP operators with examples: string, arithmetic, assignment, comparison and logical operators, and increment and decrement.
ناساندنێکی ئارام بۆ بە نموونەوە: ، ، ، و ، لەگەڵ و .
Overview of every operator group in this lesson.
تێڕوانینێکی گشتی بۆ هەموو گرووپەکانی لەم وانەیەدا.
Lesson complete!وانەکە تەواو بوو!
Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.
Key termsزاراوە سەرەکییەکان
Conditional Statements: if, else, elseif and switchڕستە مەرجییەکان: if و else و elseif و switch
Related lecturesوانە پەیوەندیدارەکان
- Strings and string functionsزنجیرەنووسەکان و کردارەکانی زنجیرەنووسە
- Conditional Statements: if, else, elseif and switchڕستە مەرجییەکان: if و else و elseif و switch
- Loops: while, do...while, for, foreach, break and continueخولەکان: while و do...while و for و foreach و break و continue
- Arrays in PHPڕیزەکەکان لە PHPـدا
- Sorting Arraysڕیزکردنی ڕیزەکەکان
References and further readingسەرچاوەکان و خوێندنەوەی زیاتر
- PHP Manual: Operators
- PHP Manual: Operator precedence
- PHP Manual: Arithmetic operators
- PHP Manual: Assignment operators
- PHP Manual: Incrementing/decrementing operators
- PHP Manual: Comparison operators
- PHP Manual: Logical operators
- PHP Manual: String operators
- PHP Manual: Array operators
- PHP Manual: PHP type comparison tables
- PHP Manual: intdiv()
- PHP Manual: fmod()
This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.