Chapterبەش 5 · Lectureوانە 5.4
Variablesگۆڕاوەکان
A variable is a named box in memory that holds a value. Learn how to create variables in PHP, the rules for naming them, how to change and copy their values, how to show them on a page, and what happens when a variable has no value.
سندوقێکی ناونراوە لە کە هەڵدەگرێت. فێر دەبیت لە PHPـدا چۆن دروست بکەیت، یاساکانی ناونانیان چین، چۆن بگۆڕیت و لەبەریان بگریتەوە، چۆن لەسەر پیشانیان بدەیت و کاتێک نییە چی ڕوودەدات.
What you will learnچی فێر دەبیت
- Explain what a variable is and how it uses memory.
- Create a variable and give it a value with
=. - Name variables correctly and spot invalid names.
- Remember that variable names are case-sensitive.
- Change, copy and print a variable's value with
echoandprint_r(). - Use a variable inside double quotes, with curly braces when needed.
- Understand the "Undefined variable" warning and what "loosely typed" means.
- ڕوون بکەیتەوە چییە و چۆن بەکاردەهێنێت.
- دروست بکەیت و بە
=پێ بدەیت. - بە دروستی ناو بۆ دابنێیت و ناوی ڕێگەپێنەدراو بناسیتەوە.
- لەبیرت بێت کە ناوی .
- بگۆڕیت، لەبەری بگریتەوە و بە
echoوprint_r()بینووسیت. - لەناو جووتدا بەکاربهێنیت و لە کاتی پێویستدا کەوانەی لوولی لە دەوری دابنێیت.
- لە «Undefined variable» و واتای «» تێبگەیت.
What is a variable?گۆڕاو چییە؟
A must remember things while it runs: a visitor's name, a student's age, a running total. The main way to store information in the middle of a PHP program is a .
لە کاتی کارکردنیدا دەبێت چەند شتێکی لەبیر بێت: ناوی سەردانکەرێک، تەمەنی خوێندکارێک، یان کۆی گشتیی چەند ژمارەیەک. ڕێگای سەرەکی بۆ هەڵگرتنی لە ناوەڕاستی PHPـدا، بەکارهێنانی .
A named place in that holds a ; the value can change while the program runs. In Python you simply write age = 20; in PHP the name starts with $ ($age = 20;).
شوێنێکی ناونراوە لەناو کە هەڵدەگرێت و دەتوانێت لە کاتی کارکردنی بگۆڕێت. لە Pythonـدا تەنها دەنووسیت age = 20؛ لە PHPـدا ناوەکە بە $ دەست پێ دەکات ($age = 20;).
A variable is a container for storing information that might vary (change) while the runs. That is where the name comes from: a variable stands in for a quantity that might vary. For example, the number of students who have logged in today is not fixed, so a program keeps it in a variable.
بریتییە لە لەخۆگرێک بۆ هەڵگرتنی کە لەوانەیە لە کاتی کارکردنی بگۆڕێت. ناوەکەشی لەمەوە هاتووە: لە جێگەی بڕێک دادەنرێت کە لەوانەیە بگۆڕێت. بۆ نموونە، ژمارەی ئەو خوێندکارانەی ئەمڕۆ چوونەتە ژوورەوە جێگیر نییە، بۆیە لە هەڵیدەگرێت.
Using a variable has two steps. First you give it a name. Then you store information (a ) in it. For example, a variable named age can hold the number 22:
بەکارهێنانی دوو هەنگاوە. سەرەتا ناوێکی بۆ دادەنێیت، پاشان () تێیدا هەڵدەگریت. بۆ نموونە، بە ناوی age دەتوانێت ژمارە 22 هەڵبگرێت:
$age = 22;Think of a spice shop in the bazaar. Every jar has a label: turmeric, black pepper, sumac. The label is the variable's name. What is inside the jar is its value. The shopkeeper can empty a jar and fill it with something new at any time, but the label stays the same.
دوکانێکی بەهارات لە بازاڕدا بهێنە پێش چاوت. هەر قاپێک لیبڵێکی پێوەیە: زەردەچەوە، بیبەری ڕەش، سوماق. لیبڵەکە ناوی . ئەوەی لەناو قاپەکەدایە . دوکاندارەکە هەر کاتێک بیەوێت دەتوانێت قاپێک بەتاڵ بکاتەوە و شتێکی نوێی تێبکات، بەڵام لیبڵەکە وەک خۆی دەمێنێتەوە.
Where the analogy stops workingسنووری ئەم نموونەیە
A real jar can hold a mix of things. A variable holds exactly one value at a time: when you put in a new value, the old one is thrown away. PHP is also strict about labels: they must follow the naming rules, and $Name and $name are two different jars.
قاپێکی ڕاستەقینە دەتوانێت تێکەڵەیەک لە شت هەڵبگرێت، بەڵام لە هەر کاتێکدا تەنها یەک هەڵدەگرێت: کاتێک تێدەخەیت، فڕێ دەدرێت. هەروەها PHP لە لیبڵەکاندا توندە: دەبێت یاساکانی ناونان بپارێزن و $Name و $name دوو قاپی جیاوازن.
Learn what a variable is, how to make one with $ and =, and how to show its value on the page.
فێر دەبیت چییە، چۆن بە $ و = یەکێک دروست دەکەیت و چۆن لەسەر لاپەڕەکە پیشان دەدەیت.
Watch 2:45 to 6:16: what a variable is and how to create one.
لە 2:45 تا 6:16 سەیر بکە: چییە و چۆن یەکێک دروست دەکەیت.
What is a variable?گۆڕاو چییە؟
Variables live in memoryگۆڕاوەکان لە بیرگەدا دەژین
Inside the computer, a variable refers to a place in : one memory location, or a set of memory locations. The value of the variable at any moment is simply what is stored in that place right now.
لەناو ، ناوێکە بۆ شوێنێک لە : یەک شوێن، یان کۆمەڵێک شوێن. لە هەر ساتێکدا، هەمان ئەو شتەیە کە ئێستا لەو شوێنەدا هەڵگیراوە.
Which values a variable can hold depends partly on its type (its ): whole numbers, numbers with a fraction, text, and so on. You will learn all the types in Data types.
چ جۆرە هەڵدەگرێت؟ ئەمە تا ڕادەیەک بەندە بە جۆرەکەیەوە (): ، ، دەق و هتد. لە وانەی جۆرەکانی دراوە فێری هەموو جۆرەکان دەبیت.
So the course material gives three definitions of a variable, and they all describe the same thing: (1) a container for storing information, (2) something that stands in for a quantity that might vary, and (3) a name for a place in memory whose contents are the value.
کەواتە کەرەستەی کۆرسەکە سێ پێناسە بۆ دەدات و هەر سێکیان باسی هەمان شت دەکەن: (١) لەخۆگرێک بۆ هەڵگرتنی ، (٢) شتێک کە لە جێگەی بڕێک دادەنرێت کە لەوانەیە بگۆڕێت، (٣) ناوێک بۆ شوێنێک لە کە ناوەڕۆکەکەی .
Creating a variable: assignmentدروستکردنی گۆڕاو: دانانی نرخ
In PHP you do not need a special command to create a variable. A variable is created the first time you give it a value. Giving a variable a value is called . As in most programming languages, you use the equals sign = for it. Write the $ sign, the name, =, the value, and a semicolon ;.
لە PHPـدا بۆ دروستکردنی پێویستت بە فەرمانێکی تایبەت نییە. یەکەم جار کە پێدەدەیت دروست دەبێت. بەم کارە دەوترێت . وەک زۆربەی ، بۆ ئەم کارە هێمای یەکسان = بەکاردێت. هێمای $، پاشان ناوەکە، =، و ; بنووسە.
| Partبەش | Exampleنموونە | What it doesچی دەکات |
|---|---|---|
$ | $age | Tells PHP: this is a variable.بە PHP دەڵێت: ئەمە . |
| nameناو | age | The variable's name: the label on the box.ناوی : لیبڵەکەی سەر سندوقەکە. |
= | = | The : it puts the value on the right into the variable on the left.: لای ڕاست دەخاتە ناو لای چەپەوە. |
| value | 22 | What you store: a number, text inside quotes, and so on.ئەوەی هەڵیدەگریت: ژمارەیەک، دەقێک لەناو و هتد. |
; | ; | Ends the .کۆتایی بە دەهێنێت. |
Creating a variable: assignmentدروستکردنی گۆڕاو: دانانی نرخ
First examplesیەکەم نموونەکان
<?php
$var1 = 10;
echo $var1;
?>10
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
$var1gets the value 10.$var110 وەردەگرێت.- Lineدێڕی 3
echosends the value of$var1to the browser.echo$var1دەنێرێت بۆ .
<?php
$var2 = "Hello World";
echo $var2;
?>Hello World
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
Text must be inside quotes.
دەق دەبێت لەناو بێت.
Creating a variable: assignmentدروستکردنی گۆڕاو: دانانی نرخ
Numbers and textژمارە و دەق
You can store negative numbers, whole numbers and numbers with a fraction. Text (a ) must be written inside quotes. Without quotes PHP does not know what you stored: it thinks the word is the name of a and stops with an .
دەتوانیت ، و هەڵبگریت. دەق () دەبێت لەناو بنووسرێت. بەبێ PHP نازانێت چیت هەڵگرتووە: وا دەزانێت وشەکە ناوی و بە دەوەستێت.
<?php
$name = "Shilan Ahmad";
$age = 22;
$price = 750.50;
$temperature = -5;
echo $name . "<br>";
echo $age . "<br>";
echo $price . "<br>";
echo $temperature;
?>Shilan Ahmad 22 750.5 -5
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Shilan Ahmad<br>22<br>750.5<br>-5Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
Text goes inside quotes.
دەق دەچێتە ناو .
- Lineدێڕی 3-5
Numbers are written without quotes. A minus sign makes a negative number.
ژمارە بەبێ دەنووسرێت. نیشانەی کەم (-) ژمارەکە سالب دەکات.
- Lineدێڕی 7
The dot
.joins two pieces of text (); here it adds a<br>after each value.خاڵ
.دوو پارچە دەق پێکەوە دەلکێنێت ()؛ لێرەدا دوای هەر<br>ێک زیاد دەکات.- Lineدێڕی 9
750.50is printed as750.5: PHP shows a number with a fraction using as few decimal places as it needs.750.50وەک750.5دەنووسرێت: PHP بە کەمترین ژمارەی پێویستی ڕەقەمی دوای خاڵ پیشان دەدات.
You can create several variables one after the other. This short example from the course slides stores a string, a whole number and a number with a fraction:
دەتوانیت چەند لە دوای یەک دروست بکەیت. ئەم نموونە کورتەی سلایدەکانی کۆرسەکە ، و هەڵدەگرێت:
<?php
$text = "Hello world!";
$x = 10;
$y = 3.5;
echo $text;
echo "<br>";
echo $x;
echo "<br>";
echo $y;
?>Hello world! 10 3.5
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Hello world!<br>10<br>3.5Creating a variable: assignmentدروستکردنی گۆڕاو: دانانی نرخ
Changing a valueگۆڕینی نرخ
The value of a variable can change while the script runs. That is exactly why it is called a variable. To give it a new value, use = again:
لە کاتی کارکردنی دەتوانێت بگۆڕێت؛ هەر بۆیە پێی دەوترێت . بۆ ئەوەی پێ بدەیت، دیسان = بەکاربهێنە:
<?php
$var1 = 10;
echo $var1;
echo "<br>";
$var1 = 100;
echo $var1;
?>10 100
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
10<br>100Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2-3
First
$var1holds 10, andechoshows it.سەرەتا
$var1ژمارە 10ی تێدایە وechoپیشانی دەدات.- Lineدێڕی 4
<br>is an HTML tag that starts a new line in the browser.<br>HTMLـە کە لە دێڕێکی نوێ دەست پێدەکات.- Lineدێڕی 5
The new value 100 replaces 10. The old value is gone.
، 100، جێگەی 10 دەگرێتەوە. نامێنێت.
Here is the same idea with text. The variable $color first holds Green. The second assignment replaces it with Blue:
هەمان بیرۆکە لەگەڵ دەق. $color سەرەتا Greenی تێدایە. دووەم Blue دەخاتە جێگەی:
<?php
$color = "Green";
echo $color . "<br>";
$color = "Blue"; // the old value is replaced
echo $color;
?>Green Blue
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Green<br>BlueCreating a variable: assignmentدروستکردنی گۆڕاو: دانانی نرخ
Copying a value, and the empty stringلەبەرگرتنەوەی نرخ و زنجیرەنووسەی بەتاڵ
You can also copy one variable into another. The new variable gets a copy of the value. The two variables are separate boxes, so changing one later does not change the other:
هەروەها دەتوانیت لەبەر بگریتەوە بۆ ناو تر. کۆپییەکی وەردەگرێت. هەردوو دوو سندوقی جیاوازن، بۆیە ئەگەر دواتر یەکێکیان بگۆڕیت، ئەوی تر ناگۆڕێت:
<?php
$name = "Dilan";
$favoriteName = $name; // copy the value of $name
echo $favoriteName . "<br>";
$name = "Rezan"; // change $name later...
echo $name . "<br>";
echo $favoriteName; // ...the copy does not change
?>Dilan Rezan Dilan
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Dilan<br>Rezan<br>DilanExplain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
$favoriteNamegets a copy of the value of$name("Dilan").$favoriteNameکۆپییەکی$name("Dilan") وەردەگرێت.- Lineدێڕی 6
Later,
$namegets a new value.دواتر
$nameوەردەگرێت.- Lineدێڕی 8
$favoriteNamestill holds "Dilan". It is a separate box.$favoriteNameهێشتا "Dilan"ی تێدایە، چونکە سندوقێکی جیاوازە.
A variable can also hold an empty string: text with no characters at all, written as two quotes with nothing between them. It is a real value (a string of length 0), and echo prints nothing for it:
دەتوانێت بەتاڵیش هەڵبگرێت: دەقێک کە هیچ تێدا نییە و بە دوو بێ ناوەڕۆک دەنووسرێت. ئەمە ڕاستەقینەیە ( بە درێژیی 0) و echo هیچی بۆ پیشان نادات:
<?php
$emptyVar = "";
echo "[" . $emptyVar . "]";
?>[]
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
The square brackets are printed only to show that nothing is between them.
کەوانە چوارگۆشەکان تەنها بۆ ئەوە نووسراون کە دەربکەوێت هیچ لە نێوانیاندا نییە.
Rules for naming variablesیاساکانی ناونانی گۆڕاوەکان
The name of a variable is also called its . PHP has strict rules for names. If you break one, PHP stops with an error before it runs anything in the file.
بە ناوی دەوترێت . PHP یاسای توندی بۆ ناو هەیە. ئەگەر یەکێکیان بشکێنیت، PHP پێش ئەوەی هیچ شتێکی ناو جێبەجێ بکات، بە دەوەستێت.
- Every variable name starts with the dollar sign
$. It tells PHP that this is a variable. (In computing, a sign like this in front of a name is called a sigil.) - After the
$, the first character must be a letter or an_. It can not be a number. - After that, the name may contain only letters, numbers and underscores (
A-Z,a-z,0-9,_). - No spaces, and no other symbols such as
+ - % ( ) . & ?. PHP reads these as operators or punctuation, not as part of the name. - A name can be any length.
$xand$numberOfStudentsInTheClassare both allowed. - Names are :
$ageand$AGEare two different variables (see the next section). $thisis reserved. PHP uses it for objects, so you cannot use it as your own variable (Fatal error: Cannot re-assign $this).
- هەموو ناوێکی بە هێمای دۆلار
$دەست پێدەکات. ئەمە بە PHP دەڵێت کە ئەمە . (لە زمانی ئایتیدا ئەم جۆرە هێمایەی پێش ناو بە sigil ناسراوە.) - دوای
$، یەکەم دەبێت پیتێک یان_بێت. نابێت ژمارە بێت. - لەوە بەدواوە، ناوەکە تەنها دەتوانێت پیت، ژمارە و لەخۆ بگرێت (
A-Z،a-z،0-9،_). - ڕێگەپێدراو نییە، هەروەها هیچ هێمایەکی تری وەک
+ - % ( ) . & ?. PHP ئەمانە وەک یان نیشانەی خاڵبەندی دەخوێنێتەوە، نەک وەک بەشێک لە ناوەکە. - ناو دەتوانێت بە هەر درێژییەک بێت.
$xو$numberOfStudentsInTheClassهەردووکیان ڕێگەپێدراون. - ناوەکان :
$ageو$AGEدوو جیاوازن (بڕوانە بەشی داهاتوو). $thisپارێزراوە. PHP بۆ بەکاری دەهێنێت، بۆیە ناتوانیت وەک خۆت بەکاری بهێنیت (Fatal error: Cannot re-assign $this).
| Nameناو | Allowed?ڕێگەپێدراوە؟ | Whyبۆچی |
|---|---|---|
$item | ✓ Yes✓ بەڵێ | Starts with a letter.بە پیت دەست پێدەکات. |
$Item | ✓ Yes✓ بەڵێ | Also valid, but a different variable from $item (capital I).ئەمیش ڕێگەپێدراوە، بەڵام جیاوازە لە $item (پیتی I گەورەیە). |
$myVariable | ✓ Yes✓ بەڵێ | Only letters.تەنها پیتە. |
$this_variable | ✓ Yes✓ بەڵێ | Letters and an underscore. (Only $this on its own is reserved.)پیت و . (تەنها $this بە تەنیا پارێزراوە.) |
$product3 | ✓ Yes✓ بەڵێ | A number is fine after the first letter.ژمارە دوای پیتی یەکەم کێشەی نییە. |
$_bookPage | ✓ Yes✓ بەڵێ | A name may start with an underscore.ناو دەتوانێت بە دەست پێبکات. |
$_name | ✓ Yes✓ بەڵێ | Starts with an underscore.بە دەست پێدەکات. |
$first_name | ✓ Yes✓ بەڵێ | Letters joined by an underscore.پیت کە بە پێکەوە لکاون. |
$name3 | ✓ Yes✓ بەڵێ | A number at the end.ژمارە لە کۆتاییدا. |
$name_3 | ✓ Yes✓ بەڵێ | An underscore and a number after the letters. و ژمارە لە دوای پیتەکان. |
$this-variable | ✗ No✗ نەخێر | - is a minus sign. PHP reads $this - variable.- نیشانەی کەمە. PHP وەک $this - variable دەیخوێنێتەوە. |
$1book | ✗ No✗ نەخێر | Starts with a number.بە ژمارە دەست پێدەکات. |
$3name | ✗ No✗ نەخێر | Starts with a number.بە ژمارە دەست پێدەکات. |
$name? | ✗ No✗ نەخێر | ? is not allowed in a name.? لە ناودا ڕێگەپێدراو نییە. |
$first+name | ✗ No✗ نەخێر | + is a plus sign. PHP reads $first + name.+ نیشانەی کۆکردنەوەیە. PHP وەک $first + name دەیخوێنێتەوە. |
$first.name | ✗ No✗ نەخێر | . joins strings. PHP reads $first . name.. پێکەوە دەلکێنێت. PHP وەک $first . name دەیخوێنێتەوە. |
$my name | ✗ No✗ نەخێر | A space ends the name. ناوەکە کۆتایی پێدەهێنێت. |
$this | ✗ No✗ نەخێر | Reserved by PHP for objects.PHP بۆ پاراستوویەتی. |
Rules for naming variablesیاساکانی ناونانی گۆڕاوەکان
What PHP says about bad namesPHP دەربارەی ناوی نادروست چی دەڵێت
Here is what PHP really says when a name starts with a number. Notice that done is never printed: a stops the whole file before any line runs.
ئەمە ئەوەیە کە PHP بە ڕاستی دەیڵێت کاتێک ناوێک بە ژمارە دەست پێدەکات. سەرنج بدە کە done هەرگیز نانووسرێت: پێش جێبەجێبوونی هەر دێڕێک، هەموو ڕادەگرێت.
<?php
$1book = 1;
echo "done";Parse error: syntax error, unexpected integer "1", expecting variable or "{" or "$" in C:\xampp\htdocs\names.php on line 2A hyphen and the reserved name $this give these messages:
هێڵی ناوەڕاست (-) و ناوی پارێزراوی $this ئەم پەیامانە دەدەن:
$this-variable = 1; -> Parse error: syntax error, unexpected token "="
$this = 5; -> Fatal error: Cannot re-assign $thisVariable names are case-sensitiveناوی گۆڕاوەکان هەستیار بە قەبارەی پیتن
Uppercase (capital) and lowercase (small) letters are different in variable names. $name and $Name, or $text and $TEXT, are completely different boxes. This is a common trap for beginners.
لە ناوی پیتی گەورە (گردەک) و پیتی بچووک (هوردەک) جیاوازن. $name و $Name، یان $text و $TEXT، سندوقی بە تەواوی جیاوازن. ئەمە تەڵەیەکی باوە بۆ تازەفێربووان.
<?php
$my_variable = "Hello World";
$my_Variable = "Hello World
Again";
echo $my_Variable;
?>Hello World Again
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Hello World
AgainPHP sends the line break and the spaces, but the browser shows them as one single space.
PHP دێڕە نوێیەکە و دەنێرێت، بەڵام هەموویان وەک یەک پیشان دەدات.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2-3
$my_variable(small v) and$my_Variable(capital V) are two different variables.$my_variable(v بچووک) و$my_Variable(V گەورە) دوو جیاوازن.- Lineدێڕی 3-4
This string continues onto the next line, so it contains a line break and six spaces.
ئەم بەردەوام دەبێت بۆ دێڕی دواتر، بۆیە دێڕێکی نوێ و شەش لەخۆ دەگرێت.
- Lineدێڕی 5
echoprints the second variable only.echoتەنها دووەم دەنووسێت.
A wrong capital letter does not change the old variable. PHP looks for a new name, finds nothing, and shows a :
پیتێکی گەورەی نادروست ناگۆڕێت. PHP بەدوای ناوێکی نوێدا دەگەڕێت، هیچ نادۆزێتەوە و پیشان دەدات:
<?php
$name = "Hana";
echo $Name;
?>Warning: Undefined variable $Name in C:\xampp\htdocs\trap.php on line 3
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<br />
<b>Warning</b>: Undefined variable $Name in <b>C:\xampp\htdocs\trap.php</b> on line <b>3</b><br />Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
$Namewas never given a value. Only$name(small n) exists.$Nameهەرگیز پێ نەدراوە. تەنها$name(n بچووک) هەیە.
Choosing clear namesهەڵبژاردنی ناوی ڕوون
A variable name should be very clear. Names like $var1 and $var2 are not errors for PHP, and they are quick to type today. But after a few months, or a few years, you will mix them up. Names like $name and $age tell you at once what they hold.
ناوی دەبێت زۆر ڕوون بێت. ناوی وەک $var1 و $var2 بۆ PHP نین و ئەمڕۆ بە خێرایی دەنووسرێن. بەڵام دوای چەند مانگێک، یان چەند ساڵێک، تێکەڵیان دەکەیت. ناوی وەک $name و $age یەکسەر پێت دەڵێن چییان تێدایە.
A name often has more than one word, but you cannot use a space. The style is the programmer's choice. Two styles are common:
ناو زۆرجار لە زیاتر لە وشەیەک پێکدێت، بەڵام ناتوانیت بەکاربهێنیت. شێوازی ناونان بە ئارەزووی کۆدنووسەکەیە. دوو شێواز باون:
| Styleشێواز | Exampleنموونە | How it worksچۆن کار دەکات |
|---|---|---|
$first_name | All words in lowercase, joined with an underscore _.هەموو وشەکان بە پیتی بچووک و بە _ پێکەوە دەلکێن. | |
$firstName | The first word starts with a lowercase letter. Every word after it starts with a capital letter, like the humps of a camel. The book the notes follow calls this Camel Caps.وشەی یەکەم بە پیتی بچووک دەست پێدەکات. هەموو وشەکانی دوای ئەو بە پیتی گەورە دەست پێدەکەن، وەک کۆپانی وشتر. ئەو کتێبەی تێبینییەکان پەیڕەوی دەکەن پێی دەڵێت Camel Caps. |
Either style makes code easier to read and to remember. By convention, the first word always starts with a lowercase letter. Pick one style and use it everywhere in a project.
بە هەر یەکێک لەم دوو شێوازە، کۆد ئاسانتر دەخوێنرێتەوە و ئاسانتر لەبیر دەمێنێت. بە نەریت، وشەی یەکەم هەمیشە بە پیتی بچووک دەست پێدەکات. شێوازێک هەڵبژێرە و لە هەموو پرۆژەکەدا هەر ئەوە بەکاربهێنە.
Showing a variable's valueپیشاندانی نرخی گۆڕاوێک
To see what a variable holds, send its value to the browser. The two most common ways are echo and print_r().
بۆ ئەوەی بزانیت چی تێدایە، بنێرە بۆ . دوو ڕێگای باو هەن: echo و print_r().
Showing a variable's valueپیشاندانی نرخی گۆڕاوێک
Using echoبەکارهێنانی echo
echo prints a value. You met it in echo and print.
echo دەنووسێت. پێشتر لە وانەی echo و print ناسیت.
<?php
$age = 22;
echo $age;
?>22
PHP lives inside HTML, so you can use echo in the middle of the content of an HTML tag. Here a short PHP block sits inside a paragraph:
PHP لەناو HTMLـدا دەژی، بۆیە دەتوانیت echo لە ناوەڕاستی ناوەڕۆکی HTMLـدا بەکاربهێنیت. لێرەدا کەرتێکی کورتی PHP لەناو بڕگەیەکدایە:
echo inside an HTML paragraphecho لەناو بڕگەیەکی HTMLwelcome.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات<p>Welcome <?php $name = "Dilan"; echo $name; ?></p>Welcome Dilan
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<p>Welcome Dilan</p>Showing a variable's valueپیشاندانی نرخی گۆڕاوێک
Using print_r()بەکارهێنانی print_r()
The course's Kurdish notes call print_r() the quickest way to show a value on the screen. It prints a value in a form that people can read easily. Many people say the r stands for readable; the PHP manual describes the output as "human-readable". For a simple value it works just like echo:
تێبینییە کوردییەکانی کۆرسەکە دەڵێن print_r() خێراترین ڕێگایە بۆ خستنەڕووی لەسەر . بە شێوەیەک دەنووسێت کە مرۆڤ بە ئاسانی بیخوێنێتەوە. زۆر کەس دەڵێن پیتی r readableـە، واتە «خوێنراو» یان «دەتوانرێت بخوێنرێتەوە»؛ ڕێبەری فەرمیی PHPیش دەڵێت «بۆ مرۆڤ خوێنراوە». بۆ سادە، ڕێک وەک echo کار دەکات:
<?php
$today = "Friday";
print_r($today);
?>Friday
print_r() is most useful for : lists of values, which you will meet in Arrays. It puts each item on its own line and indents the inner parts (adds spaces in front of them), so the output is neat instead of jumbled. The example below shows the code (the input) and what it prints (the output). The <pre> tag keeps the line breaks and spaces in the browser.
print_r() زیاتر بۆ بەسوودە: لیستی ، کە لە وانەی ڕیزەکەکان دەیانناسیت. هەر لە دێڕێکی جیادا دادەنێت و بەشە ناوەکییەکان پێشبۆش دەکات (چەند لە پێشیانەوە زیاد دەکات)، بۆیە ڕێک و پێک دەبێت نەک تێکەڵ. نموونەی خوارەوە هەم کۆدەکە پیشان دەدات (ئەوەی دەیدەیتە PHP)، هەم (ئەوەی PHP دەینووسێت). <pre> دێڕە نوێیەکان و لە دەپارێزێت.
print_r() on an arrayprint_r() لەسەر ڕیزەکێکfruit.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات<?php
$a = array('a' => 'apple', 'b' => 'banana', 'c' => array('x', 'y', 'z'));
echo "<pre>";
print_r($a);
echo "</pre>";
?>Array
(
[a] => apple
[b] => banana
[c] => Array
(
[0] => x
[1] => y
[2] => z
)
)
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<pre>Array
(
[a] => apple
[b] => banana
[c] => Array
(
[0] => x
[1] => y
[2] => z
)
)
</pre>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
An array with three items. The item
cis itself an array of three letters.بە سێ .
cخۆی بە سێ پیت.- Lineدێڕی 3-5
<pre>keeps the line breaks;print_r()prints the array.<pre>دێڕە نوێیەکان دەپارێزێت؛print_r()دەنووسێت.
Showing a variable's valueپیشاندانی نرخی گۆڕاوێک
Variables inside double quotesگۆڕاوەکان لەناو زنجیرەنووسەیەکی جووتەگوتەدا
Inside a , PHP replaces a variable with its value. This is called . A does not do this: it prints the text exactly as written (more in Strings).
لەناو ، PHP لادەبات و دادەنێت. بەمە دەوترێت . ئەم کارە ناکات: دەقەکە ڕێک وەک خۆی دەنووسێت (زیاتر لە وانەی زنجیرەنووسەکان).
<?php
$city = "Halabja";
echo 'I live in $city.';
echo "<br>";
echo "I live in $city.";
?>I live in $city. I live in Halabja.
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
I live in $city.<br>I live in Halabja.Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
Single quotes:
$cityis printed as it is.تاک:
$cityوەک خۆی دەنووسرێت.- Lineدێڕی 5
Double quotes:
$cityis replaced by its value.جووت:
$cityبە دەگۆڕدرێت.
There is one trap. If letters come straight after the variable name, PHP thinks they are part of the name. Suppose $type holds bird and you want to print the word birdcage:
تەڵەیەک هەیە. ئەگەر پیت ڕاستەوخۆ دوای ناوی بێت، PHP وا دەزانێت ئەو پیتانە بەشێکن لە ناوەکە. وا دابنێ $type وشەی birdی تێدایە و دەتەوێت وشەی birdcage بنووسیت:
$typeCageنادروست: PHP بەدوای $typeCage دەگەڕێتcage.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات<?php
$type = "bird";
echo "Keep the $typeCage clean";
?>Warning: Undefined variable $typeCage in C:\xampp\htdocs\cage.php on line 3 Keep the clean
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<br />
<b>Warning</b>: Undefined variable $typeCage in <b>C:\xampp\htdocs\cage.php</b> on line <b>3</b><br />
Keep the cleanPHP shows a warning, then prints the sentence with nothing in that place. Look closely: there are two spaces in Keep the clean.
PHP پیشان دەدات، پاشان دەقەکە دەنووسێت بێ هیچ شتێک لەو شوێنەدا. وردبەرەوە: دوو لە Keep the cleanدا هەیە.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
PHP reads the name
$typeCage, which does not exist.PHP ناوی
$typeCageدەخوێنێتەوە، کە بوونی نییە.
The solution is to put curly braces { } around the variable. Now PHP knows exactly where the name ends:
چارەسەرەکە ئەوەیە کە کەوانەی لوولی { } لە دەوری دابنێیت. ئێستا PHP دەزانێت ناوەکە ڕێک لە کوێ کۆتایی دێت:
<?php
$type = "bird";
echo "Keep the {$type}cage clean";
?>Keep the birdcage clean
Explains the PHP opening and closing tags (and why to leave out ?> in pure PHP files), and the kinds of PHP comments.
کردنەوە و داخستنی ڕوون دەکاتەوە، بۆچی لەو تەنها تێدایە ?> نانووسین و جۆرەکانی لە .
Watch 7:45 to 8:52: variables inside double-quoted text.
لە 7:45 تا 8:52 سەیر بکە: لەناو .
A variable lasts for the whole scriptگۆڕاو بۆ هەموو دەسنووسەکە دەمێنێتەوە
Here is the simplest PHP script: one statement that prints a paragraph with Hello World.
ئەمە سادەترین PHPـە: یەک کە بڕگەیەک بە Hello World دەنووسێت.
<?php
echo "<p>Hello World</p>";
?>Hello World
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<p>Hello World</p>Inside a complete HTML page, a PHP section can create a variable and print it:
لەناو تەواوی HTMLـدا، کەرتێکی PHP دەتوانێت دروست بکات و بینووسێت:
<html>
<body>
<?php
$greeting = "Hello World";
echo "<p>$greeting</p>";
?>
</body>
</html>Hello World
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<html>
<body>
<p>Hello World</p> </body>
</html>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3-6
The PHP section: create
$greeting, then print it inside a paragraph.کەرتی PHP:
$greetingدروست بکە، پاشان لەناو بڕگەیەکدا بینووسە.
A PHP file can contain several PHP sections (the parts between <?php and ?>) with HTML in between. A variable belongs to the whole script, not only to the section where you created it. If it holds 5 at the start, it still holds 5 at the end, unless you give it a new value.
PHP دەتوانێت چەند کەرتێکی PHP (ئەو بەشانەی نێوان <?php و ?>) لەخۆ بگرێت و HTML لە نێوانیاندا بێت. هی هەموو ، نەک تەنها هی ئەو کەرتەی تێیدا دروست کراوە. ئەگەر لە سەرەتادا 5ی تێدا بێت، لە کۆتاییشدا هەر 5ی تێدایە، مەگەر پێ بدەیت.
<html>
<body>
<?php
$greeting = "Hello World";
echo "<p>$greeting</p>";
?>
<p>I am a paragraph.</p>
<?php
echo "<p>$greeting again</p>";
?>
</body>
</html>Hello World I am a paragraph. Hello World again
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<html>
<body>
<p>Hello World</p>
<p>I am a paragraph.</p>
<p>Hello World again</p> </body>
</html>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3-6
The first PHP section creates
$greetingand prints it in a paragraph.کەرتی یەکەمی PHP
$greetingدروست دەکات و لە بڕگەیەکدا دەینووسێت.- Lineدێڕی 8
Plain HTML. PHP sends it to the browser unchanged.
HTMLی ئاسایی. PHP بێ گۆڕین دەینێرێت بۆ .
- Lineدێڕی 10-12
A second PHP section.
$greetingstill holds "Hello World".کەرتی دووەمی PHP.
$greetingهێشتا "Hello World"ی تێدایە.
Using a variable that has no valueبەکارهێنانی گۆڕاوێک کە نرخی نییە
If you use a variable that was never given a value, PHP cannot find it. It shows a saying that the variable is undefined (unknown), uses an empty value in its place, and keeps running.
ئەگەر بەکاربهێنیت کە هەرگیز پێ نەدراوە، PHP نایدۆزێتەوە. پیشان دەدات کە دەڵێت نەناسراوە، لە جێگەیدا بەتاڵ بەکاردەهێنێت و بەردەوام دەبێت.
$name was never created$name هەرگیز دروست نەکراوەhello.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات<?php
echo "Hello, $name!";
echo "<br>The script keeps running.";
?>Warning: Undefined variable $name in C:\xampp\htdocs\hello.php on line 2 Hello, ! The script keeps running.
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<br />
<b>Warning</b>: Undefined variable $name in <b>C:\xampp\htdocs\hello.php</b> on line <b>2</b><br />
Hello, !<br>The script keeps running.PHP is a loosely typed languageلە PHPـدا جۆری دراوەی گۆڕاوەکان دیاری ناکرێت
Look again at the examples above. We never told PHP which a variable has. PHP works it out by itself: it gives the variable the correct type depending on the value inside it. A language that works like this is called .
جارێکی تر سەیری نموونەکانی سەرەوە بکە. هەرگیز بە PHPـمان نەگوت چییە. PHP خۆی ئەمە دەزانێت: بەپێی ئەو تێیدایە، جۆری گونجاوی پێدەدات. بەو زمانانەی بەم شێوەیە کار دەکەن دەوترێت .
In other languages such as C, C++ and Java, the programmer must declare the name and the type of a variable before using it. After that, the variable can hold only that type:
لە زمانەکانی تری وەک C و C++ و Java، دەبێت پێش بەکارهێنانی ، ناو و جۆرەکەی ڕابگەیەنێت. لەوە بەدواوە، تەنها ئەو جۆرە هەڵدەگرێت:
int age = 22; // age may only ever hold a whole number
String name = "Hana"; // name may only hold textIn PHP you just assign. The same variable can even hold different types at different times. var_dump() shows the type PHP chose each time:
لە PHPـدا تەنها دادەنێیت. هەمان دەتوانێت لە کاتی جیاوازدا جۆری جیاواز هەڵبگرێت. var_dump() ئەو جۆرە پیشان دەدات کە PHP هەر جارێک هەڵیبژاردووە:
<?php
echo "<pre>";
$x = 5; // PHP sees a whole number
var_dump($x);
$x = "five"; // now the same variable holds text
var_dump($x);
$x = 5.5; // and now a number with a fraction
var_dump($x);
echo "</pre>";
?>int(5) string(4) "five" float(5.5)
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<pre>int(5)
string(4) "five"
float(5.5)
</pre>There is much more to say about types: which types exist, how PHP converts between them, and how to check them. That is the topic of the next lecture, Data types.
زۆر شتی تر دەربارەی جۆرەکان هەیە: چ جۆرێک هەیە، PHP چۆن لە نێوانیاندا دەگۆڕێت و چۆن پشکنینیان بۆ دەکەیت. ئەمە بابەتی وانەی داهاتووە، جۆرەکانی دراوە.
Common mistakesهەڵە باوەکان
Most beginner errors with variables are one of these. The "What happens" column shows the real PHP 8 message.
زۆربەی هەڵەکانی تازەفێربووان لەگەڵ یەکێکن لەمانە. لە خشتەکەدا، بەشی «چی ڕوودەدات» پەیامی ڕاستەقینەی PHP 8 پیشان دەدات.
| Mistakeهەڵە | Exampleنموونە | What happensچی ڕوودەدات | Fixچاککردن |
|---|---|---|---|
Forgetting the $لەبیرچوونی $ | name = "Aram"; | Parse error: syntax error, unexpected token "=" | $name = "Aram"; |
| Text without quotesدەق بەبێ | $name = Aram; | Error: Undefined constant "Aram" | $name = "Aram"; |
| Curly quotes خوار | $city = “Erbil”; | Error: Undefined constant "“Erbil”". PHP reads “Erbil” as the name of a constant.Error: Undefined constant "“Erbil”". PHP “Erbil” وەک ناوی دەخوێنێتەوە. | $city = "Erbil"; |
| Name starts with a numberناو بە ژمارە دەست پێدەکات | $1book = 1; | Parse error: syntax error, unexpected integer "1" | $book1 = 1; |
| Hyphen in a nameهێڵ لە ناودا | $first-name = "Hana"; | Parse error: syntax error, unexpected token "=" | $first_name = "Hana"; |
| Wrong capital letterپیتی گەورەی نادروست | echo $Name; | Warning: Undefined variable $Name | Spell the name the same way everywhere: echo $name;ناوەکە لە هەموو شوێنێک وەک یەک بنووسە: echo $name; |
| Missing semicolon ونبوو | $total = 10
echo $total; | Parse error: syntax error, unexpected token "echo" | $total = 10; |
| Letters straight after a variable in double quotesپیت ڕاستەوخۆ دوای لەناو جووت | "Keep the $typecage clean" | Warning: Undefined variable $typecage | "Keep the {$type}cage clean" |
== instead of === لە جیاتی = | $age == 21; | Nothing is stored: == only compares.هیچ هەڵناگیرێت: == تەنها بەراورد دەکات. | $age = 21; |
| Wrong opening tag کردنەوەی نادروست | <php? | The code is sent to the browser as text.کۆدەکە وەک دەق دەنێردرێت بۆ . | <?php |
Practiceڕاهێنان
Exercises
ڕاهێنانەکان
This is Lab 2, Example 2: "Declare variables and assign values to them". Open a new PHP project (in NetBeans, VS Code or any editor, saved in XAMPP's htdocs folder). Store the text Hello World! in a variable $txt and the number 10 in a variable $number. Print the two values on two lines, and add a comment above the echo lines. Before you run it, write down exactly what the browser will show.
ئەمە نموونەی دووەمی تاقیگەی ٢یە: « دروست بکە و پێ بدە». پرۆژەیەکی نوێی PHP بکەرەوە (لە NetBeans یان VS Code یان هەر دەستکاریکەرێک و لە بوخچەی htdocsی XAMPPـدا پاشەکەوتی بکە). دەقی Hello World! لە $txt و ژمارە 10 لە $numberدا هەڵبگرە. هەردوو لە دوو دێڕدا بنووسە و لە سەرووی دێڕەکانی echo زیاد بکە. پێش ئەوەی کاری پێبکەیت، ڕێک بنووسە کە چی پیشان دەدات.
Show answerوەڵامەکە پیشان بدە
<?php
$txt = "Hello World!";
$number = 10;
// Display variables value
echo $txt;
echo "<br>";
echo $number;
?>Hello World! 10
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Hello World!<br>10Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2-3
Two variables: one string, one integer.
دوو : یەکێکیان ، ئەوی تر .
- Lineدێڕی 4
A comment. PHP ignores it.
. PHP فەرامۆشی دەکات.
- Lineدێڕی 6
<br>puts the number on a new line.<br>ژمارەکە دەخاتە دێڕێکی نوێوە.
This is Lab 2, Example 3. Create three variables, $color, $Color and $COLOR, holding blue, red and green. Print one sentence for each: The color of the sky is …, The color of the sun is …, The color of the tree is …. What does the result tell you about variable names?
ئەمە نموونەی سێیەمی تاقیگەی ٢یە. سێ دروست بکە، $color و $Color و $COLOR، کە بە ڕیز blue و red و greenیان تێدا بێت. بۆ هەر یەکێکیان دێڕێک بنووسە: The color of the sky is …، The color of the sun is …، The color of the tree is …. ئەنجامەکە چی دەربارەی ناوی پێت دەڵێت؟
Show answerوەڵامەکە پیشان بدە
<?php
// Assign values to variables
$color = "blue";
$Color = "red";
$COLOR = "green";
// Print the values
echo "The color of the sky is " . $color . "<br>";
echo "The color of the sun is " . $Color . "<br>";
echo "The color of the tree is " . $COLOR . "<br>";
?>The color of the sky is blue The color of the sun is red The color of the tree is green
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
The color of the sky is blue<br>The color of the sun is red<br>The color of the tree is green<br>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3-5
Three different variables: the names differ only in capital letters.
سێ جیاواز: ناوەکان تەنها لە پیتی گەورەدا جیاوازن.
- Lineدێڕی 8-10
The dot
.joins the sentence, the value and<br>.خاڵ
.دەقەکە و و<br>پێکەوە دەلکێنێت.
Each sentence shows a different value, so $color, $Color and $COLOR are three separate variables. Variable names are case-sensitive.
هەر دێڕێک جیاواز پیشان دەدات، کەواتە $color و $Color و $COLOR سێ جیاوازن. ناوی .
This script has five mistakes. Find each one, say why it is wrong, and fix it so that the script prints Rezan Ali Karim, 21.
ئەم پێنج هەڵەی تێدایە. هەر یەکێکیان بدۆزەرەوە، بڵێ بۆچی نادروستە و چاکی بکە تاکو Rezan Ali Karim, 21 بنووسێت.
<?php
$first name = "Rezan";
$2ndName = "Ali";
$last-name = "Karim";
$age = 21
echo $First_name . " " . $2ndName . " " . $last-name . ", " . $age;
?>Show answerوەڵامەکە پیشان بدە
<?php
$first_name = "Rezan";
$second_name = "Ali";
$last_name = "Karim";
$age = 21;
echo $first_name . " " . $second_name . " " . $last_name . ", " . $age;
?>Rezan Ali Karim, 21
$first name: a space is not allowed in a name. Use$first_name.$2ndName: a name cannot start with a number. Use$second_name.$last-name: a hyphen is a minus sign. Use$last_name.$age = 21: the semicolon is missing.$First_name: wrong capital letter. The variable is$first_name.
$first name: لە ناودا ڕێگەپێدراو نییە.$first_nameبەکاربهێنە.$2ndName: ناو ناتوانێت بە ژمارە دەست پێبکات.$second_nameبەکاربهێنە.$last-name: هێڵ نیشانەی کەمە.$last_nameبەکاربهێنە.$age = 21: ونە.$First_name: پیتی گەورەی نادروست.$first_nameـە.
Without running it, write down what this script prints. Then run it and check.
بەبێ ئەوەی کاری پێبکەیت، بنووسە ئەم چی دەنووسێت. پاشان کاری پێبکە و پشکنینی بۆ بکە.
<?php
$a = 5;
$b = $a;
$a = 9;
$c = $b;
$b = 1;
echo $a, " ", $b, " ", $c;
?>Show answerوەڵامەکە پیشان بدە
It prints 9 1 5. $b copies 5 from $a. Then $a becomes 9. $c copies 5 from $b. Finally $b becomes 1, but $c keeps its own copy, 5.
9 1 5 دەنووسێت. $b ژمارە 5 لە $a لەبەر دەگرێتەوە. پاشان $a دەبێتە 9. $c ژمارە 5 لە $b لەبەر دەگرێتەوە. لە کۆتاییدا $b دەبێتە 1، بەڵام $c کۆپییەکەی خۆی دەپارێزێت، کە 5ـە.
9 1 5
Store a name, a city and an age in three variables, and an item (book) in a fourth. Using double quotes, print My name is Hana. I live in Erbil. I am 20 years old. and, on a new line, I have 3 books. (the s must come straight after the variable).
ناوێک، شارێک و تەمەنێک لە سێ و شتێک (book) لە چوارەمدا هەڵبگرە. بە جووت بنووسە: My name is Hana. I live in Erbil. I am 20 years old. و لە دێڕێکی نوێدا I have 3 books. (پیتی s دەبێت ڕاستەوخۆ دوای بێت).
Hintئاماژە
Use {$item}s.
{$item}s بەکاربهێنە.
Show answerوەڵامەکە پیشان بدە
<?php
$name = "Hana";
$city = "Erbil";
$age = 20;
$item = "book";
echo "My name is $name. I live in $city. I am $age years old.<br>";
echo "I have 3 {$item}s.";
?>My name is Hana. I live in Erbil. I am 20 years old. I have 3 books.
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
My name is Hana. I live in Erbil. I am 20 years old.<br>I have 3 books.Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 8
{$item}skeeps the s outside the name.{$item}sپیتی s لە دەرەوەی ناوەکە دەهێڵێتەوە.
Reviewپێداچوونەوە
The lecture slides end with "Thank you for your attention! Any questions?" Here are the questions a good student asks. Answer each one in your own words first, then open the answer.
سلایدەکانی وانەکە بەم دەقە کۆتاییان دێت: «سوپاس بۆ سەرنجتان! هیچ پرسیارێک؟» ئەمانە ئەو پرسیارانەن کە خوێندکارێکی باش دەیانکات. سەرەتا بە وشەی خۆت وەڵامی هەر یەکێکیان بدەرەوە، پاشان وەڵامەکە بکەرەوە.
What is a variable?گۆڕاو چییە؟
A named container: a place in memory that holds one value. The value can change while the script runs.
لەخۆگرێکی ناونراوە: شوێنێک لە کە یەک هەڵدەگرێت. دەتوانێت لە کاتی کارکردنی بگۆڕێت.
How do you create a variable in PHP?لە PHPـدا چۆن گۆڕاوێک دروست دەکەیت؟
Assign a value to it, for example $age = 22;. There is no separate declaration step.
پێ بدە، بۆ نموونە $age = 22;. هەنگاوێکی جیا بۆ ڕاگەیاندن نییە.
What are the rules for variable names?یاساکانی ناوی گۆڕاوەکان چین؟
Start with $, then a letter or underscore (not a number); then only letters, numbers and underscores; no spaces or symbols; any length; case-sensitive; and $this is reserved.
بە $ دەست پێدەکات، پاشان پیتێک یان (نەک ژمارە)؛ دواتر تەنها پیت، ژمارە و ؛ بێ و بێ هێما؛ هەر درێژییەک؛ ؛ و $this پارێزراوە.
Are $total and $Total the same variable?ئایا $total و $Total هەمان گۆڕاون؟
No. Variable names are case-sensitive, so they are two different variables.
نەخێر. ناوی ، بۆیە دوو جیاوازن.
After $favorite = $color;, what happens to $favorite if $color changes later?دوای $favorite = $color;، ئەگەر دواتر $color بگۆڕێت، چی بەسەر $favorite دێت؟
Nothing. $favorite got a copy of the value; the two variables are separate boxes.
هیچ. $favorite کۆپییەکی وەرگرتووە؛ هەردوو دوو سندوقی جیاوازن.
Why do you write "{$type}cage" instead of "$typecage"?بۆچی "{$type}cage" دەنووسیت نەک "$typecage"؟
Without braces PHP reads the name $typecage, which does not exist. The braces show where the name ends.
بەبێ کەوانە، PHP ناوی $typecage دەخوێنێتەوە کە بوونی نییە. کەوانەکان پیشان دەدەن ناوەکە لە کوێ کۆتایی دێت.
Does "Undefined variable" stop the script?ئایا «Undefined variable» دەسنووسەکە ڕادەگرێت؟
No. It is a warning. PHP uses an empty value and continues, so the page may show wrong or missing text.
نەخێر. . PHP بەتاڵ بەکاردەهێنێت و بەردەوام دەبێت، بۆیە لەوانەیە دەقی نادروست یان ناتەواو پیشان بدات.
Why is print_r() useful, and why do we put it inside <pre>?بۆچی print_r() بەسوودە و بۆچی دەیخەینە ناو <pre>ەوە؟
It prints values, especially arrays, in a readable, indented form. <pre> keeps its line breaks and spaces in the browser.
، بەتایبەتی ، بە شێوەیەکی خوێنراو و پێشبۆشکراو دەنووسێت. <pre> دێڕە نوێیەکان و لە دەپارێزێت.
What does "loosely typed" mean?«loosely typed» واتای چییە؟
You do not declare a variable's type. PHP decides the type from the value, and the same variable can hold different types at different times.
تۆ جۆری ڕاناگەیەنیت. PHP جۆرەکە لە دیاری دەکات و هەمان دەتوانێت لە کاتی جیاوازدا جۆری جیاواز هەڵبگرێت.
Key points
خاڵە سەرەکییەکان
- A variable is a named box in memory.
=puts a value into it; a new value replaces the old one. - Names start with
$and a letter or underscore, contain only letters, numbers and underscores, and are case-sensitive. - Choose clear names in one style:
$first_nameor$firstName. - Text needs straight quotes. In double quotes, variables are replaced by their values; use
{$var}when letters follow. echoandprint_r()show values;print_r()is best for arrays.- A variable lasts for the whole script (all PHP sections of one file, for one run).
- Using a variable before giving it a value gives a warning, not a fatal error.
- PHP is loosely typed: you never declare a type.
- سندوقێکی ناونراوە لە .
=دەخاتە ناوی؛ جێگەی دەگرێتەوە. - ناوەکان بە
$و پیتێک یان دەست پێدەکەن، تەنها پیت و ژمارە و لەخۆ دەگرن و . - ناوی ڕوون بە یەک شێواز هەڵبژێرە:
$first_nameیان$firstName. - دەق پێویستی بە ڕاستە. لەناو جووتدا بە دەگۆڕدرێن؛ کاتێک پیت دوایان دێت
{$var}بەکاربهێنە. echoوprint_r()پیشان دەدەن؛print_r()بۆ باشترە.- بۆ هەموو دەمێنێتەوە (هەموو کەرتەکانی PHPی یەک ، بۆ یەک جار کارکردن).
- بەکارهێنانی پێش ئەوەی پێ بدرێت دەدات، نەک .
- PHP جۆری داوا ناکات (): هەرگیز جۆر ڕاناگەیەنیت.
Reviewپێداچوونەوە
Words to knowوشە گرنگەکان
Reviewپێداچوونەوە
The glossary of the Kurdish notesفەرهەنگۆکی تێبینییە کوردییەکان
| # | English (in the notes)ئینگلیزی (لە تێبینییەکاندا) | Kurdish (in the notes)کوردی (لە تێبینییەکاندا) | CêIT termزاراوەی CêIT |
|---|---|---|---|
| 1 | Open Source | کۆدکراوە | |
| 2 | Server-side | ڕاژەبەند | |
| 3 | Client-side Language | زمانی ڕاژەخواز | |
| 4 | Programmer | بەرنامەساز | |
| 5 | Database | بنکەدراوە | |
| 6 | Feature | تایبەتمەندێتی | — |
| 7 | Server | ڕاژەکار | |
| 8 | Client | ڕاژەخواز | |
| 9 | Web Browser | تۆڕگەڕ | |
| 10 | Website | تۆڕگە | |
| 11 | Syntax | داڕستن | |
| 12 | Valid | ڕێگەپێدراو / ڕەوا | — |
| 13 | Password | تێپەڕەوشە | |
| 14 | Username | ناوی بەکارهێن | |
| 15 | Static | نەگۆڕ | |
| 16 | Dynamize | بزاوتن | |
| 17 | Script | دەسنووس | |
| 18 | Statement | ڕستە | |
| 19 | Instruction | ڕێنمایی | — |
| 20 | Error Message | هەڵەنامە | |
| 21 | Visitor | سەردانیکەر | — |
| 22 | Section | کەرت | — |
| 23 | Double Quotation Mark | جووتە نیشانەی گوتە | " |
| 24 | Single Quotation Mark | تاکە نیشانەی گوتە | ' |
| 25 | Character | نووسە | |
| 26 | String | زنجیرەنووسە | |
| 27 | Whole Number | ژمارەی تەواو | |
| 28 | Output | دەرخستە | |
| 29 | Input | نێوخستە | |
| 30 | Comment | لێدوان | |
| 31 | Slash | لارەهێڵ | — |
| 32 | Underscore | بنهێڵ | |
| 33 | Variable Name | ناوەگۆڕاو | |
| 34 | Outputting | دەرخستەکردن | — |
| 35 | Array | ڕیزەک | |
| 36 | Indent | پێشبۆش | — |
| 37 | Paragraph | کۆدەق، چەپکەڕستە، کۆڕستە | — |
A few words are written differently in the glossary and in the body of the notes: «بزاوتن» and «بزوواندن» (dynamize), «سەردانیکەر» and «سەردانکەران» (visitor, visitors), «ڕەوا» and «ڕەوان» (valid), «زمانی ڕاژەخواز» and «زمانێکی بێڕاژە» (client-side language). The notes also spell Outputting as "Outputing".
هەندێک وشە لە فەرهەنگۆکەکە و لە ناوەڕۆکی تێبینییەکاندا بە دوو شێوەی جیاواز نووسراون: «بزاوتن» و «بزوواندن»، «سەردانیکەر» و «سەردانکەران»، «ڕەوا» و «ڕەوان»، «زمانی ڕاژەخواز» و «زمانێکی بێڕاژە». هەروەها وشەی ئینگلیزیی Outputting لە تێبینییەکاندا بە هەڵە "Outputing" نووسراوە.
Videoڤیدیۆ
Watch a videoسەیری ڤیدیۆیەک بکە
A video that viewers liked a lot and praised in their comments, chosen for this lesson.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.
A Kurdish (Sorani) lesson: what a variable is, why we use it, and what we can do with variables in PHP.
وانەیەک بە کوردیی سۆرانی: چییە، بۆچی بەکاری دەهێنین و چی دەتوانین بە بکەین لە .
Kurdish (Sorani) video about PHP variables.
ڤیدیۆیەکی کوردیی سۆرانی دەربارەی .
Lesson complete!وانەکە تەواو بوو!
Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.
Key termsزاراوە سەرەکییەکان
Data typesجۆرەکانی دراوە
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 ن.