Chapterبەش 7 · Lectureوانە 7.1

Functionsکردارەکان

Write code once and use it many times: define and call your own PHP functions, send values in with parameters and default values, get results back with return, and learn the difference between passing by value and passing by reference. Includes the Lab 4 function examples, with every bug in the lab sheet found and fixed.

کۆد یەک جار بنووسە و چەندین جار بەکاری بهێنە: خۆت لە PHPـدا بناسێنە و بانگیان بکە، بە و بنێرە ناوەوە، بە return ئەنجام وەربگرەوە و جیاوازیی نێوان و فێر ببە. نموونەکانی تاقیگەی ٤یش لەخۆ دەگرێت و هەموو پەڕەی تاقیگەکە دۆزراونەتەوە و چاک کراون.

53 min read51 خولەک خوێندنەوە · 48 stepsهەنگاو

What you will learnچی فێر دەبیت

  • Explain what a function is and tell built-in functions from user-defined ones.
  • Define a function with the keyword function, name it correctly, and call it, also before its definition.
  • Send values into a function with parameters and arguments, including default values.
  • Use return to give a result back, and know that code after return never runs.
  • Explain pass by value and pass by reference (&), and when each is useful.
  • Read and fix common function errors: reserved names, wrong or missing names, redeclared functions, stray braces, missing arguments.
  • Understand that variables inside a function are local, and pass values in instead of using global.
  • ڕوونی بکەیتەوە چییە و لە جیا بکەیتەوە.
  • بە function بناسێنیت، بە دروستی ناوی لێ بنێیت و بانگی بکەیت، تەنانەت پێش ناساندنەکەشی.
  • بە و بنێریتە ناو ، لەگەڵ .
  • return بەکاربهێنیت بۆ گەڕاندنەوەی ئەنجام و بزانیت کۆدی دوای return هەرگیز جێبەجێ ناکرێت.
  • و (&) ڕوون بکەیتەوە و بزانیت هەریەکەیان کەی بەسوودە.
  • بخوێنیتەوە و چاکیان بکەیتەوە: ناوی پارێزراو، ناوی هەڵە یان نەبوو، کە دوو جار ناسێنراون، کەوانەی زیادە، کەم.
  • تێبگەیت کە ناو ناوخۆیین و لە جیاتی global بنێریتە ناوەوە.

Why functions?بۆچی کردارەکان؟

Imagine you build a website with ten pages. Every page must show the same greeting, work out the same total, or do the same check. You could copy the same ten lines of code into all ten pages. But then one small mistake must be fixed ten times, and sooner or later you forget one page.

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

A solves this. You write the code once, give it a name, and then use it as many times as you like, from any place in your page. If you find a mistake, you fix it in one place, and every page that uses the function is fixed too.

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

The course lecture says it in one sentence: the real power of PHP comes from its functions. PHP itself comes with more than a thousand ready-made functions, and you can add as many of your own as you need.

وانەی کۆرسەکە لە یەک ڕستەدا دەیڵێت: هێزی ڕاستەقینەی PHP لە . PHP خۆی زیاتر لە هەزار لەگەڵدایە و تۆش دەتوانیت هەر چەندت پێویست بێت خۆتی بۆ زیاد بکەیت.

A juice machine at the bazaarئامێری ئاوی میوە لە بازاڕدا

Think of a juice stand in the bazaar. The owner does not squeeze every orange by hand; he has a juice machine. He built it (or bought it) once: that is like a . Each time a customer comes, he presses the button: that is like a . The openings where fruit goes in are the . The actual fruit he puts in this time (two oranges, one pomegranate) are the . The squeezing inside the machine is the function's body. The glass of juice that comes out is the . And if a customer does not say which fruit, the machine uses oranges: that is a .

بیر لە دوکانێکی ئاوی میوە بکەرەوە لە بازاڕدا. خاوەنەکەی هەموو پرتەقاڵێک بە دەست ناگوشێت؛ ئامێرێکی گوشینی هەیە. ئامێرەکەی تەنها یەک جار دروست کردووە (یان کڕیوە): ئەمە وەک . هەر جارێک کڕیارێک دێت، دوگمەکە دادەگرێت: ئەمە وەک . ئەو کونانەی میوەکەیان تێدا دادەنرێت . ئەو میوەیەی ئەم جارە دەیخاتە ناوی (دوو پرتەقاڵ و یەک هەنار) . گوشینەکەی ناو ئامێرەکە ناوەڕۆکی . ئەو پەرداخە ئاوە میوەیەی دێتە دەرەوە . ئەگەر کڕیارێکیش نەڵێت چ میوەیەکی دەوێت، ئامێرەکە پرتەقاڵ بەکاردەهێنێت: ئەمە .

Where the analogy stops workingسنووری ئەم نموونەیە

The comparison breaks in two places. A real machine uses up the fruit, but a PHP function normally gets a copy of each value, so the caller's stays as it was (you will meet the exception, , later). Also, a function does not have to give anything back: some functions only print something on the page, like a machine that only shows a message on its screen.

ئەم بەراوردە لە دوو شوێندا ڕاست نییە. ئامێرێکی ڕاستەقینە میوەکە بەکاردەهێنێت و نایهێڵێت، بەڵام لە PHPـدا بە ئاسایی تەنها کۆپییەکی هەر وەردەگرێت، بۆیە بانگکەر وەک خۆی دەمێنێتەوە (دواتر ئاوارتەکەی دەناسیت: ). هەروەها پێویست ناکات هیچ شتێک بگەڕێنێتەوە: هەندێک تەنها شتێک لەسەر لاپەڕەکە دەنووسن، وەک ئامێرێک کە تەنها نامەیەک لەسەر شاشەکەی پیشان دەدات.

This lecture opens the chapter Functions and Arrays. Here you learn to define, name and call your own functions, to send values in and get values back, and to fix the most common function errors. The following lectures use functions together with arrays.

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

What is a function?کردار چییە؟

A is a named : a group of inside curly braces { }, with a name on top. The code inside does not run by itself. It runs only when you call the function by its name, and you may call it from anywhere in the page, as many times as you need.

ناونراوە: کۆمەڵێک لەناو کەوانەی لوولی { }، کە ناوێکیان لەسەرە. کۆدی ناو ئەم بلۆکە خۆی بە خۆی جێبەجێ ناکرێت. تەنها کاتێک بانگی بکەیت بە ناوەکەیەوە جێبەجێ دەکرێت و دەتوانیت لە هەر شوێنێکی لاپەڕەکەوە و هەر چەند جار پێویست بێت بانگی بکەیت.

A function can take information in (through its ), do some work, and give a result back (its ). Both are optional: a function may take no information at all, and it may give nothing back.

دەتوانێت زانیاری وەربگرێت (لە ڕێگەی )، کارێک بکات و ئەنجامێک بگەڕێنێتەوە (). هەردووکیان ئارەزوومەندانەن: لەوانەیە هیچ زانیارییەک وەرنەگرێت و هیچیش نەگەڕێنێتەوە.

Definitionپێناسە

A named block of code that does one job and can be used again and again by calling its name. It can take and give back a .

بلۆکێکی ناونراوی کۆدە کە کارێکی دیاریکراو ئەنجام دەدات و دەتوانیت بە بانگکردنی ناوەکەی چەندین جار بەکاری بهێنیتەوە. دەتوانێت وەربگرێت و بگەڕێنێتەوە.

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

In Kurdish: what a function is, why we use functions, and what we can do with them in PHP.

بە کوردی: چییە، بۆچی بەکاردەهێنین و لە چییان پێ دەکەین.

Short Kurdish (Sorani) introduction to functions

ناساندنێکی کورت بە کوردیی سۆرانی بۆ .

What is a function?کردار چییە؟

Two kinds: built-in and user-definedدوو جۆر: ئامادە و دروستکراو

  1. are created by PHP and are ready to use. You only call them. Examples: strlen(), strtoupper(), str_replace(), strstr(), max(), phpinfo().
  2. are created by you, the programmer, with the keyword function. They are the subject of this lecture.
  1. : PHP خۆی دروستی کردوون و ئامادەی بەکارهێنانن. تەنها بانگیان دەکەیت. نموونە: strlen()، strtoupper()، str_replace()، strstr()، max()، phpinfo().
  2. : خۆت، وەک بەرنامەساز، بە function دروستیان دەکەیت. بابەتی ئەم وانەیە ئەمانەن.

You call both kinds in exactly the same way: the name, then parentheses ( ) with any values inside. Here are a few built-in functions at work:

هەردوو جۆرەکە بە هەمان شێوە بانگ دەکرێن: ناوەکە، پاشان کەوانە ( ) و ئەو دەتەوێت بیاننێریت لەناویدا. ئەمەش چەند لە کاتی کارکردندا:

Calling built-in functionsبانگکردنی کردارە ئامادەکانindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
echo strlen("Kurdistan") . "<br>";
echo strtoupper("hello") . "<br>";
echo str_replace("world", "Kurdistan", "Hello world") . "<br>";
echo strstr("user@example.com", "@") . "<br>";
echo max(4, 19, 7);
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

strlen() counts the characters in a : "Kurdistan" has 9.

strlen() پیتەکانی دەژمێرێت: "Kurdistan" ٩ پیتی هەیە.

Lineدێڕی 3

strtoupper() turns the letters into capitals.

strtoupper() پیتەکان دەکات بە پیتی گەورە.

Lineدێڕی 4

str_replace() replaces "world" with "Kurdistan".

str_replace() وشەی "world" بە "Kurdistan" دەگۆڕێت.

Lineدێڕی 5

strstr() gives back the part of the string from the first @ to the end.

strstr() ئەو بەشەی دەگەڕێنێتەوە کە لە یەکەم @ـەوە دەست پێدەکات تا کۆتایی.

Lineدێڕی 6

max() gives back the largest of its .

max() گەورەترین ژمارە لەنێو دەگەڕێنێتەوە.

The rest of this lecture is about user-defined functions. You will meet many more built-in functions later, for example the string functions in the Strings lecture and count() in the Arrays lecture.

بەشەکانی تری ئەم وانەیە باسی دەکەن. دواتر زۆر تریش دەناسیت، بۆ نموونە لە وانەی تایبەتی خۆیاندا و count() لە وانەی داهاتوودا.

What is a function?کردار چییە؟

Three notes on built-in functionsسێ تێبینی دەربارەی کردارە ئامادەکان

Defining (creating) a functionناساندنی (دروستکردنی) کردارێک

To create a function you write a . It has four parts:

بۆ دروستکردنی ، دەنووسیت. چوار بەشی هەیە:

  1. the function;
  2. the name of the function (you choose it; see the naming rules);
  3. parentheses ( ) that hold the , separated by commas. They are optional: the parentheses may be empty, but they must be there;
  4. the body: the code that does the work, inside curly braces { }.
  1. function؛
  2. ناوی (خۆت هەڵیدەبژێریت؛ بڕوانە یاساکانی ناونان)؛
  3. کەوانە ( ) کە لەخۆ دەگرن و بە کۆما لێک جیا دەکرێنەوە. ئارەزوومەندانەن: لەوانەیە کەوانەکە بەتاڵ بێت، بەڵام دەبێت هەر هەبێت؛
  4. ناوەڕۆک: ئەو کۆدەی کارەکە دەکات، لەناو کەوانەی لوولی { }.
The shape of every function definitionشێوەی هەموو ناساندنەکانی کردارindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function functionName($parameter1, $parameter2) {
    // the body: code that runs each time the function is called
}
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

function keyword, the name, and the parameter list in parentheses.

function، ناوەکە و لیستی لەناو کەوانەدا.

Lineدێڕی 2-4

The body between { and }. Note: no after the closing }.

ناوەڕۆکەکە لە نێوان { و }دا. سەرنج: دوای }ی کۆتایی دانانرێت.

Quick checkپشکنینی خێراWhich keyword starts a function definition?کام دەست پێدەکات؟

Defining (creating) a functionناساندنی (دروستکردنی) کردارێک

The first example: greet()یەکەم نموونە: greet()

Here is the first example from the course lecture. It defines a function greet() and then calls it three times. Each call prints "Hello" once:

ئەمە یەکەم نموونەی وانەی کۆرسەکەیە. بە ناوی greet() دەناسێنێت و پاشان سێ جار بانگی دەکات. هەر بانگکردنێک یەک جار "Hello" چاپ دەکات:

One definition, three callsیەک ناساندن، سێ بانگکردنindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function greet() {
    print "Hello<br>";
}

greet();
greet();
greet();
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2-4

The definition. print works like echo here: it sends text to the page.

ناساندنەکە. print لێرەدا وەک echo کار دەکات: دەق دەنێرێت بۆ لاپەڕەکە.

Lineدێڕی 6-8

Three calls, so the body runs three times.

سێ بانگکردن، بۆیە ناوەڕۆکەکە سێ جار جێبەجێ دەکرێت.

The slide version ("Hello\n") as the browser shows itوەشانی سلایدەکە ("Hello\n") وەک تۆڕگەڕ پیشانی دەدات
Hello Hello Hello

PHP sent three lines, but the browser joins them into one line.

PHP سێ دێڕی ناردووە، بەڵام هەرسێکیان دەکات بە یەک دێڕ.

Defining (creating) a functionناساندنی (دروستکردنی) کردارێک

Two steps, always: create, then callهەمیشە دوو هەنگاو: دروستکردن، پاشان بانگکردن

Using a function always takes two steps. First you create (define) it. Then you call it by its name. Defining alone does nothing that you can see.

بەکارهێنانی هەمیشە دوو هەنگاوە. یەکەم، دروستی دەکەیت (دەیناسێنیت). دووەم، بە ناوەکەیەوە بانگی دەکەیت. تەنها ناساندن هیچ شتێکی بینراو ناکات.

Create, then callدروستکردن، پاشان بانگکردنindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
// Step 1: CREATE the function (nothing is printed yet)
function your_name()
{
    echo "Aram Karim";
}

// Step 2: CALL the function (now the code inside runs)
your_name();
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2-6

Creating. The function your_name() is defined. Nothing is printed yet.

دروستکردن. your_name() دەناسێنرێت. هێشتا هیچ چاپ ناکرێت.

Lineدێڕی 8-9

Calling. Now the code inside the braces runs and prints the name.

بانگکردن. ئێستا کۆدی ناو کەوانە لوولییەکان جێبەجێ دەکرێت و ناوەکە چاپ دەکات.

To prove it, here is a page that defines a function but never calls it. The receives an empty page:

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

Defined but never calledناسێنراوە بەڵام بانگ نەکراوەindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function myname($firstName)
{
    echo "my name is " . $firstName;
}
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2-5

A complete, correct definition of myname(). But no line calls myname(...), so echo never runs.

ناساندنێکی تەواو و دروستی myname(). بەڵام هیچ دێڕێک بانگی myname(...) ناکات، بۆیە echo هەرگیز جێبەجێ ناکرێت.

Naming rulesیاساکانی ناونان

A function name is an , so it follows rules much like variable names, but without a $ in front:

