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ـدا چۆن دروست بکەیت، یاساکانی ناونانیان چین، چۆن بگۆڕیت و لەبەریان بگریتەوە، چۆن لەسەر پیشانیان بدەیت و کاتێک نییە چی ڕوودەدات.

26 min read25 خولەک خوێندنەوە · 24 stepsهەنگاو

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 echo and print_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ـدا، بەکارهێنانی .

Definitionپێناسە

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⁩ هەڵبگرێت:

A variable named age that holds 22گۆڕاوێک بە ناوی age کە ⁦22⁩ هەڵدەگرێتPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
$age = 22;
A variable is a labelled jarگۆڕاو وەک قاپێکی ناونووسکراو

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 دوو قاپی جیاوازن.

4 | PHP Variable and Data Type Tutorial | 2023 | Learn PHP Full Course for BeginnersDani Krossing · Englishئینگلیزی · 165K viewsبینین · 3.1K likesلایک · Partبەشی 2:45–6:16 (ofلە 17:29)

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.

لەناو ، ناوێکە بۆ شوێنێک لە : یەک شوێن، یان کۆمەڵێک شوێن. لە هەر ساتێکدا، هەمان ئەو شتەیە کە ئێستا لەو شوێنەدا هەڵگیراوە.

Three lines of PHP code on the left: $name = "Dilan";, $age = 22; and $price = 750.5;. Arrows lead to three boxes in memory (RAM) on the right. Each box has a label with the variable name and holds its value. Beside each box is its type: string, integer, float.سێ دێڕ کۆدی PHP لە لای چەپ: $name = "Dilan"; و $age = 22; و $price = 750.5;. تیرەکان دەچن بۆ سێ سندوق لە بیرگەدا (RAM) لە لای ڕاست. هەر سندوقێک لیبڵێکی پێوەیە کە ناوی گۆڕاوەکەی لەسەرە و نرخەکەی لەناو سندوقەکەدایە. لە تەنیشت هەر سندوقێکەوە جۆرەکەی نووسراوە: زنجیرەنووسە، ژمارەی تەواو، ژمارەی کەرتی.
Each assignment puts a value into a labelled box (a memory location).هەر دەخاتە ناو سندوقێکی ناونووسکراوەوە (شوێنێک لە ).CêIT

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ـدا بۆ دروستکردنی پێویستت بە فەرمانێکی تایبەت نییە. یەکەم جار کە پێدەدەیت دروست دەبێت. بەم کارە دەوترێت . وەک زۆربەی ، بۆ ئەم کارە هێمای یەکسان = بەکاردێت. هێمای $، پاشان ناوەکە، =، و ; بنووسە.

The parts of $age = 22;بەشەکانی $age = 22;
PartبەشExampleنموونەWhat it doesچی دەکات
$$ageTells PHP: this is a variable.بە PHP دەڵێت: ئەمە .
nameناوageThe variable's name: the label on the box.ناوی : لیبڵەکەی سەر سندوقەکە.
==The : it puts the value on the right into the variable on the left.: لای ڕاست دەخاتە ناو لای چەپەوە.
value22What you store: a number, text inside quotes, and so on.ئەوەی هەڵیدەگریت: ژمارەیەک، دەقێک لەناو و هتد.
;;Ends the .کۆتایی بە دەهێنێت.
Quick checkپشکنینی خێراWhich line stores the number 22 in a variable called age?کام دێڕ ژمارە ⁦22⁩ لە بە ناوی ageدا هەڵدەگرێت؟

Creating a variable: assignmentدروستکردنی گۆڕاو: دانانی نرخ

First examplesیەکەم نموونەکان

A variable that holds a numberگۆڕاوێک کە ژمارەیەک هەڵدەگرێتnumber.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
   $var1 = 10;
   echo $var1;
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

$var1 gets the value 10.

$var1 ⁦10⁩ وەردەگرێت.

Lineدێڕی 3

echo sends the value of $var1 to the browser.

echo $var1 دەنێرێت بۆ .

A variable that holds textگۆڕاوێک کە دەق هەڵدەگرێتtext.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
   $var2 = "Hello World";
   echo $var2;
?>
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 نازانێت چیت هەڵگرتووە: وا دەزانێت وشەکە ناوی و بە دەوەستێت.

Text, a whole number, a fraction and a negative numberدەق، ژمارەیەکی تەواو، ژمارەیەکی کەرتی و ژمارەیەکی سالبvalues.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$name = "Shilan Ahmad";
$age = 22;
$price = 750.50;
$temperature = -5;