ناوی ، بۆیە یاساکانی وەک ناوی وایە، بەڵام بەبێ $ لە پێشیەوە:

  • Choose a name that says what the function does, for example calculateTotal(), showMenu() or addFive(). A verb at the start helps.
  • Start with a letter or an underscore _, never with a number: add2() is fine, 2add() is not.
  • After the first character, use only letters, digits and underscores. No spaces, no hyphens.
  • Do not use a reserved word such as echo, print, if, while, function or return.
  • Do not reuse the name of an existing function, including built-in ones such as strlen() (see Redeclaring).
  • ناوێک هەڵبژێرە کە بڵێت چی دەکات، بۆ نموونە calculateTotal() یان showMenu() یان addFive(). باشترە ناوەکە بە وشەیەک دەست پێبکات کە کارێک دەردەبڕێت، وەک calculate یان show.
  • بە پیتێک یان ژێرهێڵێک _ دەست پێبکە، هەرگیز بە ژمارە: add2() دروستە، 2add() نا.
  • دوای یەکەم پیت، تەنها پیت و ژمارە و ژێرهێڵ بەکاربهێنە. نە ، نە هێڵی کورت ⁦(-)⁩.
  • وشەی پارێزراو بەکار مەهێنە، وەک echo، print، if، while، function یان return.
  • ناوی کە پێشتر هەیە دووبارە بەکار مەهێنەوە، تەنانەت وەک strlen() (بڕوانە دووبارە ناساندنەوە).
Valid and invalid function names (each checked with PHP 8)ناوی دروست و نادروستی (هەریەکەیان بە ⁦PHP 8⁩ تاقیکراوەتەوە)
NameناوAllowed?ڕێگەپێدراوە؟Whyبۆچی
greet()Yesبەڵێletters onlyتەنها پیت
_helper()Yesبەڵێmay start with _دەتوانێت بە _ دەست پێبکات
add2()Yesبەڵێdigits are fine after the first letterژمارە دوای یەکەم پیت کێشە نییە
your_name()Yesبەڵێunderscores join wordsژێرهێڵ وشەکان پێکەوە دەبەستێت
2add()Noنەخێرstarts with a numberبە ژمارە دەست پێدەکات
my-name()Noنەخێر- is the minus - لێدەرکردنە
my name()Noنەخێرcontains a space تێدایە
print(), echo()Noنەخێرreserved words of PHPوشەی پارێزراوی PHPـن
strlen()Noنەخێرalready a built-in functionپێشتر

For names made of several words, two styles are common: camelCase (addFive, calculateTotal) and snake_case (your_name, calculate_total). The course material uses both. Pick one style for a project and keep to it.

بۆ ئەو ناوانەی لە چەند وشەیەک پێکهاتوون، دوو شێواز باون: camelCase (addFive، calculateTotal) و ⁦snake_case⁩ (your_name، calculate_total). کەرەستەکانی کۆرسەکە هەردووکیان بەکاردەهێنن. بۆ هەر پڕۆژەیەک یەک شێواز هەڵبژێرە و پابەندی بە.

Function names and variable names also live in separate worlds: a function x() and a variable $x can exist side by side without any clash (you will see this in the return example).

ناوی و ناوی هەروەها لە دوو جیهانی جیاوازدان: بە ناوی x() و بە ناوی $x دەتوانن پێکەوە هەبن بێ ئەوەی تێکبگیرێن (ئەمە لە نموونەی returnدا دەبینیت).

Naming rulesیاساکانی ناونان

Function names ignore upper and lower caseناوی کردارەکان هەستیار بە قەبارەی پیت نین

PHP does not care about capital letters in function names. If you define addFive(), you can call it as addfivE() or ADDFIVE(), and PHP runs the same function. In other words, function names are not .

PHP گوێ بە پیتی گەورە و بچووک نادات لە ناوی . ئەگەر addFive() بناسێنیت، دەتوانیت بە addfivE() یان ADDFIVE() بانگی بکەیت و PHP هەمان جێبەجێ دەکات. بە واتایەکی تر، ناوی نین.

Three spellings, one functionسێ شێوەی نووسین، یەک کردارindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function addFive($num) {
    return $num + 5;
}

echo addFive(5) . "<br>";   // the exact name
echo addfivE(5) . "<br>";   // different case: still works
echo ADDFIVE(5);            // all capitals: still works
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2-4

The definition uses the name addFive.

ناساندنەکە ناوی addFive بەکاردەهێنێت.

Lineدێڕی 6-8

All three calls reach the same function, so each prints 10.

هەر سێ بانگکردنەکە دەگەنە هەمان ، بۆیە هەریەکەیان ⁦10⁩ چاپ دەکات.

are different: their names are case-sensitive. $name and $Name are two separate variables:

جیاوازن: ناویان . $name و $Name دوو جیاوازن:

Variable names are case-sensitiveناوی گۆڕاوەکان هەستیار بە قەبارەی پیتنindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$name = "Shilan";
echo $name;   // Shilan
echo $Name;   // a different variable: it does not exist
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 4

$Name was never given a value, so PHP shows a and prints nothing for it.

هیچ نەدراوەتە $Name، بۆیە PHP پیشان دەدات و هیچ شتێک بۆ ئەو چاپ ناکات.

What PHP compares letter by letterPHP چی پیت بە پیت بەراورد دەکات
Name of…ناوی…Case-sensitive?؟Exampleنموونە
a Yesبەڵێ$name ≠ $Name
a NoنەخێرaddFive() = ADDFIVE()
a Noنەخێرecho = ECHO, function = FUNCTION
Quick checkپشکنینی خێراWhich sentence is true in PHP?کام ڕستە لە PHPـدا ڕاستە؟

Naming rulesیاساکانی ناونان

Lab fix 1: a function called Printچاککردنی یەکەمی تاقیگە: کردارێک بە ناوی Print

Example 1 of Lab 4 ("Function without argument") tries to define and call a function named Print. This is the code as it appears on the lab sheet, with the <?php tags added:

نموونەی یەکەمی تاقیگەی ٤ (" بەبێ ") هەوڵ دەدات بە ناوی Print بناسێنێت و بانگی بکات. ئەمە کۆدەکەیە وەک لە پەڕەی تاقیگەکەدا هاتووە، لەگەڵ زیادکردنی <?php:

Lab sheet, Example 1 (does not work)پەڕەی تاقیگە، نموونەی ١ (کار ناکات)index.phpTEXT
<?php
/* Defining a PHP Function */
     function Print() {
       echo “ you are running a function with out arguments";
     }
 /* Calling a PHP Function */
      print();
?>

PHP stops before running anything. There are three problems:

PHP پێش جێبەجێکردنی هیچ شتێک ڕادەوەستێت. سێ کێشە هەیە:

  1. The name Print is taken. print is a , a reserved word of PHP. Because function names ignore case, Print is print, so PHP cannot accept it as a new function name. The error message says it found the token print where it expected (.
  2. The call print(); would not call your function anyway. It is read as the print construct, which needs something to print.
  3. A curly quote. The string starts with “ (a curly quote from a word processor) and ends with ". PHP only understands straight quotes, so this alone would also be a .
  1. ناوی Print گیراوە. print ، واتە وشەیەکی پارێزراوی PHP. چونکە ناوی گوێ بە قەبارەی پیت نادەن، Print هەمان printە، بۆیە PHP وەک ناوی نوێ قبووڵی ناکات. پەیامی دەڵێت printی دۆزیوەتەوە لە شوێنێکدا کە چاوەڕێی ( بووە.
  2. بانگکردنی print(); بە هەر حاڵ تۆ بانگ ناکات. وەک پێکهاتەی print دەخوێنرێتەوە، کە پێویستی بە شتێکە بۆ چاپکردن.
  3. خوار. بە “ دەست پێدەکات ( خوار کە نووسین دروستی دەکات) و بە " کۆتایی دێت. PHP تەنها ڕاست دەناسێت، بۆیە ئەمە بە تەنهاش .

The fix: give the function a name that is not reserved and says what it does, call it with that same name, and use straight quotes. (We also correct "with out" to "without".)

چارەسەرەکە: ناوێک بدە بە کە پارێزراو نەبێت و بڵێت چی دەکات، بە هەمان ناو بانگی بکە و ڕاست بەکاربهێنە. (هەروەها "⁦with out⁩" دەکەینە "⁦without".⁩)

Example 1, fixedنموونەی ١، چاککراوindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
/* Defining a PHP function */
function printMessage() {
    echo "You are running a function without arguments.";
}

/* Calling the function */
printMessage();
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

A new, free name: printMessage.

ناوێکی نوێ و ئازاد: printMessage.

Lineدێڕی 8

The call uses exactly the same name.

بانگکردنەکە ڕێک هەمان ناو بەکاردەهێنێت.

Calling a functionبانگکردنی کردارێک

To call a function, write its name, then parentheses, then a semicolon: greet();. If the function has , put the inside the parentheses, separated by commas: add(20, 30);.

بۆ بانگکردنی ، ناوەکەی بنووسە، پاشان کەوانە، پاشان : greet();. ئەگەر هەبێت، لەناو کەوانەکەدا بنووسە و بە کۆما لێکیان جیا بکەرەوە: add(20, 30);.

A call can stand alone as a , as above. It can also be part of a bigger , for example echo add(20, 30); or $total = add(20, 30);. You will see why this is useful in Return values.

دەتوانێت بە تەنها بێت، وەک سەرەوە. هەروەها دەتوانێت بەشێک بێت لە گەورەتر، بۆ نموونە echo add(20, 30); یان $total = add(20, 30);. لە نرخە گەڕاوەکاندا دەبینیت ئەمە بۆچی بەسوودە.

A function runs only when it is called, and you may call it from anywhere in the page, as often as you like. Every call runs the whole body again, from its first line.

تەنها کاتێک جێبەجێ دەکرێت کە بانگ بکرێت و دەتوانیت لە هەر شوێنێکی لاپەڕەکەوە و هەر چەند جار بتەوێت بانگی بکەیت. هەر بانگکردنێک هەموو ناوەڕۆکەکە لە یەکەم دێڕییەوە دووبارە جێبەجێ دەکاتەوە.

Calling a functionبانگکردنی کردارێک

Calling before the definitionبانگکردن پێش ناساندن

Look at this example from the course lecture. The call myfunction(); on line 3 comes before the definition of myfunction() on line 5. It still works:

سەیری ئەم نموونەیەی وانەی کۆرسەکە بکە. بانگکردنی myfunction(); لە دێڕی ٣ پێش ناساندنی myfunction() لە دێڕی ٥ هاتووە. کەچی هەر کار دەکات:

The call comes first, the definition laterسەرەتا بانگکردن، پاشان ناساندنindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
echo "my name is ";
myfunction();

function myfunction()
{
    echo "Kak Rezan";
}
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

Prints the first part of the sentence.

بەشی یەکەمی ڕستەکە چاپ دەکات.

Lineدێڕی 3

Calls myfunction(), which is defined further down.

بانگی myfunction() دەکات کە لە خوارەوە ناسێنراوە.

Lineدێڕی 5-8

The definition. PHP already knew it before line 3 ran.

ناساندنەکە. PHP پێش جێبەجێکردنی دێڕی ٣ ئەمەی دەناسی.

Why does this work? Before PHP runs a file, it first reads the whole file. While reading, it notes every function that is defined at the top level of the file, that is, not inside an if block and not inside another function. So when line 3 runs, PHP already knows myfunction(). The lab program in the scope section also calls its two functions before it defines them.

بۆچی کار دەکات؟ پێش ئەوەی PHP فایلێک جێبەجێ بکات، سەرەتا هەموو فایلەکە دەخوێنێتەوە. لە کاتی خوێندنەوەدا، هەموو ئەو تۆمار دەکات کە لە ئاستی سەرەوەی فایلەکەدا ناسێنراون، واتە نە لەناو بلۆکێکی ifدا و نە لەناو تردا. بۆیە کاتێک دێڕی ٣ جێبەجێ دەکرێت، PHP پێشتر myfunction() دەناسێت. تاقیگەکەش لە بەشی globalدا هەردوو پێش ناساندنیان بانگ دەکات.

Quick checkپشکنینی خێراA file calls sayHi(); on line 2 and defines function sayHi() {...} at the top level on line 5. What happens?فایلێک لە دێڕی ٢دا sayHi(); بانگ دەکات و لە دێڕی ٥دا، لە ئاستی سەرەوەی فایلەکەدا، function sayHi() {...} دەناسێنێت. چی ڕوودەدات؟

Calling a functionبانگکردنی کردارێک

The exception: a function defined inside a blockئاوارتەکە: کردارێک کە لەناو بلۆکێکدا ناسێنرابێت

If a definition sits inside an if block (or inside another function), PHP creates the function only when that block actually runs. Calling it earlier is an error:

ئەگەر ناساندنەکە لەناو بلۆکێکی ifدا بێت (یان لەناو تردا)، PHP تەنها ئەو کاتە دروست دەکات کە ئەو بلۆکە بەڕاستی جێبەجێ دەکرێت. بانگکردنی پێش ئەو کاتە :

Too early: the if block has not run yetزووە: بلۆکی if هێشتا جێبەجێ نەکراوەindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$ready = true;

sayHi();   // Error: sayHi() does not exist yet

if ($ready) {
    function sayHi() {
        echo "Hi!";
    }
}
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 4

At this moment sayHi() does not exist yet.

لەم کاتەدا sayHi() هێشتا بوونی نییە.

Lineدێڕی 6-10

The function is created only when this block runs.

تەنها کاتێک دروست دەکرێت کە ئەم بلۆکە جێبەجێ بکرێت.

Call it after the blockدوای بلۆکەکە بانگی بکەindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$ready = true;

if ($ready) {
    function sayHi() {
        echo "Hi!";
    }
}

sayHi();   // Works: the if block has already run
?>

Calling a functionبانگکردنی کردارێک

Calling a function that does not existبانگکردنی کردارێک کە بوونی نییە

If you call a name that PHP does not know, for example because of a typing mistake, PHP stops the with a . Nothing after that line runs:

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

A typing mistake in the nameهەڵەی نووسین لە ناوەکەداindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function greet() {
    echo "Hi!";
}

gret();   // typing mistake: the function is called greet
?>

If you forget the parentheses, PHP does not see a call at all. It thinks greet is the name of a , and stops with a different message:

ئەگەر کەوانەکان لەبیر بکەیت، PHP هیچ بانگکردنێک نابینێت. وا دەزانێت greet ناوی و بە پەیامێکی تر ڕادەوەستێت:

Missing parenthesesکەوانەکان نینindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function greet() {
    echo "Hi!";
}

greet;   // the parentheses are missing
?>

Both messages tell you the line number. Read them carefully: the name in the message is exactly what PHP saw in your code. You will meet a real case of this error in Lab fix 2.

هەردوو پەیامەکە ژمارەی دێڕەکەت پێدەڵێن. بە وردی بیانخوێنەرەوە: ئەو ناوەی لە پەیامەکەدایە ڕێک ئەوەیە کە PHP لە کۆدەکەتدا بینیویەتی. نموونەیەکی ڕاستەقینەی ئەم لە چاککردنی دووەمی تاقیگەدا دەبینیت.

Parameters and arguments: sending information inپارامیتەرەکان و ئارگیومێنتەکان: ناردنی زانیاری بۆ ناوەوە

To make a function more useful, you can send information into it. You do this with . A parameter is just like a : it has a $ name, and inside the function you use it like any other variable. Parameters are written after the function name, inside the parentheses. You can have as many as you like; separate them with commas.

بۆ ئەوەی بەسوودتر بێت، دەتوانیت زانیاری بنێریتە ناوی. ئەمە بە دەکەیت. ڕێک وەک وایە: ناوێکی بە $ـەوە هەیە و لەناو وەک هەر تر بەکاری دەهێنیت. دوای ناوی و لەناو کەوانەکەدا دەنووسرێن. هەر چەندێکت بوێت دەتوانیت دایان بنێیت؛ بە کۆما لێکیان جیا بکەرەوە.

Three parameters in the parenthesesسێ پارامیتەر لەناو کەوانەکەداindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function myfunction($par1, $par2, $par3)
{
    echo "This function received: $par1, $par2 and $par3";
}

myfunction("tea", "bread", "cheese");
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

The parameter list: $par1, $par2, $par3.

لیستی : $par1، $par2، $par3.

Lineدێڕی 4

The body uses all three parameters.

ناوەڕۆکەکە هەر سێ بەکاردەهێنێت.

Lineدێڕی 7

Three arguments, one for each parameter.

سێ ، یەکێک بۆ هەر .

Parameters and arguments: sending information inپارامیتەرەکان و ئارگیومێنتەکان: ناردنی زانیاری بۆ ناوەوە

Parameter or argument?پارامیتەر یان ئارگیومێنت؟

The two words are easy to mix up. The course slides also mix them ("argument variables", "the function's argument may have a default value"). Here is the precise difference:

ئەم دوو وشەیە بە ئاسانی تێکەڵ دەبن. سلایدەکانی کۆرسەکەش تێکەڵیان دەکەن (⁦"argument variables⁩"، "⁦the function's argument may have a default value"⁩). ئەمە جیاوازییە وردەکەیە:

Parameter vs argument بەرامبەر
Where it appearsلە کوێ دەردەکەوێتin the definitionلە ناساندنەکەداin the callلە بانگکردنەکەدا
What it isچییەa variable that waits for a value کە چاوەڕێی دەکاتthe actual value that is sentئەو بەڕاستی دەنێردرێت
Exampleنموونە$firstName in function myname($firstName)$firstName لە function myname($firstName)"Aram" in myname("Aram")"Aram" لە myname("Aram")
Juice machineئامێری ئاوی میوەthe opening for fruitکونی میوەکەthe fruit you put inئەو میوەیەی دەیخەیتە ناوی
Definitionپێناسە

A variable named in a function definition that receives a value when the function is called: in def greet(name): (Python) or function greet($name) (PHP), name is a parameter, and the value passed in is the . In a database query, a parameter is a value asked for when the query runs, such as the city in an Access parameter query.

گۆڕاوێکە کە لە ناساندنی ناو دەبرێت و لە کاتی بانگکردندا نرخێک وەردەگرێت: لە def greet(name): ⁦(Python)⁩ یان function greet($name) ⁦(PHP)⁩ـدا name پارامیتەرە و ئەو نرخەی دەینێریت . لە ، پارامیتەر نرخێکە کە لە کاتی کارپێکردنی پرسەکەدا داوا دەکرێت، وەک شارەکە لە پرسی پارامیتەری Accessـدا.

Definitionپێناسە

The actual value you pass to a function when you call it. In greet("Aram"), "Aram" is the argument that fills the $name.

ئەو نرخە ڕاستەقینەیەیە کە لە کاتی بانگکردنی دەینێریت بۆی. لە greet("Aram")، "Aram" ئەو ئارگیومێنتەیە کە $name پڕ دەکاتەوە.

Quick checkپشکنینی خێراIn function greet($name) { ... } and the call greet("Aram"), what is "Aram"?لە function greet($name) { ... } و بانگکردنی greet("Aram")دا، "Aram" چییە؟

Parameters and arguments: sending information inپارامیتەرەکان و ئارگیومێنتەکان: ناردنی زانیاری بۆ ناوەوە

A function with one parameterکردارێک بە یەک پارامیتەر

The lecture's next example has one parameter, $firstName. Each call sends a different name, so the same function prints a different line each time:

نموونەی دواتری وانەکە یەک هەیە، $firstName. هەر بانگکردنێک ناوێکی جیاواز دەنێرێت، بۆیە هەمان هەر جارە دێڕێکی جیاواز چاپ دەکات:

One parameter, four callsیەک پارامیتەر، چوار بانگکردنindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function myname($firstName)
{
    echo "my name is " . $firstName . "!<br />";
}

myname("Aram");
myname("Shilan");
myname("Karwan");
myname("Dilan");
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

$firstName is the parameter: an empty box waiting for a value.

$firstName : سندوقێکی بەتاڵ کە چاوەڕێی دەکات.

Lineدێڕی 4

Inside the body, $firstName is used like any variable. The . joins the strings ().

لەناو ناوەڕۆکەکەدا، $firstName وەک هەر بەکاردێت. . پێکەوە دەلکێنێت ().

Lineدێڕی 7-10

Each call puts a different argument into $firstName.

هەر بانگکردنێک جیاواز دەخاتە ناو $firstNameـەوە.

Parameters and arguments: sending information inپارامیتەرەکان و ئارگیومێنتەکان: ناردنی زانیاری بۆ ناوەوە

Several parameters: the order mattersچەند پارامیتەر: ڕیزبەندی گرنگە

With two parameters, the function receives two values. PHP matches them by position: the first argument goes into the first parameter, the second argument into the second parameter.

بە دوو ، دوو وەردەگرێت. PHP بەپێی شوێن پێکیان دەگەیەنێت: یەکەم دەچێتە ناو یەکەم و دووەم دەچێتە ناو دووەم.

Two parametersدوو پارامیتەرindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function myname($firstName, $lastName)
{
    echo "Hello there " . $firstName . " " . $lastName . "!<br />";
}

myname("Aram", "Ali");
myname("Shilan", "Omar");
myname("Rezan", "Mahmood");
myname("Hana", " Bakr");
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

Two parameters, separated by a comma.

دوو ، کە بە کۆمایەک لێک جیا کراونەتەوە.

Lineدێڕی 4

" " puts a space between the first and last name.

" " دەخاتە نێوان ناو و پاشناوەوە.

Lineدێڕی 10

" Bakr" starts with a space, so the HTML gets two spaces. The browser shows only one.

" Bakr" بە دەست پێدەکات، بۆیە HTMLـەکە دوو وەردەگرێت. تەنها یەکێکیان پیشان دەدات.

Parameters and arguments: sending information inپارامیتەرەکان و ئارگیومێنتەکان: ناردنی زانیاری بۆ ناوەوە

The slides' add(): order mattersadd()ـی سلایدەکان: ڕیزبەندی گرنگە

The course slides show the same idea with a function add(). It receives two numbers, prints them, adds them, and prints the result:

سلایدەکانی کۆرسەکە هەمان بیرۆکە بە بە ناوی add() پیشان دەدەن. دوو ژمارە وەردەگرێت، چاپیان دەکات، کۆیان دەکاتەوە و ئەنجامەکە چاپ دەکات:

add() with two parametersadd() بە دوو پارامیتەرindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function add($num1, $num2)
{
    echo "first data is $num1 <br>";
    echo "second data is $num2 <br>";
    $sum = $num1 + $num2;
    echo "your data is $sum";
}

add(20, 30);
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

The parameters work like variables inside the function.

لەناو وەک کار دەکەن.

Lineدێڕی 6

A new variable $sum, made inside the function.

نوێ بە ناوی $sum، کە لەناو دروست کراوە.

Lineدێڕی 10

20 goes into $num1, 30 into $num2.

⁦20⁩ دەچێتە ناو $num1 و ⁦30⁩ دەچێتە ناو $num2.

More about this in Variables inside functions. Notice also that this add() prints its result instead of giving it back; you will see why returning is often better in Echo or return?.

زیاتر لەم بارەیەوە لە گۆڕاوەکان لەناو کردارەکاندادا بخوێنەرەوە. هەروەها سەرنج بدە کە ئەم add()ـە ئەنجامەکەی چاپ دەکات لە جیاتی ئەوەی بیگەڕێنێتەوە؛ لە echo یان return؟دا دەبینیت بۆچی گەڕاندنەوە زۆرجار باشترە.

Parameters and arguments: sending information inپارامیتەرەکان و ئارگیومێنتەکان: ناردنی زانیاری بۆ ناوەوە

Lab fix 2: calling the wrong nameچاککردنی دووەمی تاقیگە: بانگکردنی ناوی هەڵە

Lab 4, Example 3 ("PHP functions with multiple arguments") defines a function with two parameters. It prints the sum, the remainder (, %) and the quotient (/) of two numbers. On the lab sheet it looks like this:

نموونەی سێیەمی تاقیگەی ٤ (" لە PHPـدا بە چەند ") بە دوو دەناسێنێت. کۆ، (%) و ئەنجامی دابەشکردنی (/) دوو ژمارە چاپ دەکات. لە پەڕەی تاقیگەکەدا بەم شێوەیەیە:

Lab sheet, Example 3 (does not work)پەڕەی تاقیگە، نموونەی ٣ (کار ناکات)index.phpTEXT
<?php
function calculate_Function ($num1, $num2) {
        $sum = $num1 + $num2;
       $mod = $num1 % $num2;
        $div = $num1 / $num2;
         echo "Sum of the two numbers is : $sum";
           echo “mod of the two numbers is : $mod";
           echo “div of the two numbers is : $div";
     }
       addFunction(20, 10);
?>

There are three problems, and PHP shows them one at a time:

سێ کێشە هەیە و PHP یەک لە دوای یەک پیشانیان دەدات:

  1. Curly quotes (lines 7 and 8). Two echo lines open their strings with “. PHP does not see a string at all: it reads “mod as some unknown name and then stops at the word of. Fix: straight quotes ".
  2. The wrong function is called (line 10). The function is defined as calculate_Function, but the code calls addFunction(20, 10). Once the quotes are fixed, PHP stops with this :
  3. No separators. The three echo lines print no space and no <br>, so the three results run together on one line.
  1. خوار (دێڕی ٧ و ٨). دوو دێڕی echo بە “ دەکەنەوە. PHP هیچ نابینێت: “mod وەک ناوێکی نەناسراو دەخوێنێتەوە و پاشان لە وشەی ofدا ڕادەوەستێت. چارەسەر: ڕاست ".
  2. هەڵە بانگ کراوە (دێڕی ١٠). بە ناوی calculate_Function ناسێنراوە، بەڵام کۆدەکە addFunction(20, 10) بانگ دەکات. کاتێک چاک دەکرێنەوە، PHP بە ڕادەوەستێت:
  3. هیچ جیاکەرەوەیەک نییە. سێ دێڕەکەی echo نە چاپ دەکەن نە <br>، بۆیە سێ ئەنجامەکە بە یەکەوە لە یەک دێڕدا دەردەکەون.
After fixing only the quotesدوای چاککردنی تەنها نیشانەکانی گوتە
Fatal error: Uncaught Error: Call to undefined function addFunction() in C:\xampp\htdocs\function_array\index.php:10
Stack trace:
#0 {main}
  thrown in C:\xampp\htdocs\function_array\index.php on line 10

Parameters and arguments: sending information inپارامیتەرەکان و ئارگیومێنتەکان: ناردنی زانیاری بۆ ناوەوە

Lab fix 2: the corrected codeچاککردنی دووەمی تاقیگە: کۆدە چاککراوەکە

Here is the fixed version. It calls the function by its real name, uses straight quotes, ends each line with <br>, and calls the function twice to show a remainder and a decimal result:

ئەمە وەشانی چاککراوەکەیە. بە ناوی ڕاستەقینەی خۆی بانگ دەکات، ڕاست بەکاردەهێنێت، هەر دێڕێک بە <br> کۆتایی پێدەهێنێت و دوو جار بانگ دەکات بۆ ئەوەی و ئەنجامێکی کەرتی پیشان بدات:

Example 3, fixedنموونەی ٣، چاککراوindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function calculate_Function($num1, $num2) {
    $sum = $num1 + $num2;
    $mod = $num1 % $num2;
    $div = $num1 / $num2;
    echo "Sum of the two numbers is : $sum<br>";
    echo "mod of the two numbers is : $mod<br>";
    echo "div of the two numbers is : $div<br>";
}

calculate_Function(20, 10);
calculate_Function(7, 2);
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3-5

+ adds, % gives the remainder, / divides.

+ کۆ دەکاتەوە، % دەدات، / دابەش دەکات.

Lineدێڕی 6-8

Each line now ends with <br>.

ئێستا هەر دێڕێک بە <br> کۆتایی دێت.

Lineدێڕی 11-12

The call uses the real name calculate_Function.

بانگکردنەکە ناوی ڕاستەقینە calculate_Function بەکاردەهێنێت.

For comparison, this is what the lab code prints with the right name but without the <br> tags:

بۆ بەراوردکردن، ئەمە ئەوەیە کە کۆدی تاقیگەکە بە ناوی ڕاست بەڵام بەبێ <br> چاپی دەکات:

Outputدەرئەنجام
Sum of the two numbers is : 30mod of the two numbers is : 0div of the two numbers is : 2

Parameters and arguments: sending information inپارامیتەرەکان و ئارگیومێنتەکان: ناردنی زانیاری بۆ ناوەوە

Too many or too few argumentsئارگیومێنتی زۆر یان کەم

What if the number of arguments does not match the number of parameters? PHP treats the two cases differently:

ئەگەر ژمارەی لەگەڵ ژمارەی یەک نەگرێتەوە چی دەبێت؟ PHP بە دوو شێوەی جیاواز مامەڵە لەگەڵ ئەم دوو حاڵەتە دەکات:

  • Extra arguments are simply ignored.
  • Missing arguments (for parameters without a default value) stop the script with an ArgumentCountError.
  • زیادە بە سادەیی پشتگوێ دەخرێن.
  • کەم (بۆ ئەو نییە) بە ArgumentCountError ڕادەگرن.
One argument too many, then one too fewیەک ئارگیومێنت زیاتر، پاشان یەک کەمترindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function myname($firstName, $lastName)
{
    echo "Hello there " . $firstName . " " . $lastName . "!<br />";
}

myname("Aram", "Ali", "extra");   // extra argument: ignored
myname("Aram");                   // missing argument: error
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 7

Three arguments for two parameters: "extra" is ignored.

سێ بۆ دوو : "extra" پشتگوێ دەخرێت.

Lineدێڕی 8

One argument for two parameters: fatal error.

یەک بۆ دوو : .

The message tells you everything: which function, how many arguments were passed (1), on which line, and how many were expected (exactly 2). If a parameter may sometimes be left out, give it a default value, as the next section shows.

پەیامەکە هەموو شتێکت پێدەڵێت: کام ، چەند نێردراوە ⁦(1)⁩، لە کام دێڕدا و چەند چاوەڕوان دەکرا (ڕێک ⁦2⁩). ئەگەر هەندێک جار نەنێردرێت، بدەرێ، وەک لە بەشی داهاتوودا دەبینیت.

Quick checkپشکنینی خێراfunction add($a, $b) { return $a + $b; } What happens with add(1, 2, 3) and with add(1)?function add($a, $b) { return $a + $b; } بە add(1, 2, 3) و بە add(1) چی ڕوودەدات؟

Default parameter valuesنرخە بنەڕەتییەکان

A parameter can have a . You write it with = in the definition. If the caller does not send an argument for that parameter, the function uses the default value instead.

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

setHeight() with a default of 50setHeight() بە نرخێکی بنەڕەتیی ⁦50⁩index.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function setHeight($minheight = 50) {
    echo "The height is : $minheight <br>";
}

setHeight(350);
setHeight();
setHeight(135);
setHeight(80);
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

$minheight = 50: the default value.

$minheight = 50: .

Lineدێڕی 3

$minheight inside double quotes is replaced by its value ().

$minheight لەناو جووتەگوتەدا بە دەگۆڕدرێت ().

Lineدێڕی 7

No argument, so $minheight is 50.

هیچ نییە، بۆیە $minheight دەبێتە ⁦50⁩.

Only the second call, setHeight(), sends nothing, so only that call uses the default 50. The other three calls send their own value, which replaces the default.

تەنها بانگکردنی دووەم، setHeight()، هیچ نانێرێت، بۆیە تەنها ئەو بانگکردنە ⁦50⁩ بەکاردەهێنێت. سێ بانگکردنەکەی تر خۆیان دەنێرن، کە جێگەی دەگرێتەوە.

Default parameter valuesنرخە بنەڕەتییەکان

Optional parameters go lastپارامیتەرە ئارەزوومەندانەکان لە کۆتاییدا دێن

A function can mix required parameters (no default) and optional ones (with a default). Put the optional ones after the required ones. If an optional parameter comes first, its default can never be used, because the caller must still fill it to reach the required one after it. PHP 8 warns you about this:

دەتوانێت پێویست (بێ ) و ئارەزوومەندانە (بە ) تێکەڵ بکات. ئارەزوومەندانەکان دوای پێویستەکان دابنێ. ئەگەر ئارەزوومەندانە یەکەم بێت، هەرگیز بەکارنایەت، چونکە بانگکەر دەبێت هەر پڕی بکاتەوە بۆ ئەوەی بگاتە پێویستەکەی دوایەوە. ⁦PHP 8⁩ لەمە ئاگادارت دەکاتەوە:

Optional before required: a warningئارەزوومەندانە پێش پێویست: هۆشداریindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function greetUser($greeting = "Hello", $name) {
    echo "$greeting, $name!";
}

greetUser("Hi", "Dilan");
?>
Required first, optional lastسەرەتا پێویست، لە کۆتاییدا ئارەزوومەندانەindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function greetUser($name, $greeting = "Hello") {
    echo "$greeting, $name!<br>";
}

greetUser("Dilan");          // uses the default greeting
greetUser("Soran", "Hi");    // replaces the default
?>
Quick checkپشکنینی خێراWhere should a parameter with a default value go? کە هەیە دەبێت لە کوێ بێت؟

Default parameter valuesنرخە بنەڕەتییەکان

Lab fix 3: a stray braceچاککردنی سێیەمی تاقیگە: کەوانەیەکی زیادە

Lab 4, Example 5 ("Setting Default Values for Function Parameters") gives $param the default value NULL. On the lab sheet, a lonely { follows the two calls:

نموونەی پێنجەمی تاقیگەی ٤ ("دانانی بۆ ") NULL دەدات بە $param. لە پەڕەی تاقیگەکەدا، {ێکی تەنها دوای هەردوو بانگکردنەکە هاتووە:

Lab sheet, Example 5 (does not work)پەڕەی تاقیگە، نموونەی ٥ (کار ناکات)index.phpTEXT
<?php
 function printMe($param = NULL) {
        print $param;     }
              printMe("This is test");
              printMe();
       {
?>

Every { must have a matching }. PHP reached the end of the file (line 7) while the { from line 6 was still open, so it rejected the whole file: even the correct lines above it never run. Fix: delete the stray {. While we are here, we also move the function's closing } onto its own line. On the lab sheet it hides at the end of the print line, which is legal but easy to miss.

هەر {ێک دەبێت }ێکی هاوتای هەبێت. PHP گەیشتە کۆتایی فایلەکە (دێڕی ٧) لە کاتێکدا {ی دێڕی ٦ هێشتا کراوە بوو، بۆیە هەموو فایلەکەی ڕەت کردەوە: تەنانەت دێڕە دروستەکانی سەرەوەش هەرگیز جێبەجێ ناکرێن. چارەسەر: {ە زیادەکە بسڕەوە. لە هەمان کاتدا }ی کۆتایی دەبەینە سەر دێڕێکی جیا. لە پەڕەی تاقیگەکەدا لە کۆتایی دێڕی printدا شاراوەتەوە، کە دروستە بەڵام بە ئاسانی نابینرێت.

Example 5, fixedنموونەی ٥، چاککراوindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function printMe($param = NULL) {
    print $param;
}

printMe("This is test");
printMe();   // uses the default NULL, so nothing is printed
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

$param = NULL: if no argument comes, $param is NULL.

$param = NULL: ئەگەر هیچ نەیەت، $param دەبێتە NULL.

Lineدێڕی 3

print works like echo here.

print لێرەدا وەک echo کار دەکات.

Lineدێڕی 7

No argument: NULL is printed as an empty string, so nothing appears.

هیچ نییە: NULL وەک بەتاڵ چاپ دەکرێت، بۆیە هیچ دەرناکەوێت.

About print: it outputs one string, just like echo, and it always returns the number 1. The differences between the two are covered in Output with echo and print.

دەربارەی print: یەک دەردەخات، ڕێک وەک echo و هەمیشە ژمارە 1 دەگەڕێنێتەوە. جیاوازییەکانی نێوانیان لە وانەی echo و printدا باس کراون.

Returning a valueگەڕاندنەوەی نرخێک

Often a function takes its arguments, does some computation, and returns a value. The keyword return does this. The returned value becomes the value of the call itself, right where the call stands in your code.

زۆرجار وەردەگرێت، هەژمارێک دەکات و دەگەڕێنێتەوە. return ئەم کارە دەکات. دەبێتە خودی بانگکردنەکە، ڕێک لەو شوێنەی بانگکردنەکەی تێدایە لە کۆدەکەتدا.

Definitionپێناسە

The value a function sends back to the code that called it, using the return keyword. return also ends the function at once, so code after it never runs.

ئەو نرخەیە کە بە وشەی return دەینێرێتەوە بۆ ئەو کۆدەی بانگی کردووە. return هەروەها یەکسەر کۆتایی پێ دەهێنێت، بۆیە کۆدی دوای ئەو هەرگیز جێبەجێ ناکرێت.

A function that returns a stringکردارێک کە زنجیرەنووسەیەک دەگەڕێنێتەوەindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function greeting() {
    return "Hello";
}

print greeting() . " Glenn<br>";
print greeting() . " Sally<br>";
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

greeting() gives back the string "Hello".

greeting() "Hello" دەگەڕێنێتەوە.

Lineدێڕی 6-7

Each call is replaced by "Hello", then joined with a name.

هەر بانگکردنێک جێگەی خۆی دەدات بە "Hello"، پاشان بە ناوێکەوە دەلکێنرێت.

Lab 4, Example 4 ("PHP Functions returning value") stores the returned value in a variable first, then prints it:

نموونەی چوارەمی تاقیگەی ٤ (" لە PHPـدا کە دەگەڕێننەوە") سەرەتا لە هەڵدەگرێت، پاشان چاپی دەکات:

Lab 4, Example 4تاقیگەی ٤، نموونەی ٤index.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function addFunction($num1, $num2) {
    $sum = $num1 + $num2;
    return $sum;
}

$return_value = addFunction(10, 20);
echo "Returned value from the function : $return_value";
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 4

Hand the sum back to the caller.

کۆکە بدەرەوە بە بانگکەر.

Lineدێڕی 7

The call is replaced by 30, which goes into $return_value.

بانگکردنەکە دەبێتە ⁦30⁩، کە دەچێتە ناو $return_value.

Returning a valueگەڕاندنەوەی نرخێک

Using the returned valueبەکارهێنانی نرخە گەڕاوەکە

Because a call with return simply is a value, you can use it anywhere a value is allowed: store it, print it, calculate with it, or even pass it to another call:

چونکە بانگکردنێک کە returnی تێدایە بە سادەیی ، دەتوانیت لە هەر شوێنێکدا بەکاری بهێنیت کە ڕێگەپێدراوە: هەڵیبگرە، چاپی بکە، هەژماری پێ بکە، یان تەنانەت بینێرە بۆ بانگکردنێکی تر:

add() that returns its resultadd() کە ئەنجامەکەی دەگەڕێنێتەوەindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function add($num1, $num2)
{
    return $num1 + $num2;
}

$total = add(20, 30);
echo "The sum is $total<br>";
echo "Double the sum is " . add(20, 30) * 2 . "<br>";
echo "Sum of three numbers: " . add(add(1, 2), 3);
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 4

The sum is returned, not printed.

کۆکە دەگەڕێنرێتەوە، چاپ ناکرێت.

Lineدێڕی 7

Store the result.

ئەنجامەکە هەڵبگرە.

Lineدێڕی 9

Calculate with it: add(20, 30) * 2 is 100.

هەژماری پێ بکە: add(20, 30) * 2 یەکسانە بە ⁦100⁩.

Lineدێڕی 10

Nest calls: the inner add(1, 2) gives 3, then add(3, 3) gives 6.

بانگکردن لەناو بانگکردندا: add(1, 2)ی ناوەوە 3 دەدات، پاشان add(3, 3) 6 دەدات.

A function with no return statement still gives something back: the special value NULL (), which means no value:

کە هیچ returnێکی تێدا نییە هەر شتێک دەگەڕێنێتەوە: تایبەتی NULL ()، کە واتای هیچ نییە دەگەیەنێت:

No return means NULLبێ return واتە NULLindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function sayHello() {
    echo "Hello! ";
}

$result = sayHello();
var_dump($result);
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 6

sayHello() prints "Hello! " but returns nothing, so $result is NULL.

sayHello() "⁦Hello!⁩ " چاپ دەکات بەڵام هیچ ناگەڕێنێتەوە، بۆیە $result دەبێتە NULL.

Lineدێڕی 7

var_dump() shows the type and value of a variable.

var_dump() جۆر و پیشان دەدات.

Quick checkپشکنینی خێراA function has no return statement. What does a call to it give back? هیچ returnێکی تێدا نییە. بانگکردنی چی دەگەڕێنێتەوە؟

Returning a valueگەڕاندنەوەی نرخێک

return ends the function at oncereturn یەکسەر کۆتایی بە کردارەکە دەهێنێت

return does two things at the same moment: it sends a value back, and it ends the function immediately. Any code after return in the same function never runs:

return لە یەک کاتدا دوو کار دەکات: دەنێرێتەوە و یەکسەر کۆتایی بە دەهێنێت. هەر کۆدێک دوای return لە هەمان هەرگیز جێبەجێ ناکرێت:

Code after return never runsکۆدی دوای return هەرگیز جێبەجێ ناکرێتindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function x() {
    return "Hey";
    echo "How";   // never runs: return has already ended the function
}

$x = "a variable";

echo x() . "<br>";
echo $x;
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

The function ends here and gives back "Hey".

لێرەدا کۆتایی دێت و "Hey" دەگەڕێنێتەوە.

Lineدێڕی 4

Unreachable: "How" is never printed.

ناگاتێ: "How" هەرگیز چاپ ناکرێت.

Lineدێڕی 7

A variable $x and a function x() can live side by side: their names never clash.

بە ناوی $x و بە ناوی x() دەتوانن پێکەوە هەبن: ناوەکانیان هەرگیز تێکناگیرێن.

Lineدێڕی 9

echo x() prints the returned value.

echo x() چاپ دەکات.

This is useful. A function may contain several return statements, and the first one that runs wins. The lecture's howdy() example uses this to greet people in different languages:

ئەمە بەسوودە. دەتوانێت چەند returnی تێدا بێت و یەکەمیان کە جێبەجێ بکرێت براوەیە. نموونەی howdy()ی وانەکە ئەمە بەکاردەهێنێت بۆ سڵاوکردن بە زمانی جیاواز:

Several return statementsچەند ڕستەیەکی returnindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function howdy($lang) {
    if ($lang == 'es') return "Hola";
    if ($lang == 'fr') return "Bonjour";
    return "Hello";
}

print howdy('es') . " Glenn<br>";
print howdy('fr') . " Sally<br>";
print howdy('de') . " Aram<br>";
print howdy('ES') . " Hana";
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

If $lang is 'es', return "Hola" and stop.

ئەگەر $lang یەکسان بێت بە 'es'، "Hola" بگەڕێنەوە و بوەستە.

Lineدێڕی 4

Only reached when the first test failed.

تەنها کاتێک دەگاتە ئێرە کە تاقیکردنەوەی یەکەم سەرنەکەوتبێت.

Lineدێڕی 5

The fallback: reached only when no if matched.

بژاردەی کۆتایی: تەنها کاتێک دەگاتە ئێرە کە هیچ ifێک ڕاست نەبووبێت.

Lineدێڕی 10

'de' matches nothing, so the answer is "Hello".

'de' لەگەڵ هیچیان ناگونجێت، بۆیە ئەنجامەکە "Hello"یە.

Lineدێڕی 11

'ES' is not 'es': string comparison with == is case-sensitive.

'ES' یەکسان نییە بە 'es': بەراوردکردنی بە == .

Quick checkپشکنینی خێراWhat does this print? function f() { return 1; echo 2; } echo f();ئەمە چی چاپ دەکات؟ function f() { return 1; echo 2; } echo f();

Returning a valueگەڕاندنەوەی نرخێک

Echo or return?echo یان return؟

A function can deliver its result in two ways: print it with echo inside the function, or return it and let the caller decide what to do with it. In the lab session, students wrote the same function both ways. It adds two numbers, but first uses isset() to check that neither number is NULL (see Conditionals for isset() and !).

دەتوانێت بە دوو شێوە ئەنجامەکەی بدات: لەناو بە echo چاپی بکات، یان بیگەڕێنێتەوە و بانگکەر بڕیار بدات چی لێ بکات. لە کاتی تاقیگەدا، خوێندکاران هەمان بە هەردوو شێوە نووسی. دوو ژمارە کۆ دەکاتەوە، بەڵام سەرەتا بە isset() دەپشکنێت کە هیچ کام لە ژمارەکان NULL نەبێت (بۆ isset() و ! بڕوانە وانەی مەرجەکان).

Version 1: the function printsوەشانی ١: کردارەکە چاپ دەکاتindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function sum($num1, $num2) {
    if (!isset($num1) || !isset($num2)) {
        echo "One of the two numbers is not set yet.";
    } else {
        echo "Summation of $num1 and $num2 is " . ($num1 + $num2);
    }
}

sum(1, null);
echo "<br>";
sum(1, 1);
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

!isset($num1) is true when $num1 is NULL. || means or.

!isset($num1) ڕاستە کاتێک $num1 یەکسان بێت بە NULL. || واتە یان.

Lineدێڕی 6

The sum is printed right here, inside the function.

کۆکە هەر لێرە، لەناو چاپ دەکرێت.

Lineدێڕی 10

null is passed on purpose, so the message is printed.

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

Version 2: the function returns, the caller printsوەشانی ٢: کردارەکە دەگەڕێنێتەوە، بانگکەر چاپ دەکاتindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function sum($num1, $num2) {
    if (!isset($num1) || !isset($num2)) {
        return "One of the two numbers is not set yet.";
    } else {
        return "$num1 + $num2 = " . ($num1 + $num2);
    }
}

echo "Summation 1: " . sum(1, 1) . "<br />";
echo "Summation 2: " . sum(1, null);
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 4, 6

return instead of echo: the text is handed back, not printed.

return لە جیاتی echo: دەقەکە دەدرێتەوە، چاپ ناکرێت.

Lineدێڕی 10-11

The caller decides: it adds a label and <br /> around each result.

بانگکەر بڕیار دەدات: ناونیشانێک و <br /> بە دەوری هەر ئەنجامێکدا زیاد دەکات.

Printing versus returningچاپکردن بەرامبەر گەڕاندنەوە
echo inside the functionecho لەناو return a valueگەڕاندنەوەی بە return
Where does the result go?ئەنجامەکە بۆ کوێ دەچێت؟straight onto the pageڕاستەوخۆ بۆ سەر لاپەڕەکەback to the code that called the functionدەگەڕێتەوە بۆ ئەو کۆدەی بانگی کردووە
Can you use the result again?دەتوانیت ئەنجامەکە دووبارە بەکاربهێنیتەوە؟No, it is already printedنەخێر، پێشتر چاپ کراوەYes: store it, calculate with it, print itبەڵێ: هەڵیبگرە، هەژماری پێ بکە، چاپی بکە
Who decides the format?کێ شێوەی پیشاندان دیاری دەکات؟the functionthe callerبانگکەر

Rule of thumb: let functions return their results, and do the printing outside. Then the same function can be used to print, to calculate further, or to save to a database. The slides' add() from Several parameters prints its sum; the add() in Returning a value returns it, so it can be used inside other expressions.

یاسایەکی گشتی: با ئەنجامەکانیان بگەڕێننەوە و چاپکردن لە دەرەوە بکە. ئەو کاتە هەمان دەتوانرێت بۆ چاپکردن، بۆ هەژمارکردنی زیاتر، یان بۆ پاشەکەوتکردن لە بەکاربهێنرێت. add()ی سلایدەکان لە بەشی چەند پارامیتەر کۆکەی چاپ دەکات؛ add()ی بەشی return دەیگەڕێنێتەوە، بۆیە دەتوانرێت لەناو تردا بەکاربهێنرێت.

What happens during a callلە کاتی بانگکردندا چی ڕوودەدات؟

You now know every part of a function. This picture puts them together for one call, $total = add(20, 30);:

ئێستا هەموو بەشەکانی دەناسیت. ئەم وێنەیە هەموویان بۆ یەک بانگکردن پێکەوە کۆ دەکاتەوە، $total = add(20, 30);:

Diagram of a function call. On the left, the caller's code $total = add(20, 30);. Arrow 1 carries the arguments 20 and 30 into the function add on the right, where they are copied into the parameters $num1 and $num2. Step 2: the body computes $sum = 50 and runs return $sum;. Arrow 3 carries the return value 50 back to the call, and step 4 shows that $total is now 50, so echo $total; prints 50.وێنەی بانگکردنێکی کردار. لە لای چەپ کۆدی بانگکەر هەیە: $total = add(20, 30);. تیری ١ ئارگیومێنتەکان 20 و 30 دەبات بۆ ناو کرداری add لە لای ڕاست، کە لەوێ کۆپی دەکرێن بۆ ناو پارامیتەرەکان $num1 و $num2. هەنگاوی ٢: ناوەڕۆکەکە $sum = 50 هەژمار دەکات و return $sum; جێبەجێ دەکات. تیری ٣ نرخە گەڕاوەکە 50 دەگەڕێنێتەوە بۆ بانگکردنەکە و هەنگاوی ٤ پیشان دەدات کە ئێستا $total یەکسانە بە 50، بۆیە echo $total; 50 چاپ دەکات.
One call, step by step: arguments go in, the body runs, the return value comes back.یەک بانگکردن، هەنگاو بە هەنگاو: دەچنە ژوورەوە، ناوەڕۆکەکە جێبەجێ دەکرێت، دەگەڕێتەوە.CêIT
The six steps of a callشەش هەنگاوی بانگکردنێک
  1. Find the functionدۆزینەوەی کردارەکە

    PHP looks up the name add (upper or lower case does not matter). If no such function exists, PHP stops with Call to undefined function.

    PHP بەدوای ناوی addدا دەگەڕێت (پیتی گەورە یان بچووک گرنگ نییە). ئەگەر وا نەبێت، PHP بە ⁦Call to undefined function⁩ ڕادەوەستێت.

  2. Copy the arguments into the parametersکۆپیکردنی ئارگیومێنتەکان بۆ ناو پارامیتەرەکان

    20 goes into $num1 and 30 into $num2, by position. A missing argument takes its default value; if there is none, PHP stops with an ArgumentCountError.

    ⁦20⁩ دەچێتە ناو $num1 و ⁦30⁩ دەچێتە ناو $num2، بەپێی شوێن. کە نەنێردرابێت خۆی وەردەگرێت؛ ئەگەر نەیبێت، PHP بە ArgumentCountError ڕادەوەستێت.

  3. Run the bodyجێبەجێکردنی ناوەڕۆکەکە

    The statements inside { } run from the top. The calling code waits. Variables made here, like $sum, are local.

    ناو { } یەک لە دوای یەک لە سەرەوە جێبەجێ دەکرێن. کۆدی بانگکەر چاوەڕێ دەکات. ئەو لێرە دروست دەکرێن، وەک $sum، ناوخۆیین.

  4. Returnگەڕانەوە

    return $sum; ends the function and sends 50 back. (Without return, the function ends at its closing } and gives back NULL.)

    return $sum; کۆتایی بە دەهێنێت و ⁦50⁩ دەنێرێتەوە. (بەبێ return، لە }ی کۆتاییدا تەواو دەبێت و NULL دەگەڕێنێتەوە.)

  5. Continue where you left offبەردەوامبوون لەو شوێنەی وەستابوو

    The call add(20, 30) is replaced by 50, so $total becomes 50, and the page continues with the next line.

    بانگکردنی add(20, 30) دەبێتە ⁦50⁩، بۆیە $total دەبێتە ⁦50⁩ و لاپەڕەکە لە دێڕی دواترەوە بەردەوام دەبێت.

  6. Forget the local variablesلەبیرکردنی گۆڕاوە ناوخۆییەکان

    $num1, $num2 and $sum disappear. The next call starts completely fresh.

    $num1 و $num2 و $sum نامێنن. بانگکردنی داهاتوو بە تەواوی لە سەرەتاوە دەست پێدەکات.

Pass by value and pass by referenceناردن بە نرخ و ناردن بە ئاماژە

So far, every parameter has received a copy of its argument. This is PHP's default, . Sometimes you want the function to change the caller's variable itself. Then you use , marked with &. The picture shows the difference:

تا ئێستا، هەموو کۆپییەکی وەرگرتووە. ئەمە شێوە ئاساییەکەی PHPـیە: . هەندێک جار دەتەوێت خودی بانگکەر بگۆڕێت. ئەو کاتە بەکاردەهێنیت، کە بە & نیشانە دەکرێت. وێنەکە جیاوازییەکە پیشان دەدات:

Two panels. Left, by value: the caller's box $val holds 10; a dashed arrow copies 10 into a separate box $alias inside the function double($alias), which changes to 20. After the call $val is still 10. Right, by reference: one shared box changes from 10 to 30; two labels point to it, $val from the caller and &$realthing from the function triple(&$realthing). After the call $val is 30.دوو بەش. لای چەپ، بە نرخ: سندوقی بانگکەر $val ژمارە 10ی تێدایە؛ تیرێکی پچڕپچڕ 10 کۆپی دەکات بۆ سندوقێکی جیا بە ناوی $alias لەناو کرداری double($alias)، کە دەبێتە 20. دوای بانگکردن $val هێشتا 10ـە. لای ڕاست، بە ئاماژە: یەک سندوقی هاوبەش لە 10ەوە دەبێتە 30؛ دوو ناو ئاماژەی بۆ دەکەن، $val لە بانگکەرەوە و &$realthing لە کرداری triple(&$realthing)ەوە. دوای بانگکردن $val دەبێتە 30.
By value the function works on a copy; by reference it works on the caller's own variable.بە لەسەر کۆپییەک کار دەکات؛ بە لەسەر خودی بانگکەر کار دەکات.CêIT
A photocopy or the original notebookفۆتۆکۆپی یان دەفتەرە ڕەسەنەکە

You ask a teacher to correct your homework. If you hand in a photocopy, the teacher writes red marks on the copy, and your notebook at home stays clean. That is pass by value. If you hand in your original notebook, every mark the teacher makes stays in your notebook. That is pass by reference: the function works on your own variable, not on a copy.

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

Where the analogy stops workingسنووری ئەم نموونەیە

With a reference nothing is really handed over: the caller and the function simply use two names for one and the same variable.

لە هیچ شتێک بەڕاستی نادرێت بە کەس: بانگکەر و تەنها دوو ناو بۆ یەک بەکاردەهێنن.

PHP Function Parameters - Named Arguments - Variadic Functions & Unpacking - Full PHP 8 TutorialProgram With Gio · Englishئینگلیزی · 61K viewsبینین · 2.7K likesلایک · Partبەشی 3:01–4:00 (ofلە 12:05)

A short, clear clip on the difference between passing a value and passing a reference (&) to a PHP function.

بەشێکی کورت و ڕوون دەربارەی جیاوازیی و (&) بۆ .

Watch 3:01–4:00, pass by value vs pass by reference

لە ⁦3:01⁩ تا ⁦4:00⁩ سەیر بکە: بەراورد بە .

Pass by value and pass by referenceناردن بە نرخ و ناردن بە ئاماژە

Pass by value (the default)ناردن بە نرخ (شێوە ئاساییەکە)

When you pass a variable to a function, the function receives a copy of its value. Changes inside the function do not affect the original variable outside. This is called call by value or , because the function gets the variable's value, not the variable itself.

کاتێک دەنێریت بۆ ، کۆپییەکی وەردەگرێت. گۆڕانکاری لەناو کار ناکاتە سەر ڕەسەنەکەی دەرەوە. پێی دەگوترێت ⁦call by value⁩ یان ، چونکە وەردەگرێت، نەک خودی .

Call by valueناردن بە نرخ ⁦(call by value)⁩index.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function double($alias) {
    $alias = $alias * 2;
    return $alias;
}

$val = 10;
$dval = double($val);
echo "Value = $val Doubled = $dval";
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

$alias receives a copy of 10.

$alias کۆپییەکی ⁦10⁩ وەردەگرێت.

Lineدێڕی 3

Only the copy becomes 20.

تەنها کۆپییەکە دەبێتە ⁦20⁩.

Lineدێڕی 8

$dval gets the returned 20.

$dval ژمارە ⁦20⁩ی گەڕاوە وەردەگرێت.

Lineدێڕی 9

$val is still 10.

$val هێشتا ⁦10⁩ـە.

Quick checkپشکنینی خێراfunction double($n) { $n = $n * 2; return $n; } What is $v after $v = 10; double($v);?function double($n) { $n = $n * 2; return $n; } دوای $v = 10; double($v);، $v چەندە؟

Pass by value and pass by referenceناردن بە نرخ و ناردن بە ئاماژە

Pass by reference with &ناردن بە ئاماژە بە &

Sometimes we want a function to change one of its arguments. Then put & in front of the parameter in the definition. Now the parameter is not a copy: it is a second name (a ) for the caller's variable. Whatever the function does to it happens to the original. This is call by reference, or .

هەندێک جار دەمانەوێت یەکێک لە بگۆڕێت. ئەو کاتە & لە پێش لە ناساندنەکەدا دابنێ. ئیتر کۆپی نییە: ناوێکی دووەمە () بۆ بانگکەر. هەر شتێک پێی بکات، بە ڕەسەنەکەش دەکرێت. ئەمە ⁦call by reference⁩ـە، یان .

Call by referenceناردن بە ئاماژە ⁦(call by reference)⁩index.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function triple(&$realthing) {
    $realthing = $realthing * 3;
}

$val = 10;
triple($val);
echo "Triple = $val";
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

&$realthing: a reference to the caller's variable, not a copy.

&$realthing: بۆ بانگکەر، نەک کۆپی.

Lineدێڕی 3

This changes $val itself.

ئەمە خودی $val دەگۆڕێت.

Lineدێڕی 7

No return and no assignment needed: $val is changed directly.

نە return پێویستە نە : $val ڕاستەوخۆ دەگۆڕێت.

Lab 4, Example 2 ("with argument") puts the two styles side by side: addFive() takes its parameter by value, addSix() by reference. The lab sheet shows only the two definitions, with no calls, so you cannot see the difference (and its layout is broken). Here they are with calls added:

نموونەی دووەمی تاقیگەی ٤ ⁦("with argument")⁩ هەردوو شێوەکە لە تەنیشت یەکەوە دادەنێت: addFive() بە وەردەگرێت، addSix() بە . پەڕەی تاقیگەکە تەنها هەردوو ناساندنەکە پیشان دەدات، بێ هیچ بانگکردنێک، بۆیە جیاوازییەکە نابینیت (شێوەی دانانی کۆدەکەش لە پەڕەکەدا تێکچووە). لێرەدا بانگکردنمان بۆ زیاد کردوون:

Lab 4, Example 2, with callsتاقیگەی ٤، نموونەی ٢، لەگەڵ بانگکردنindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function addFive($num) {
    $num += 5;
}

function addSix(&$num) {
    $num += 6;
}

$orignum = 10;
addFive($orignum);
echo "After addFive: $orignum<br>";

addSix($orignum);
echo "After addSix: $orignum<br>";
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2-4

By value: only the local copy grows by 5, then it is thrown away.

بە : تەنها کۆپییە ناوخۆییەکە 5ی بۆ زیاد دەبێت، پاشان فڕێ دەدرێت.

Lineدێڕی 6-8

By reference: the caller's variable grows by 6.

بە : بانگکەر 6ی بۆ زیاد دەبێت.

Lineدێڕی 11

$orignum stays 10.

$orignum هەر ⁦10⁩ دەمێنێتەوە.

Lineدێڕی 14

$orignum becomes 16.

$orignum دەبێتە ⁦16⁩.

Pass by value and pass by referenceناردن بە نرخ و ناردن بە ئاماژە

& goes in the definition only& تەنها لە ناساندنەکەدا دادەنرێت

Write & only in the function definition, never in the call. Very old PHP versions allowed triple(&$val) ("call-time pass-by-reference"); it was removed in PHP 5.4, and today it is a :

& تەنها لە ناساندنی بنووسە، هەرگیز لە بانگکردندا. وەشانە زۆر کۆنەکانی PHP ڕێگەیان بە triple(&$val) دەدا ⁦("call-time pass-by-reference")⁩؛ لە ⁦PHP 5.4⁩دا لابرا و ئەمڕۆ :

& in the call: not allowed& لە بانگکردندا: ڕێگەپێنەدراوindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function triple(&$realthing) {
    $realthing = $realthing * 3;
}

$val = 10;
triple(&$val);
echo "Triple = $val";
?>

Pass by value and pass by referenceناردن بە نرخ و ناردن بە ئاماژە

A reference needs a variableئاماژە پێویستی بە گۆڕاو هەیە

A reference must point to a variable: a named place that can be changed. A literal such as 5 is not a variable; there is nothing for the function to change. So passing a literal to a by-reference parameter is a :

دەبێت ئاماژە بۆ بکات: شوێنێکی ناونراو کە دەتوانرێت بگۆڕدرێت. ڕاستەوخۆ وەک 5 نییە؛ هیچ شتێک نییە بیگۆڕێت. بۆیە ناردنی ژمارەیەکی ڕاستەوخۆ بۆ کە بە وەردەگرێت :

A literal cannot be passed by referenceنرخی ڕاستەوخۆ بە ئاماژە نانێردرێتindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function addFive($num) {
    return $num += 5;
}

function addSix(&$num) {
    return $num += 6;
}

echo addsix(5); // Fatal Error
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 6-8

addSix() wants a variable it can change.

addSix() دەوێت کە بتوانێت بیگۆڕێت.

Lineدێڕی 10

5 is a literal, so PHP stops. (Note also addsix for addSix: names ignore case.)

5 ڕاستەوخۆیە، بۆیە PHP ڕادەوەستێت. (هەروەها سەرنج بدە addsix لە جیاتی addSix: ناوەکان گوێ بە قەبارەی پیت نادەن.)

Quick checkپشکنینی خێراFor function addSix(&$num), which call is an error?بۆ function addSix(&$num)، کام بانگکردن ؟

Pass by value and pass by referenceناردن بە نرخ و ناردن بە ئاماژە

Passing another function's resultناردنی ئەنجامی کردارێکی تر

Passing the result of another function is a special case. A return value is not a variable either, but PHP lets it through with a notice and works on a temporary copy:

ناردنی ئەنجامی تر حاڵەتێکی تایبەتە. نییە، بەڵام PHP بە تێبینییەک ڕێگەی پێدەدات و لەسەر کۆپییەکی کاتی کار دەکات:

A return value passed by referenceنرخی گەڕاوە کە بە ئاماژە نێردراوەindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function addFive($num) {
    return $num += 5;
}

function addSix(&$num) {
    return $num += 6;
}

echo addsix(ADDFive(5));
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 10

ADDFive(5) returns 10; addsix() adds 6 and returns 16. The mixed capitals still work.

ADDFive(5) ⁦10⁩ دەگەڕێنێتەوە؛ addsix() 6 زیاد دەکات و ⁦16⁩ دەگەڕێنێتەوە. پیتە تێکەڵەکان هەر کار دەکەن.

Store the result in a variable first. Then the reference has a real variable to work on, and you can see that it changed:

سەرەتا ئەنجامەکە لە هەڵبگرە. ئەو کاتە ڕاستەقینەی هەیە بۆ کارکردن لەسەری و دەبینیت کە گۆڕاوە:

Store first, then pass by referenceسەرەتا هەڵبگرە، پاشان بە ئاماژە بینێرەindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function addFive($num) {
    return $num + 5;
}

function addSix(&$num) {
    $num += 6;
    return $num;
}

$value = addFive(5);   // $value is 10
echo addSix($value) . "<br>";   // 16
echo $value;                    // also 16: addSix() changed it
?>

One name, one functionیەک ناو، یەک کردار

Each function name can be defined only once in a script. Defining the same name a second time is a . Because function names ignore case, a() and A() count as the same name:

هەر ناوێکی تەنها یەک جار لە دەناسێنرێت. ناساندنی هەمان ناو بۆ جاری دووەم . چونکە ناوی گوێ بە قەبارەی پیت نادەن، a() و A() وەک یەک ناو دادەنرێن:

a() and A() are the same namea() و A() هەمان ناونindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function a() {}
function A() {}   // Function names are case-insensitive
?>

The page prints nothing at all, not even from lines before the error: PHP finds this problem while it reads the file, before it runs any line. Code editors such as VS Code underline the second definition in red even before you run the page. The same error appears if you reuse the name of a built-in function:

لاپەڕەکە هیچ شتێک چاپ ناکات، تەنانەت لە دێڕەکانی پێش : PHP ئەم کێشەیە لە کاتی خوێندنەوەی فایلەکەدا دەدۆزێتەوە، پێش ئەوەی هیچ دێڕێک جێبەجێ بکات. وەک ⁦VS Code⁩ تەنانەت پێش کردنەوەی لاپەڕەکە هێڵێکی سوور بە ژێر ناساندنی دووەمدا دەکێشن. هەمان دەردەکەوێت ئەگەر ناوی دووبارە بەکاربهێنیتەوە:

strlen is already takenstrlen پێشتر گیراوەindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function strlen($text) {
    return 0;
}
?>
Quick checkپشکنینی خێراA file defines function test() {} and later function TEST() {}. What happens?فایلێک function test() {} و دواتر function TEST() {} دەناسێنێت. چی ڕوودەدات؟

Variables inside functions (scope)گۆڕاوەکان لەناو کردارەکاندا (مەودا)

Each function has its own small world of variables, called its . A variable created inside a function is a : it exists only while the function runs, and the rest of the page cannot see it. The parameters are local variables too.

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

A local variable is invisible outsideگۆڕاوی ناوخۆیی لە دەرەوە نابینرێتindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function makeTea() {
    $cups = 3;   // a local variable
    echo "Making $cups cups of tea.<br>";
}

makeTea();
echo "Cups outside: $cups";
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

$cups is created inside makeTea().

$cups لەناو makeTea()دا دروست دەکرێت.

Lineدێڕی 8

Outside the function $cups does not exist: a warning, and nothing is printed for it.

لە دەرەوەی $cups بوونی نییە: و هیچ شتێکی بۆ چاپ ناکرێت.

It also works the other way. A variable created outside all functions lives in the , and it is not visible inside a function:

پێچەوانەکەشی ڕاستە. ئەو لە دەرەوەی هەموو دروست دەکرێت لە دەژیت و لەناو نابینرێت:

A global variable is invisible insideگۆڕاوی گشتی لەناوەوە نابینرێتindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$city = "Sulaymaniyah";

function showCity() {
    echo "City: $city";   // $city is not visible here
}

showCity();
?>
Quick checkپشکنینی خێرا$city = "Erbil"; function show() { echo $city; } show(); What happens?$city = "Erbil"; function show() { echo $city; } show(); چی ڕوودەدات؟

PHP Tutorial (& MySQL) #14 - Variable ScopeNet Ninja · Englishئینگلیزی · 66K viewsبینین · 1.3K likesلایک · Partبەشی 0:00–7:49 (ofلە 8:57)

Why a function cannot see variables from outside, how passing them in or using global fixes this, and how local variables work.

بۆچی ناتوانێت دەرەوەی خۆی ببینێت، چۆن ناردنیان بۆ ناوی، یان بەکارهێنانی global، ئەمە چارەسەر دەکات و چۆن کار دەکەن.

Watch 0:00–7:49: local scope, passing values in, global variables

لە ⁦0:00⁩ تا ⁦7:49⁩ سەیر بکە: ، ناردنی بۆ ناو و .

Variables inside functions (scope)گۆڕاوەکان لەناو کردارەکاندا (مەودا)

The clean solution: pass the value inچارەسەرە پاکەکە: نرخەکە بنێرە ناوەوە

The clean solution is to pass the value in as an argument:

چارەسەرە پاکەکە ئەوەیە کە وەک بنێریتە ناوەوە:

Pass it in insteadلە جیاتی ئەوە بینێرە ناوەوەindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$city = "Sulaymaniyah";

function showCity($place) {
    echo "City: $place";
}

showCity($city);
?>

There is more to scope, such as static variables, the $GLOBALS array and the . All of this is explained in Constants and variable scope.

زیاتری تێدایە، وەک و $GLOBALS و . هەموو ئەمانە لە وانەی نەگۆڕەکان و مەودادا ڕوون کراونەتەوە.

Variables inside functions (scope)گۆڕاوەکان لەناو کردارەکاندا (مەودا)

The keyword global (lab program)وشە کلیلەییەکەی global (بەرنامەکەی تاقیگە)

In the lab session, students wrote a program that prints the even and the odd numbers from 1 to 10 with two functions. It brings together a , an if test and the operator %: $count % 2 == 0 is true for even numbers and $count % 2 == 1 for odd ones. It also calls both functions before defining them, and it uses the keyword global:

لە کاتی تاقیگەدا، خوێندکاران نووسی کە ژمارە جووتەکان و تاکەکان لە 1 تا ⁦10⁩ بە دوو چاپ دەکات. و تاقیکردنەوەیەکی if و % پێکەوە کۆ دەکاتەوە: $count % 2 == 0 بۆ ژمارە جووتەکان ڕاستە و $count % 2 == 1 بۆ تاکەکان. هەروەها هەردوو پێش ناساندنیان بانگ دەکات و global بەکاردەهێنێت:

Even and odd numbers (lab program)ژمارە جووت و تاکەکان (بەرنامەکەی تاقیگە)index.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<html>
    <body>
        <?php
            $count = 1;
            echo "Even numbers: ";

            displayEvenNums();

            echo "<br />";

            echo "Odd numbers: ";
            displayOddNums();

            function displayEvenNums() {
                global $count;
                while ($count <= 10) {
                    if ($count % 2 == 0) {
                        echo "$count".", ";
                    }
                    $count++;
                }
            }

            function displayOddNums() {
                $count = 1;
                while ($count <= 10) {
                    if ($count % 2 == 1) {
                        echo "$count".", ";
                    }
                    $count++;
                }
            }

        ?>
    </body>
</html>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 4

A global variable $count, created outside all functions.

بە ناوی $count، کە لە دەرەوەی هەموو دروست کراوە.

Lineدێڕی 7, 13

Both functions are called before their definitions (lines 15 and 25).

هەردوو پێش ناساندنیان (دێڕی ١٥ و ٢٥) بانگ دەکرێن.

Lineدێڕی 16

global $count; makes $count here mean the global $count.

global $count; وا دەکات $countی ئێرە هەمان $countی گشتی بێت.

Lineدێڕی 18

% gives the remainder; a remainder of 0 means even.

% دەدات؛ 0 واتە جووت.

Lineدێڕی 26

This function makes its own local $count.

ئەم $countێکی ناوخۆیی بۆ خۆی دروست دەکات.

global $count; tells PHP: inside this function, $count is the global variable $count, not a new local one. So displayEvenNums() changes the global variable: when it finishes, the global $count is 11. displayOddNums() does not use global; it creates its own local $count = 1, so it does not care that the global one is now 11.

global $count; بە PHP دەڵێت: لەناو ئەم ، $count هەمان $countـە، نەک کە تازە دروست بکرێت. بۆیە displayEvenNums() دەگۆڕێت: کاتێک تەواو دەبێت، $countی گشتی دەبێتە ⁦11⁩. displayOddNums() global بەکارناهێنێت؛ $count = 1ی ناوخۆیی بۆ خۆی دروست دەکات، بۆیە گرنگ نییە لای کە گشتییەکە ئێستا ⁦11⁩ـە.

Proof: the global variable was changedبەڵگە: گۆڕاوە گشتییەکە گۆڕاوەindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$count = 1;
function displayEvenNums() {
    global $count;
    while ($count <= 10) {
        if ($count % 2 == 0) {
            echo "$count, ";
        }
        $count++;
    }
}
displayEvenNums();
echo "<br>After the call, \$count is $count";
?>

Common mistakes and how to fix themهەڵە باوەکان و چۆنیەتی چاککردنیان

Most function problems produce a clear message. Learn to recognise these, and you will fix them in seconds. Every message below was produced by PHP 8.

زۆربەی کێشەکانی پەیامێکی ڕوون دروست دەکەن. فێری ناسینەوەیان ببە و لە چەند چرکەیەکدا چاکیان دەکەیتەوە. هەموو پەیامەکانی خوارەوە ⁦PHP 8⁩ دروستی کردوون.

Function errors at a glance بە یەک چاوپێداخشاندن
MistakeهەڵەWhat PHP saysPHP چی دەڵێتFixچارەسەر
Curly quotes “ ” copied from slides or a word processor خوار “ ” کە لە سلاید یان نووسینەوە کۆپی کراونParse error: syntax error, unexpected identifier ...Retype the quotes as straight quotes " بە ڕاستی " بنووسەوە
Naming a function print, echo or another reserved wordناونانی بە print یان echo یان وشەیەکی پارێزراوی ترParse error: syntax error, unexpected token "print", expecting "("Choose a free, descriptive name (Lab fix 1)ناوێکی ئازاد و ڕوون هەڵبژێرە (چاککردنی ١)
Calling a name that was never defined (typing mistake, wrong name)بانگکردنی ناوێک کە هەرگیز نەناسێنراوە (هەڵەی نووسین، ناوی هەڵە)Fatal error: Uncaught Error: Call to undefined function gret()Use exactly the name in the definition (Lab fix 2)ڕێک ئەو ناوە بەکاربهێنە کە لە ناساندنەکەدایە (چاککردنی ٢)
Forgetting the parentheses: greet;لەبیرکردنی کەوانەکان: greet;Fatal error: Uncaught Error: Undefined constant "greet"Write greet();greet(); بنووسە
Defining the same name twice (also a() and A(), or a built-in name)ناساندنی هەمان ناو دوو جار (هەروەها a() و A()، یان ناوی )Fatal error: Cannot redeclare function a()Give every function its own nameناوێکی تایبەت بدە بە هەر
A missing or extra { or }{ یان }ی کەم یان زیادەParse error: Unclosed '{' on line 6Match every { with a }; indent your code (Lab fix 3)هەر {ێک بە }ێک تەواو بکە؛ کۆدەکەت ڕێک بخە بە (چاککردنی ٣)
Too few arguments کەمتر لە پێویستFatal error: Uncaught ArgumentCountError: Too few arguments to function ...Pass every required argument, or give the parameter a default valueهەموو پێویستەکان بنێرە، یان بدە بە
An optional parameter before a required one ئارەزوومەندانە پێش پێویستێکDeprecated: ... Optional parameter $greeting declared before required parameter $name ...Put parameters with defaults last کە هەیە بخە کۆتایی
Expecting a by-value function to change your variableچاوەڕوانکردنی ئەوەی کە بە وەردەگرێت بگۆڕێتNo message: the variable is simply unchangedهیچ پەیامێک نییە: بە سادەیی ناگۆڕێتreturn the new value and assign it, or use & نوێیەکە بە return بگەڕێنەوە و دایبنێ، یان & بەکاربهێنە
Writing & in the call: triple(&$val)نووسینی & لە بانگکردندا: triple(&$val)Parse error: syntax error, unexpected token "&"Write & only in the definition& تەنها لە ناساندنەکەدا بنووسە
Passing a literal to a & parameter: addSix(5)ناردنی ڕاستەوخۆ بۆ &: addSix(5)Fatal error: Uncaught Error: addSix(): Argument #1 ($num) could not be passed by referencePass a variable بنێرە
Using an outside variable inside a functionبەکارهێنانی دەرەوە لەناو Warning: Undefined variable $cityPass it in as an argumentوەک بینێرە ناوەوە
Code placed after returnکۆد لە دوای returnNo message: the code just never runsهیچ پەیامێک نییە: کۆدەکە هەرگیز جێبەجێ ناکرێتMove it before returnبیبە بۆ پێش return
Using \n for new lines on a web pageبەکارهێنانی \n بۆ دێڕی نوێ لە No message: everything appears on one lineهیچ پەیامێک نییە: هەموو شتێک لە یەک دێڕدا دەردەکەوێتPrint <br><br> چاپ بکە

Lab 4 and exercisesتاقیگەی ٤ و ڕاهێنانەکان

Lab 4 ("Function & Array") asks you to type and run its examples. Below are the lab's function tasks, the extra programs written during the lab session, and some new practice. Try each one yourself before you open the answer.

تاقیگەی ٤ ⁦("Function & Array")⁩ داوات لێدەکات نموونەکانی بنووسیت و جێبەجێیان بکەیت. لە خوارەوە ئەرکەکانی تاقیگەکە، کاتی تاقیگەکە و چەند ڕاهێنانێکی نوێ هەن. پێش کردنەوەی چارەسەرەکە، هەر یەکێکیان خۆت تاقی بکەرەوە.

Setting up Lab 4ئامادەکردنی تاقیگەی ٤
  1. Start the serverدەستپێکردنی ڕاژەکارەکە

    Open the Control Panel and start Apache (see XAMPP).

    بکەرەوە و Apache دەست پێبکە (بڕوانە وانەی XAMPP).

  2. Make a project folderدروستکردنی فۆڵدەرێک بۆ پڕۆژەکە

    Inside , create a called function_array, for example C:\xampp\htdocs\function_array.

    لەناو بە ناوی function_array دروست بکە، بۆ نموونە C:\xampp\htdocs\function_array.

  3. Create index.phpدروستکردنی index.php

    Create the file index.php in that folder with your editor. The lab sheet names NetBeans; in the lab session students used VS Code. Any or works. (VS Code may open the folder in Restricted Mode; choose to trust the folder to turn on all features.)

    بە دەستکاریکەرەکەت فایلی index.php لەو دروست بکە. پەڕەی تاقیگەکە ناوی NetBeans دەهێنێت؛ لە کاتی تاقیگەدا خوێندکاران ⁦VS Code⁩ـیان بەکارهێنا. هەر یان کار دەکات. (لەوانەیە ⁦VS Code⁩ بە ⁦Restricted Mode⁩ بکاتەوە؛ متمانە بە بکە بۆ ئەوەی هەموو توانستەکانی چالاک بن.)

  4. Add the PHP tagsزیادکردنی تاگەکانی PHP

    The lab snippets have no <?php ... ?> tags. Write each example between them.

    پارچە کۆدەکانی تاقیگەکە <?php ... ?>ـیان تێدا نییە. هەر نموونەیەک لە نێوانیاندا بنووسە.

  5. Run itجێبەجێکردن

    Open http://localhost/function_array/ in the browser. After every change, save and refresh the page.

    http://localhost/function_array/ لە بکەرەوە. دوای هەر گۆڕانکارییەک، پاشەکەوت بکە و لاپەڕەکە نوێ بکەرەوە.

Lab 4 and exercisesتاقیگەی ٤ و ڕاهێنانەکان

Lab 4: the function examplesتاقیگەی ٤: نموونەکانی کردار

ExerciseڕاهێنانLab Example 1: a function without parametersنموونەی ١ی تاقیگە: کردارێک بێ پارامیتەر

Write a function without parameters that prints You are running a function without arguments. Then call it. The lab sheet names this function Print(): explain why that name cannot work.

بێ بنووسە کە ⁦You are running a function without arguments⁩. چاپ بکات. پاشان بانگی بکە. پەڕەی تاقیگەکە ناوی ئەم دەنێت Print(): ڕوونی بکەرەوە بۆچی ئەو ناوە کار ناکات.

Hintئاماژە

Pick a name that is not a PHP keyword, such as printMessage.

ناوێک هەڵبژێرە کە نەبێت لە PHPـدا، وەک printMessage.

Show answerوەڵامەکە پیشان بدە
index.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
/* Defining a PHP function */
function printMessage() {
    echo "You are running a function without arguments.";
}

/* Calling the function */
printMessage();
?>

print is a (a reserved word) and function names ignore case, so Print cannot be used as a new function name. See Lab fix 1.

print (وشەیەکی پارێزراو) و ناوی گوێ بە قەبارەی پیت نادەن، بۆیە Print ناتوانرێت وەک ناوی نوێ بەکاربهێنرێت. بڕوانە چاککردنی ١.

ExerciseڕاهێنانLab Example 2: by value and by referenceنموونەی ٢ی تاقیگە: بە نرخ و بە ئاماژە

Define addFive($num), which adds 5 to its parameter (by value), and addSix(&$num), which adds 6 (by reference). Set $orignum = 10. Call addFive($orignum) and print $orignum; then call addSix($orignum) and print it again. Predict both numbers before you run the code.

addFive($num) بناسێنە کە 5 بۆ زیاد دەکات (بە ) و addSix(&$num) کە 6 زیاد دەکات (بە ). $orignum = 10 دابنێ. addFive($orignum) بانگ بکە و $orignum چاپ بکە؛ پاشان addSix($orignum) بانگ بکە و دووبارە چاپی بکەرەوە. پێش جێبەجێکردنی کۆدەکە، هەردوو ژمارەکە پێشبینی بکە.

Show answerوەڵامەکە پیشان بدە
index.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function addFive($num) {
    $num += 5;
}

function addSix(&$num) {
    $num += 6;
}

$orignum = 10;
addFive($orignum);
echo "After addFive: $orignum<br>";

addSix($orignum);
echo "After addSix: $orignum<br>";
?>

addFive() changes only its local copy, so $orignum stays 10. addSix() works on $orignum itself through the reference, so it becomes 16.

addFive() تەنها کۆپییە ناوخۆییەکەی خۆی دەگۆڕێت، بۆیە $orignum هەر ⁦10⁩ دەمێنێتەوە. addSix() لە ڕێگەی لەسەر خودی $orignum کار دەکات، بۆیە دەبێتە ⁦16⁩.

ExerciseڕاهێنانLab Example 3: more than one argumentنموونەی ٣ی تاقیگە: زیاتر لە یەک ئارگیومێنت

Write calculate_Function($num1, $num2), which prints the sum, the remainder and the quotient of its two numbers, each on its own line. Call it with 20 and 10.

calculate_Function($num1, $num2) بنووسە کە کۆ و و ئەنجامی دابەشکردنی دوو ژمارەکەی چاپ بکات، هەریەکەیان لە دێڕێکی جیادا. بە ⁦20⁩ و ⁦10⁩ بانگی بکە.

Show answerوەڵامەکە پیشان بدە
index.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function calculate_Function($num1, $num2) {
    $sum = $num1 + $num2;
    $mod = $num1 % $num2;
    $div = $num1 / $num2;
    echo "Sum of the two numbers is : $sum<br>";
    echo "mod of the two numbers is : $mod<br>";
    echo "div of the two numbers is : $div<br>";
}

calculate_Function(20, 10);
calculate_Function(7, 2);
?>

Call the function by the name you defined. The lab sheet calls addFunction(20, 10) by mistake; see Lab fix 2.

بەو ناوە بانگ بکە کە ناساندووتە. پەڕەی تاقیگەکە بە هەڵە addFunction(20, 10) بانگ دەکات؛ بڕوانە چاککردنی ٢.

ExerciseڕاهێنانLab Example 4: returning a valueنموونەی ٤ی تاقیگە: گەڕاندنەوەی نرخێک

Write addFunction($num1, $num2), which returns the sum. Store the result of addFunction(10, 20) in $return_value and print Returned value from the function : 30.

addFunction($num1, $num2) بنووسە کە کۆکە بگەڕێنێتەوە. ئەنجامی addFunction(10, 20) لە $return_valueدا هەڵبگرە و ⁦Returned value from the function : 30⁩ چاپ بکە.

Show answerوەڵامەکە پیشان بدە
index.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function addFunction($num1, $num2) {
    $sum = $num1 + $num2;
    return $sum;
}

$return_value = addFunction(10, 20);
echo "Returned value from the function : $return_value";
?>
ExerciseڕاهێنانLab Example 5: a default valueنموونەی ٥ی تاقیگە: نرخێکی بنەڕەتی

Write printMe($param = NULL), which prints $param. Call it once with This is test and once with no argument. What does each call print?

printMe($param = NULL) بنووسە کە $param چاپ بکات. جارێک بە ⁦This is test⁩ و جارێک بێ هیچ بانگی بکە. هەر بانگکردنێک چی چاپ دەکات؟

Show answerوەڵامەکە پیشان بدە
index.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function printMe($param = NULL) {
    print $param;
}

printMe("This is test");
printMe();   // uses the default NULL, so nothing is printed
?>

The first call prints This is test. The second uses the default NULL, which prints as nothing. Do not copy the stray { from the lab sheet; see Lab fix 3.

بانگکردنی یەکەم ⁦This is test⁩ چاپ دەکات. دووەم NULL بەکاردەهێنێت، کە هیچ چاپ ناکات. {ە زیادەکەی پەڕەی تاقیگەکە کۆپی مەکە؛ بڕوانە چاککردنی ٣.

Lab 4 and exercisesتاقیگەی ٤ و ڕاهێنانەکان

Programs from the lab sessionبەرنامەکانی کاتی تاقیگە

ExerciseڕاهێنانLab session: even and odd numbersکاتی تاقیگە: ژمارە جووت و تاکەکان

Using two functions and while loops, print the even numbers from 1 to 10 on one line and the odd numbers on the next line.

بە بەکارهێنانی دوو و ، ژمارە جووتەکان لە 1 تا ⁦10⁩ لە یەک دێڕدا و ژمارە تاکەکان لە دێڕی دواتردا چاپ بکە.

Hintئاماژە

A number is even when $n % 2 == 0.

ژمارەیەک جووتە کاتێک $n % 2 == 0 بێت.

Show answerوەڵامەکە پیشان بدە

The lab's own program (with global) is shown in The keyword global, together with a cleaner version that returns a string. Here is a third way: one function with two parameters that jumps by 2, so it needs no % test at all.

خودی تاقیگەکە (بە global) لە وشە کلیلەییەکەی globalدا پیشان دراوە، لەگەڵ وەشانێکی پاکتر کە دەگەڕێنێتەوە. ئەمەش ڕێگەی سێیەمە: یەک بە دوو کە 2 بە 2 باز دەدات، بۆیە هیچ پێویستی بە تاقیکردنەوەی % نییە.

index.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function displayNumbers($start, $end) {
    $count = $start;
    while ($count <= $end) {
        echo $count;
        if ($count + 2 <= $end) {
            echo ", ";
        }
        $count += 2;
    }
}

echo "Even numbers: ";
displayNumbers(2, 10);
echo "<br>";
echo "Odd numbers: ";
displayNumbers(1, 10);
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 5

Print the number, then a comma only if another number follows.

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

Lineدێڕی 9

Jump to the next even (or odd) number.

باز بدە بۆ ژمارەی جووتی (یان تاکی) دواتر.

ExerciseڕاهێنانLab session: echo first, then returnکاتی تاقیگە: سەرەتا echo، پاشان return

Write sum($num1, $num2). If one of the numbers is null, it prints One of the two numbers is not set yet.; otherwise it prints the sum. Then rewrite it so that it returns the text instead, and print Summation 1: ... for sum(1, 1) and Summation 2: ... for sum(1, null).

sum($num1, $num2) بنووسە. ئەگەر یەکێک لە ژمارەکان null بێت، ⁦One of the two numbers is not set yet⁩. چاپ دەکات؛ ئەگەرنا کۆکە چاپ دەکات. پاشان بە شێوەیەک دووبارە بینووسەرەوە کە لە جیاتی ئەوە دەقەکە بگەڕێنێتەوە و بۆ sum(1, 1) ⁦Summation 1⁩: ... و بۆ sum(1, null) ⁦Summation 2⁩: ... چاپ بکە.

Show answerوەڵامەکە پیشان بدە
index.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function sum($num1, $num2) {
    if (!isset($num1) || !isset($num2)) {
        echo "One of the two numbers is not set yet.";
    } else {
        echo "Summation of $num1 and $num2 is " . ($num1 + $num2);
    }
}

sum(1, null);
echo "<br>";
sum(1, 1);
?>
index.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function sum($num1, $num2) {
    if (!isset($num1) || !isset($num2)) {
        return "One of the two numbers is not set yet.";
    } else {
        return "$num1 + $num2 = " . ($num1 + $num2);
    }
}

echo "Summation 1: " . sum(1, 1) . "<br />";
echo "Summation 2: " . sum(1, null);
?>

See Echo or return? for why the second version is more flexible, and for the parentheses around $num1 + $num2.

بۆ ئەوەی بزانیت بۆچی وەشانی دووەم نەرمترە و بۆ کەوانەکانی دەوری $num1 + $num2، بڕوانە echo یان return؟.

ExerciseڕاهێنانChallenge: print a table of numbersئاستەنگ: چاپکردنی خشتەیەکی ژمارە

Try this after the Multidimensional arrays lecture. In the lab session, students stored two rows of three numbers, array(0, 3, 6) and array(5, 7, 9), in an array $a, and printed every value with its row and column number using two nested for loops. Write a function printGrid($grid) that does the same for any such array.

ئەمە دوای وانەی ڕیزەکە فرەڕەهەندەکان تاقی بکەرەوە. لە کاتی تاقیگەدا، خوێندکاران دوو لە سێ ژمارە، array(0, 3, 6) و array(5, 7, 9)، لە $aدا هەڵگرت و هەر لەگەڵ ژمارەی و بە دوو ناوەکی چاپ کرد. بە ناوی printGrid($grid) بنووسە کە هەمان کار بۆ هەر لەو جۆرە بکات.

Hintئاماژە

Use count($grid) for the number of rows and count($grid[$row]) for the number of columns.

بۆ ژمارەی count($grid) و بۆ ژمارەی count($grid[$row]) بەکاربهێنە.

Show answerوەڵامەکە پیشان بدە
index.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function printGrid($grid) {
    for ($row = 0; $row < count($grid); $row++) {
        echo "<p>Row num: $row</p>";
        echo "<ul>";
        for ($col = 0; $col < count($grid[$row]); $col++) {
            echo "<li>Col num ($col): " . $grid[$row][$col] . "</li>";
        }
        echo "</ul>";
    }
}

$a = array(
    array(0, 3, 6),   // Row 0 (first row)
    array(5, 7, 9)    // Row 1 (second row)
);

printGrid($a);
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

count($grid) is the number of rows, so no number is hard-coded.

count($grid) ژمارەی ، بۆیە هیچ ژمارەیەک بە جێگیری نەنووسراوە.

Lineدێڕی 4-5

Each row gets its own heading and its own <ul> list.

هەر سەردێڕ و لیستی <ul>ی تایبەت بە خۆی هەیە.

Lineدێڕی 7

$grid[$row][$col] is the value in that row and column.

$grid[$row][$col] ئەو کە لەو و .

Lab 4 and exercisesتاقیگەی ٤ و ڕاهێنانەکان

More practiceڕاهێنانی زیاتر

ExerciseڕاهێنانA function add($a, $b)کردارێک بە ناوی add($a, $b)

Write add($a, $b) and call it three times with different numbers.

add($a, $b) بنووسە و سێ جار بە ژمارەی جیاواز بانگی بکە.

Show answerوەڵامەکە پیشان بدە
index.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function add($a, $b) {
    return $a + $b;
}

echo "5 + 3 = " . add(5, 3) . "<br>";
echo "20 + 30 = " . add(20, 30) . "<br>";
echo "-4 + 1.5 = " . add(-4, 1.5);
?>

One definition, three calls. Because add() returns the sum, each call can be joined into a sentence with ..

یەک ناساندن، سێ بانگکردن. چونکە add() کۆکە دەگەڕێنێتەوە، هەر بانگکردنێک دەتوانرێت بە . بلکێنرێت بە ڕستەیەکەوە.

ExerciseڕاهێنانIs it even?جووتە؟

Write isEven($number), which returns true for even numbers and false for odd ones. Use it in a loop to print whether each number from 1 to 4 is even or odd.

isEven($number) بنووسە کە بۆ ژمارە جووتەکان true و بۆ تاکەکان false بگەڕێنێتەوە. لە بەکاری بهێنە بۆ ئەوەی بۆ هەر ژمارەیەک لە 1 تا 4 چاپ بکەیت جووتە یان تاک.

Show answerوەڵامەکە پیشان بدە
index.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function isEven($number) {
    return $number % 2 == 0;
}

for ($i = 1; $i <= 4; $i++) {
    if (isEven($i)) {
        echo "$i is even<br>";
    } else {
        echo "$i is odd<br>";
    }
}
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

The comparison itself is true or false, so it can be returned directly.

بەراوردکردنەکە خۆی true یان falseـە، بۆیە ڕاستەوخۆ دەگەڕێنرێتەوە.

ExerciseڕاهێنانArea with a default heightڕووبەر بە بەرزیی بنەڕەتی

Write rectangleArea($width, $height = 1), which returns the area. What do rectangleArea(4, 5) and rectangleArea(7) return?

rectangleArea($width, $height = 1) بنووسە کە ڕووبەرەکە بگەڕێنێتەوە. rectangleArea(4, 5) و rectangleArea(7) چی دەگەڕێننەوە؟

Show answerوەڵامەکە پیشان بدە
index.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function rectangleArea($width, $height = 1) {
    return $width * $height;
}

echo rectangleArea(4, 5) . "<br>";
echo rectangleArea(7);
?>
ExerciseڕاهێنانFind the bugsباگەکان بدۆزەرەوە

This code has three problems. PHP reports only the first one. Find all three, then fix the code so that it prints Welcome! and then Hello, Karwan!.

ئەم کۆدە سێ کێشەی تێدایە. PHP تەنها یەکەمیان ڕادەگەیەنێت. هەر سێکیان بدۆزەرەوە، پاشان کۆدەکە چاک بکەرەوە بۆ ئەوەی سەرەتا ⁦Welcome!⁩ و پاشان ⁦Hello, Karwan!⁩ چاپ بکات.

index.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function Greet($name) {
    return "Hello, $name!";
    echo "Welcome!";
}

function greet($name) {
    echo "Hi";
}

echo greet();
?>
Show answerوەڵامەکە پیشان بدە
  1. greet() is defined twice (Greet and greet are the same name): Cannot redeclare function greet().
  2. echo "Welcome!"; comes after return, so it would never run.
  3. greet() is called without the required argument: an ArgumentCountError.
  1. greet() دوو جار ناسێنراوە (Greet و greet هەمان ناون): ⁦Cannot redeclare function greet()⁩.
  2. echo "Welcome!"; دوای return هاتووە، بۆیە هەرگیز جێبەجێ ناکرێت.
  3. greet() بێ پێویستەکە بانگ کراوە: ArgumentCountError.
index.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function greet($name) {
    echo "Welcome!<br>";
    return "Hello, $name!";
}

echo greet("Karwan");
?>

Lab 4 and exercisesتاقیگەی ٤ و ڕاهێنانەکان

Practice: values and referencesڕاهێنان: نرخ و ئاماژە

ExerciseڕاهێنانPredict the outputدەرخستەکە پێشبینی بکە

Without running it, what does this code print? Explain each of the three numbers.

بێ جێبەجێکردن، ئەم کۆدە چی چاپ دەکات؟ هەر سێ ژمارەکە ڕوون بکەرەوە.

index.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function change($x, &$y) {
    $x = $x + 1;
    $y = $y + 1;
    return $x + $y;
}

$a = 1;
$b = 1;
$c = change($a, $b);
echo "$a $b $c";
?>
Show answerوەڵامەکە پیشان بدە
Outputدەرئەنجام
1 2 4

$x is a copy, so $a stays 1. $y is a reference, so $b becomes 2. Inside the function, $x and $y are both 2, so it returns 4, and $c is 4.

$x کۆپییە، بۆیە $a هەر 1 دەمێنێتەوە. $y ، بۆیە $b دەبێتە 2. لەناو ، $x و $y هەردووکیان 2ن، بۆیە 4 دەگەڕێنێتەوە و $c دەبێتە 4.

ExerciseڕاهێنانA reference with a default valueئاماژە لەگەڵ نرخێکی بنەڕەتی

Write addBonus(&$score, $bonus = 5), which adds a bonus to a student's score. Give Aram (80) the default bonus and Shilan (70) a bonus of 10, then print both scores.

addBonus(&$score, $bonus = 5) بنووسە کە نمرەیەکی زیادە بۆ نمرەی خوێندکارێک زیاد بکات. نمرە زیادەی بنەڕەتی بدە بە ئارام ⁦(80)⁩ و ⁦10⁩ بدە بە شیلان ⁦(70)⁩، پاشان هەردوو نمرەکە چاپ بکە.

Show answerوەڵامەکە پیشان بدە
index.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function addBonus(&$score, $bonus = 5) {
    $score = $score + $bonus;
}

$aram = 80;
$shilan = 70;
addBonus($aram);        // default bonus: 5
addBonus($shilan, 10);  // bonus of 10
echo "Aram: $aram<br>";
echo "Shilan: $shilan";
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

&$score changes the caller's variable; $bonus is optional.

&$score بانگکەر دەگۆڕێت؛ $bonus ئارەزوومەندانەیە.

ExerciseڕاهێنانSwap two variablesئاڵوگۆڕکردنی دوو گۆڕاو

Write swap(&$a, &$b), which exchanges the values of two variables. Test it with $first = "tea" and $second = "coffee".

swap(&$a, &$b) بنووسە کە دوو ئاڵوگۆڕ بکات. بە $first = "tea" و $second = "coffee" تاقی بکەرەوە.

Hintئاماژە

You need a third, temporary variable.

پێویستت بە سێیەمی کاتی هەیە.

Show answerوەڵامەکە پیشان بدە
index.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function swap(&$a, &$b) {
    $temp = $a;
    $a = $b;
    $b = $temp;
}

$first = "tea";
$second = "coffee";
swap($first, $second);
echo "first: $first, second: $second";
?>

A function can return only one value, but here two variables must change. That is a good use for references.

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

Summary and reviewکورتە و پێداچوونەوە

  • A is a named block of code that runs only when it is called. PHP has (ready to use) and (written by you).
  • Define it with function name($param1, $param2) { ... }; call it with name(arg1, arg2);. Defining alone prints nothing.
  • Names start with a letter or _, may not be reserved words, and are not case-sensitive. Each name can be defined only once.
  • Top-level functions can be called before their definition; functions defined inside a block exist only after it runs.
  • are the variables in the definition; are the values in the call, matched by position. Too few arguments is an error; extra ones are ignored.
  • A ($x = 50) is used when the argument is left out. Optional parameters go last.
  • return sends a value back and ends the function; code after it never runs. No return means NULL. Prefer returning over printing inside the function.
  • By default arguments are passed by value (a copy). With & in the definition they are passed by reference, and the function changes the caller's variable. A reference needs a variable, not a literal.
  • Variables inside a function are local; outside variables are not visible inside unless you pass them in (or use global, which is best avoided).
  • ناونراوە کە تەنها کاتێک جێبەجێ دەکرێت کە بانگ بکرێت. لە PHPـدا دوو جۆریان هەیە: (ئامادەی بەکارهێنانن) و (خۆت دەیاننووسیت).
  • بە function name($param1, $param2) { ... } بیناسێنە؛ بە name(arg1, arg2); بانگی بکە. تەنها ناساندن هیچ چاپ ناکات.
  • ناوەکان بە پیت یان _ دەست پێدەکەن، نابێت وشەی پارێزراو بن و نین. هەر ناوێک تەنها یەک جار دەناسێنرێت.
  • ئاستی سەرەوە دەتوانرێن پێش ناساندنیان بانگ بکرێن؛ ئەوانەی لەناو بلۆکێکدا ناسێنراون تەنها دوای جێبەجێبوونی ئەو بلۆکە هەن.
  • ئەو کە لە ناساندنەکەدان؛ ئەو کە لە بانگکردنەکەدان و بەپێی شوێن پێک دەگەیەنرێن. کەم ؛ زیادەکان پشتگوێ دەخرێن.
  • ($x = 50) کاتێک بەکاردێت کە نەنێردرێت. ئارەزوومەندانەکان لە کۆتاییدا دێن.
  • return دەنێرێتەوە و کۆتایی بە دەهێنێت؛ کۆدی دوای ئەو هەرگیز جێبەجێ ناکرێت. نەبوونی return واتە NULL. گەڕاندنەوە لە چاپکردن لەناو باشترە.
  • بە شێوەی ئاسایی بە دەنێردرێن (کۆپی). بە & لە ناساندنەکەدا بە دەنێردرێن و بانگکەر دەگۆڕێت. پێویستی بە هەیە، نەک ڕاستەوخۆ.
  • ناو ناوخۆیین؛ دەرەوە لەناوەوە نابینرێن مەگەر بیاننێریتە ناوەوە (یان global بەکاربهێنیت، کە باشترە لێی دوور بکەویتەوە).
Review questionsپرسیارەکانی پێداچوونەوە
What are the two types of functions in PHP?دوو جۆری کردار لە PHPـدا کامانەن؟

Built-in functions, which PHP provides ready to use (for example strlen(), str_replace(), phpinfo()), and user-defined functions, which the programmer creates with the keyword function.

، کە PHP ئامادەیان دەکات (بۆ نموونە strlen()، str_replace()، phpinfo()) و ، کە بەرنامەساز بە function دروستیان دەکات.

Does defining a function run its code?ئایا ناساندنی کردارێک کۆدەکەی جێبەجێ دەکات؟

No. A function runs only when it is called. A page that only defines a function prints nothing.

نەخێر. تەنها کاتێک جێبەجێ دەکرێت کە بانگ بکرێت. لاپەڕەیەک کە تەنها دەناسێنێت هیچ چاپ ناکات.

Can you call a function before its definition in the file?دەتوانیت کردارێک پێش ناساندنی لە فایلەکەدا بانگ بکەیت؟

Yes, if it is defined at the top level of the file: PHP reads the whole file first. Not if it is defined inside an if block or another function that has not run yet.

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

Are function names case-sensitive? Are variable names?ئایا ناوی کردارەکان هەستیار بە قەبارەی پیتن؟ ناوی گۆڕاوەکان چۆن؟

Function names are not: addFive() and ADDFIVE() are the same function. Variable names are: $name and $Name are different variables.

ناوی نا: addFive() و ADDFIVE() هەمان . ناوی بەڵێ: $name و $Name دوو جیاوازن.

Why does function Print() {...} fail?بۆچی function Print() {...} شکست دەهێنێت؟

print is a reserved language construct, and because function names ignore case, Print is the same word. PHP gives a parse error. Choose another name, such as printMessage().

print پارێزراوە و چونکە ناوی گوێ بە قەبارەی پیت نادەن، Print هەمان وشەیە. PHP دەدات. ناوێکی تر هەڵبژێرە، وەک printMessage().

What is the difference between a parameter and an argument?جیاوازیی نێوان پارامیتەر و ئارگیومێنت چییە؟

A parameter is the variable in the function definition ($firstName); an argument is the actual value sent in the call ("Aram").

ئەو کە لە ($firstName)؛ ئەو ڕاستەقینەیەیە کە لە بانگکردنەکەدا دەنێردرێت ("Aram").

When is a default value used?نرخی بنەڕەتی کەی بەکاردێت؟

When the caller leaves out the argument for that parameter. In setHeight($minheight = 50), the call setHeight() uses 50.

کاتێک بانگکەر بۆ ئەو نانێرێت. لە setHeight($minheight = 50)دا، بانگکردنی setHeight() ⁦50⁩ بەکاردەهێنێت.

What two things does return do?return چ دوو کارێک دەکات؟

It sends a value back to the caller, and it ends the function immediately, so code after it never runs.

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

Explain call by value and call by reference. Where does & go?ناردن بە نرخ و ناردن بە ئاماژە ڕوون بکەرەوە. & لە کوێ دادەنرێت؟

By value (the default), the function gets a copy, so the caller's variable does not change (double($val) leaves $val at 10). By reference, the parameter is another name for the caller's variable, so changes reach it (triple($val) makes $val 30). The & goes before the parameter in the definition only, never in the call.

بە (شێوە ئاساییەکە)، کۆپییەک وەردەگرێت، بۆیە بانگکەر ناگۆڕێت (double($val) $val لە ⁦10⁩دا دەهێڵێتەوە). بە ، ناوێکی ترە بۆ بانگکەر، بۆیە گۆڕانکارییەکان دەگەنە ئەو (triple($val) $val دەکات بە ⁦30⁩). & تەنها لە ناساندنەکەدا پێش دادەنرێت، هەرگیز لە بانگکردندا.

Why does addSix(5) fail when addSix() takes &$num?بۆچی addSix(5) شکست دەهێنێت کاتێک addSix() &$num وەردەگرێت؟

A reference must point to a variable that can be changed. 5 is a literal, not a variable, so PHP stops with could not be passed by reference. Passing another function's result works only with a notice; store it in a variable first.

دەبێت ئاماژە بۆ بکات کە بگۆڕدرێت. 5 ڕاستەوخۆیە، نەک ، بۆیە PHP بە ⁦could not be passed by reference⁩ ڕادەوەستێت. ناردنی ئەنجامی تر تەنها بە تێبینییەک کار دەکات؛ سەرەتا لە هەڵیبگرە.

Can a function use a variable that was created outside it?دەتوانێت کردارێک گۆڕاوێک بەکاربهێنێت کە لە دەرەوەی ئەو دروست کراوە؟

Not directly: inside a function only its own local variables and parameters are visible. Pass the value in as an argument (best), or declare it with global (best avoided). See Constants and variable scope.

ڕاستەوخۆ نا: لەناو تەنها و خۆی دەبینرێن. وەک بنێرە ناوەوە (باشترین ڕێگا)، یان بە global ڕایبگەیەنە (باشترە لێی دوور بکەویتەوە). بڕوانە وانەی نەگۆڕەکان و مەودا.

Videoڤیدیۆ

Watch a videoسەیری ڤیدیۆیەک بکە

A video that viewers liked a lot and praised in their comments, chosen for this lesson.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.

PHP Full Course for free 🐘Bro Code · Englishئینگلیزی · 3M viewsبینین · 65K likesلایک · Partبەشی 2:27:40–2:36:29 (ofلە 4:00:00)

How to create a function, send values in with arguments, and get a result back with return, with a short practice example.

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

Watch 2:27:40–2:36:29, the functions chapter of a full course

لە ⁦2:27:40⁩ تا ⁦2:36:29⁩ سەیر بکە: بەشی لە کۆرسێکی تەواودا.

Lesson complete!وانەکە تەواو بوو!

Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.

Key termsزاراوە سەرەکییەکان

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

Arrays in PHPڕیزەکەکان لە PHPـدا

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

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