echo $name . "<br>";
echo $age . "<br>";
echo $price . "<br>";
echo $temperature;
?>
Explain 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.50 is printed as 750.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:

دەتوانیت چەند لە دوای یەک دروست بکەیت. ئەم نموونە کورتەی سلایدەکانی کۆرسەکە ، و هەڵدەگرێت:

Three variablesسێ گۆڕاوthree.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$text = "Hello world!";
$x = 10;
$y = 3.5;

echo $text;
echo "<br>";
echo $x;
echo "<br>";
echo $y;
?>

Creating 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:

لە کاتی کارکردنی دەتوانێت بگۆڕێت؛ هەر بۆیە پێی دەوترێت . بۆ ئەوەی پێ بدەیت، دیسان = بەکاربهێنە:

The value changes from 10 to 100نرخەکە لە ⁦10⁩ەوە دەبێتە ⁦100⁩change.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$var1 = 10;
echo $var1;
echo "<br>";
$var1 = 100;
echo $var1;
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2-3

First $var1 holds 10, and echo shows 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 دەخاتە جێگەی:

Changing a value: Green becomes Blueگۆڕینی نرخ: Green دەبێتە Bluecolor.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$color = "Green";
echo $color . "<br>";
$color = "Blue";      // the old value is replaced
echo $color;
?>
Three panels. 1. Assign: $color = "Green"; makes a box labelled $color that holds "Green". 2. Change: $color = "Blue"; puts "Blue" in the same box; the old "Green" is crossed out. 3. Copy: $favorite = $color; makes a second box labelled $favorite that also holds "Blue".سێ بەش. ١. دانانی نرخ: $color = "Green"; سندوقێک بە لیبڵی $color دروست دەکات کە "Green"ی تێدایە. ٢. گۆڕینی نرخ: $color = "Blue"; وشەی "Blue" دەخاتە هەمان سندوقەوە و "Green"ـی کۆن هێڵی بەسەردا هاتووە. ٣. لەبەرگرتنەوە: $favorite = $color; سندوقێکی دووەم بە لیبڵی $favorite دروست دەکات کە ئەویش "Blue"ی تێدایە.
Assign, change, copy: what happens in memory.دانان، گۆڕین، لەبەرگرتنەوە: لە چی ڕوودەدات.CêIT

Creating 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:

هەروەها دەتوانیت لەبەر بگریتەوە بۆ ناو تر. کۆپییەکی وەردەگرێت. هەردوو دوو سندوقی جیاوازن، بۆیە ئەگەر دواتر یەکێکیان بگۆڕیت، ئەوی تر ناگۆڕێت:

Copying a value into a second variableلەبەرگرتنەوەی نرخێک بۆ گۆڕاوێکی دووەمcopy.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

$favoriteName gets a copy of the value of $name ("Dilan").

$favoriteName کۆپییەکی $name ⁦("Dilan")⁩ وەردەگرێت.

Lineدێڕی 6

Later, $name gets a new value.

دواتر $name وەردەگرێت.

Lineدێڕی 8

$favoriteName still holds "Dilan". It is a separate box.

$favoriteName هێشتا "Dilan"ی تێدایە، چونکە سندوقێکی جیاوازە.

Quick checkپشکنینی خێراAfter $a = 5; $b = $a; $a = 9; what is the value of $b?دوای $a = 5; $b = $a; $a = 9;، $b چەندە؟

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 هیچی بۆ پیشان نادات:

A variable that holds an empty stringگۆڕاوێک کە زنجیرەنووسەیەکی بەتاڵی تێدایەempty.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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 پێش ئەوەی هیچ شتێکی ناو جێبەجێ بکات، بە دەوەستێت.

  1. 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.)
  2. After the $, the first character must be a letter or an _. It can not be a number.
  3. After that, the name may contain only letters, numbers and underscores (A-Z, a-z, 0-9, _).
  4. No spaces, and no other symbols such as + - % ( ) . & ?. PHP reads these as operators or punctuation, not as part of the name.
  5. A name can be any length. $x and $numberOfStudentsInTheClass are both allowed.
  6. Names are : $age and $AGE are two different variables (see the next section).
  7. $this is reserved. PHP uses it for objects, so you cannot use it as your own variable (Fatal error: Cannot re-assign $this).
  1. هەموو ناوێکی بە هێمای دۆلار $ دەست پێدەکات. ئەمە بە PHP دەڵێت کە ئەمە . (لە زمانی ئایتیدا ئەم جۆرە هێمایەی پێش ناو بە sigil ناسراوە.)
  2. دوای $، یەکەم دەبێت پیتێک یان _ بێت. نابێت ژمارە بێت.
  3. لەوە بەدواوە، ناوەکە تەنها دەتوانێت پیت، ژمارە و لەخۆ بگرێت (A-Z، a-z، 0-9، _).
  4. ڕێگەپێدراو نییە، هەروەها هیچ هێمایەکی تری وەک + - % ( ) . & ?. PHP ئەمانە وەک یان نیشانەی خاڵبەندی دەخوێنێتەوە، نەک وەک بەشێک لە ناوەکە.
  5. ناو دەتوانێت بە هەر درێژییەک بێت. $x و $numberOfStudentsInTheClass هەردووکیان ڕێگەپێدراون.
  6. ناوەکان : $age و $AGE دوو جیاوازن (بڕوانە بەشی داهاتوو).
  7. $this پارێزراوە. PHP بۆ بەکاری دەهێنێت، بۆیە ناتوانیت وەک خۆت بەکاری بهێنیت (Fatal error: Cannot re-assign $this).
Valid and invalid variable namesناوی ڕێگەپێدراو و ڕێگەپێنەدراوی
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 بۆ پاراستوویەتی.
Quick checkپشکنینی خێراWhich of these is a valid variable name?کامیان ناوێکی ڕێگەپێدراوی ؟

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 هەرگیز نانووسرێت: پێش جێبەجێبوونی هەر دێڕێک، هەموو ڕادەگرێت.

A name that starts with a numberناوێک کە بە ژمارە دەست پێدەکاتnames.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$1book = 1;
echo "done";

A hyphen and the reserved name $this give these messages:

هێڵی ناوەڕاست (-) و ناوی پارێزراوی $this ئەم پەیامانە دەدەن:

Two more invalid names and their messagesدوو ناوی ڕێگەپێنەدراوی تر و پەیامەکانیانTEXT
$this-variable = 1;  ->  Parse error: syntax error, unexpected token "="
$this = 5;           ->  Fatal error: Cannot re-assign $this

Variable 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، سندوقی بە تەواوی جیاوازن. ئەمە تەڵەیەکی باوە بۆ تازەفێربووان.

Two names that differ in one letterدوو ناو کە تەنها لە یەک پیتدا جیاوازنcase.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$my_variable = "Hello World";
$my_Variable = "Hello World
      Again";
echo $my_Variable;
?>
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

echo prints 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 بەدوای ناوێکی نوێدا دەگەڕێت، هیچ نادۆزێتەوە و پیشان دەدات:

One wrong letterیەک پیتی نادروستtrap.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$name = "Hana";
echo $Name;
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

$Name was never given a value. Only $name (small n) exists.

$Name هەرگیز پێ نەدراوە. تەنها $name (n بچووک) هەیە.

Quick checkپشکنینی خێراWhat does this print? $color = "red"; $Color = "blue"; echo $color;ئەمە چی دەنووسێت؟ $color = "red"; $Color = "blue"; echo $color;

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:

ناو زۆرجار لە زیاتر لە وشەیەک پێکدێت، بەڵام ناتوانیت بەکاربهێنیت. شێوازی ناونان بە ئارەزووی کۆدنووسەکەیە. دوو شێواز باون:

Two common naming stylesدوو شێوازی باوی ناونان
StyleشێوازExampleنموونەHow it worksچۆن کار دەکات
$first_nameAll words in lowercase, joined with an underscore _.هەموو وشەکان بە پیتی بچووک و بە _ پێکەوە دەلکێن.
$firstNameThe 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 ناسیت.

age.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$age = 22;
echo $age;
?>

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>

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 کار دەکات:

today.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$today = "Friday";
print_r($today);
?>

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>";
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

An array with three items. The item c is itself an array of three letters.

بە سێ . c خۆی بە سێ پیت.

Lineدێڕی 3-5

<pre> keeps the line breaks; print_r() prints the array.

<pre> دێڕە نوێیەکان دەپارێزێت؛ print_r() دەنووسێت.

Quick checkپشکنینی خێراYou want to see everything inside an array, neatly. Which is the best choice?دەتەوێت هەموو ناوەڕۆکی بە ڕێکی ببینیت. کامیان باشترین هەڵبژاردەیە؟

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 لادەبات و دادەنێت. بەمە دەوترێت . ئەم کارە ناکات: دەقەکە ڕێک وەک خۆی دەنووسێت (زیاتر لە وانەی زنجیرەنووسەکان).

Single quotes and double quotesنیشانەی گوتەی تاک و جووتcity.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$city = "Halabja";
echo 'I live in $city.';
echo "<br>";
echo "I live in $city.";
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

Single quotes: $city is printed as it is.

تاک: $city وەک خۆی دەنووسرێت.

Lineدێڕی 5

Double quotes: $city is 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 بنووسیت:

Wrong: PHP looks for $typeCageنادروست: PHP بەدوای $typeCage دەگەڕێتcage.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$type = "bird";
echo "Keep the $typeCage 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 دەزانێت ناوەکە ڕێک لە کوێ کۆتایی دێت:

Right: curly braces mark the end of the nameدروست: کەوانەی لوولی کۆتایی ناوەکە دیاری دەکاتcage.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$type = "bird";
echo "Keep the {$type}cage clean";
?>
Quick checkپشکنینی خێرا$type holds "bird". Which line prints birdcage?$type "bird"ی تێدایە. کام دێڕ birdcage دەنووسێت؟

Basic PHP Syntax - PHP 8 TutorialProgram With Gio · Englishئینگلیزی · 237K viewsبینین · 4.8K likesلایک · Partبەشی 7:45–8:52 (ofلە 12:32)

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⁩ دەنووسێت.

hello.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
	echo "<p>Hello World</p>";
?>

Inside a complete HTML page, a PHP section can create a variable and print it:

لەناو تەواوی HTMLـدا، کەرتێکی PHP دەتوانێت دروست بکات و بینووسێت:

A whole page with one PHP sectionلاپەڕەتۆڕێک بە یەک کەرتی PHPgreeting.phpPHP
<html>
	<body>
		<?php
			$greeting = "Hello World";
			echo "<p>$greeting</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ی تێدایە، مەگەر پێ بدەیت.

One variable, two PHP sectionsیەک گۆڕاو، دوو کەرتی PHPgreeting.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<html>
	<body>
		<?php
			$greeting = "Hello World";
			echo "<p>$greeting</p>";
		?>

		<p>I am a paragraph.</p>

		<?php
			echo "<p>$greeting again</p>";
		?>
	</body>
</html>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3-6

The first PHP section creates $greeting and 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. $greeting still holds "Hello World".

کەرتی دووەمی PHP. $greeting هێشتا "⁦Hello World⁩"ی تێدایە.

Quick checkپشکنینی خێراA file has two PHP sections. The first one sets $city = "Erbil";. What does echo $city; in the second section print? دوو کەرتی PHPی تێدایە. کەرتی یەکەم $city = "Erbil"; دادەنێت. echo $city; لە کەرتی دووەمدا چی دەنووسێت؟

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.";
?>
Quick checkپشکنینی خێراWhat happens when a script uses a variable that was never given a value?چی ڕوودەدات کاتێک بەکاردەهێنێت کە هەرگیز پێ نەدراوە؟

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، دەبێت پێش بەکارهێنانی ، ناو و جۆرەکەی ڕابگەیەنێت. لەوە بەدواوە، تەنها ئەو جۆرە هەڵدەگرێت:

Java (for comparison): the type comes firstJava (بۆ بەراورد): جۆرەکە یەکەم دێتTEXT
int age = 22;          // age may only ever hold a whole number
String name = "Hana";  // name may only hold text

In 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 هەر جارێک هەڵیبژاردووە:

One variable, three typesیەک گۆڕاو، سێ جۆرtypes.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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>";
?>
Quick checkپشکنینی خێراWhat does var_dump($x); show after $x = 5; $x = "five";?دوای $x = 5; $x = "five";، var_dump($x); چی پیشان دەدات؟

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⁩ پیشان دەدات.

Ten common mistakes with variablesدە هەڵەی باو لەگەڵ
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 $NameSpell 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

ڕاهێنانەکان

ExerciseڕاهێنانLab task: store a greeting and a numberئەرکی تاقیگە: سڵاوێک و ژمارەیەک هەڵبگرە

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وەڵامەکە پیشان بدە
example2.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$txt = "Hello World!";
$number = 10;
// Display variables value
echo $txt;
echo "<br>";
echo $number;
?>
Explain 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> ژمارەکە دەخاتە دێڕێکی نوێوە.

ExerciseڕاهێنانLab task: three coloursئەرکی تاقیگە: سێ ڕەنگ

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وەڵامەکە پیشان بدە
example3.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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>";
?>
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 سێ جیاوازن. ناوی .

ExerciseڕاهێنانFix the namesناوەکان چاک بکە

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 بنووسێت.

Starter code (broken)کۆدی سەرەتا (تێکچوو)names.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$first name = "Rezan";
$2ndName = "Ali";
$last-name = "Karim";
$age = 21
echo $First_name . " " . $2ndName . " " . $last-name . ", " . $age;
?>
Show answerوەڵامەکە پیشان بدە
names-fixed.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$first_name = "Rezan";
$second_name = "Ali";
$last_name = "Karim";
$age = 21;
echo $first_name . " " . $second_name . " " . $last_name . ", " . $age;
?>
  1. $first name: a space is not allowed in a name. Use $first_name.
  2. $2ndName: a name cannot start with a number. Use $second_name.
  3. $last-name: a hyphen is a minus sign. Use $last_name.
  4. $age = 21: the semicolon is missing.
  5. $First_name: wrong capital letter. The variable is $first_name.
  1. $first name: لە ناودا ڕێگەپێدراو نییە. $first_name بەکاربهێنە.
  2. $2ndName: ناو ناتوانێت بە ژمارە دەست پێبکات. $second_name بەکاربهێنە.
  3. $last-name: هێڵ نیشانەی کەمە. $last_name بەکاربهێنە.
  4. $age = 21: ونە.
  5. $First_name: پیتی گەورەی نادروست. $first_nameـە.
ExerciseڕاهێنانPredict the outputدەرخستەکە پێشبینی بکە

Without running it, write down what this script prints. Then run it and check.

بەبێ ئەوەی کاری پێبکەیت، بنووسە ئەم چی دەنووسێت. پاشان کاری پێبکە و پشکنینی بۆ بکە.

predict.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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ـە.

Outputدەرئەنجام
9 1 5
ExerciseڕاهێنانAn "about me" lineدێڕێک دەربارەی خۆم

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وەڵامەکە پیشان بدە
about.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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.";
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 8

{$item}s keeps 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.

سلایدەکانی وانەکە بەم دەقە کۆتاییان دێت: «سوپاس بۆ سەرنجتان! هیچ پرسیارێک؟» ئەمانە ئەو پرسیارانەن کە خوێندکارێکی باش دەیانکات. سەرەتا بە وشەی خۆت وەڵامی هەر یەکێکیان بدەرەوە، پاشان وەڵامەکە بکەرەوە.

Review questionsپرسیارەکانی پێداچوونەوە
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_name or $firstName.
  • Text needs straight quotes. In double quotes, variables are replaced by their values; use {$var} when letters follow.
  • echo and print_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فەرهەنگۆکی تێبینییە کوردییەکان

The 37 words of «واتای وشەکان», as written in the notes٣٧ وشەکەی «واتای وشەکان»، وەک لە تێبینییەکاندا نووسراون
#English (in the notes)ئینگلیزی (لە تێبینییەکاندا)Kurdish (in the notes)کوردی (لە تێبینییەکاندا)CêIT termزاراوەی CêIT
1Open Sourceکۆدکراوە
2Server-sideڕاژەبەند
3Client-side Languageزمانی ڕاژەخواز
4Programmerبەرنامەساز
5Databaseبنکەدراوە
6Featureتایبەتمەندێتی—
7Serverڕاژەکار
8Clientڕاژەخواز
9Web Browserتۆڕگەڕ
10Websiteتۆڕگە
11Syntaxداڕستن
12Validڕێگەپێدراو / ڕەوا—
13Passwordتێپەڕەوشە
14Usernameناوی بەکارهێن
15Staticنەگۆڕ
16Dynamizeبزاوتن
17Scriptدەسنووس
18Statementڕستە
19Instructionڕێنمایی—
20Error Messageهەڵەنامە
21Visitorسەردانیکەر—
22Sectionکەرت—
23Double Quotation Markجووتە نیشانەی گوتە "
24Single Quotation Markتاکە نیشانەی گوتە '
25Characterنووسە
26Stringزنجیرەنووسە
27Whole Numberژمارەی تەواو
28Outputدەرخستە
29Inputنێوخستە
30Commentلێدوان
31Slashلارەهێڵ—
32Underscoreبنهێڵ
33Variable Nameناوەگۆڕاو
34Outputtingدەرخستەکردن—
35Arrayڕیزەک
36Indentپێشبۆش—
37Paragraphکۆدەق، چەپکەڕستە، کۆڕستە—

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.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.

PHP for beginners [ Kurdish ] - 4 - پی ئێچ پی بۆ ئاستی سەرەتاییK97 Media · Kurdish (Sorani)کوردی (سۆرانی) · 2.9K viewsبینین · 82 likesلایک

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زاراوە سەرەکییەکان

Next lessonوانەی داهاتوو

Data typesجۆرەکانی دراوە

References and further readingسەرچاوەکان و خوێندنەوەی زیاتر

This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.