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 ئەنجام وەربگرەوە و جیاوازیی نێوان و فێر ببە. نموونەکانی تاقیگەی ٤یش لەخۆ دەگرێت و هەموو پەڕەی تاقیگەکە دۆزراونەتەوە و چاک کراون.
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
returnto give a result back, and know that code afterreturnnever 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 خۆی زیاتر لە هەزار لەگەڵدایە و تۆش دەتوانیت هەر چەندت پێویست بێت خۆتی بۆ زیاد بکەیت.
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.
دەتوانێت زانیاری وەربگرێت (لە ڕێگەی )، کارێک بکات و ئەنجامێک بگەڕێنێتەوە (). هەردووکیان ئارەزوومەندانەن: لەوانەیە هیچ زانیارییەک وەرنەگرێت و هیچیش نەگەڕێنێتەوە.
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 .
بلۆکێکی ناونراوی کۆدە کە کارێکی دیاریکراو ئەنجام دەدات و دەتوانیت بە بانگکردنی ناوەکەی چەندین جار بەکاری بهێنیتەوە. دەتوانێت وەربگرێت و بگەڕێنێتەوە.
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دوو جۆر: ئامادە و دروستکراو
- are created by PHP and are ready to use. You only call them. Examples:
strlen(),strtoupper(),str_replace(),strstr(),max(),phpinfo(). - are created by you, the programmer, with the keyword
function. They are the subject of this lecture.
- : PHP خۆی دروستی کردوون و ئامادەی بەکارهێنانن. تەنها بانگیان دەکەیت. نموونە:
strlen()،strtoupper()،str_replace()،strstr()،max()،phpinfo(). - : خۆت، وەک بەرنامەساز، بە
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:
هەردوو جۆرەکە بە هەمان شێوە بانگ دەکرێن: ناوەکە، پاشان کەوانە ( ) و ئەو دەتەوێت بیاننێریت لەناویدا. ئەمەش چەند لە کاتی کارکردندا:
<?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);
?>9 HELLO Hello Kurdistan @example.com 19
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
9<br>HELLO<br>Hello Kurdistan<br>@example.com<br>19Explain 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:
بۆ دروستکردنی ، دەنووسیت. چوار بەشی هەیە:
- the
function; - the name of the function (you choose it; see the naming rules);
- parentheses
( )that hold the , separated by commas. They are optional: the parentheses may be empty, but they must be there; - the body: the code that does the work, inside curly braces
{ }.
-
function؛ - ناوی (خۆت هەڵیدەبژێریت؛ بڕوانە یاساکانی ناونان)؛
- کەوانە
( )کە لەخۆ دەگرن و بە کۆما لێک جیا دەکرێنەوە. ئارەزوومەندانەن: لەوانەیە کەوانەکە بەتاڵ بێت، بەڵام دەبێت هەر هەبێت؛ - ناوەڕۆک: ئەو کۆدەی کارەکە دەکات، لەناو کەوانەی لوولی
{ }.
<?php
function functionName($parameter1, $parameter2) {
// the body: code that runs each time the function is called
}
?>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
functionkeyword, the name, and the parameter list in parentheses.function، ناوەکە و لیستی لەناو کەوانەدا.- Lineدێڕی 2-4
The body between
{and}. Note: no after the closing}.ناوەڕۆکەکە لە نێوان
{و}دا. سەرنج: دوای}ی کۆتایی دانانرێت.
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" چاپ دەکات:
<?php
function greet() {
print "Hello<br>";
}
greet();
greet();
greet();
?>Hello Hello Hello
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Hello<br>Hello<br>Hello<br>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2-4
The definition.
printworks likeechohere: it sends text to the page.ناساندنەکە.
printلێرەدا وەکechoکار دەکات: دەق دەنێرێت بۆ لاپەڕەکە.- Lineدێڕی 6-8
Three calls, so the body runs three times.
سێ بانگکردن، بۆیە ناوەڕۆکەکە سێ جار جێبەجێ دەکرێت.
"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.
بەکارهێنانی هەمیشە دوو هەنگاوە. یەکەم، دروستی دەکەیت (دەیناسێنیت). دووەم، بە ناوەکەیەوە بانگی دەکەیت. تەنها ناساندن هیچ شتێکی بینراو ناکات.
<?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();
?>Aram Karim
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:
بۆ سەلماندنی ئەمە، ئەمە لاپەڕەیەکە کە دەناسێنێت بەڵام هەرگیز بانگی ناکات. لاپەڕەیەکی بەتاڵ وەردەگرێت:
<?php
function myname($firstName)
{
echo "my name is " . $firstName;
}
?>Nothing: the page is empty, because the function is never called.
هیچ: لاپەڕەکە بەتاڵە، چونکە هەرگیز بانگ نەکراوە.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2-5
A complete, correct definition of
myname(). But no line callsmyname(...), soechonever 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()oraddFive(). 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,functionorreturn. - 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()(بڕوانە دووبارە ناساندنەوە).
| 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 هەمان جێبەجێ دەکات. بە واتایەکی تر، ناوی نین.
<?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
?>10 10 10
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
10<br>10<br>10Explain 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 دوو جیاوازن:
<?php
$name = "Shilan";
echo $name; // Shilan
echo $Name; // a different variable: it does not exist
?>Shilan Warning: Undefined variable $Name in C:\xampp\htdocs\function_array\index.php on line 4
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 4
$Namewas never given a value, so PHP shows a and prints nothing for it.هیچ نەدراوەتە
$Name، بۆیە PHP پیشان دەدات و هیچ شتێک بۆ ئەو چاپ ناکات.
| Name of…ناوی… | Case-sensitive?؟ | Exampleنموونە |
|---|---|---|
| a | Yesبەڵێ | $name ≠ $Name |
| a | Noنەخێر | addFive() = ADDFIVE() |
| a | Noنەخێر | echo = ECHO, function = FUNCTION |
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:
<?php
/* Defining a PHP Function */
function Print() {
echo “ you are running a function with out arguments";
}
/* Calling a PHP Function */
print();
?>Parse error: syntax error, unexpected token "print", expecting "(" in C:\xampp\htdocs\function_array\index.php on line 3PHP stops before running anything. There are three problems:
PHP پێش جێبەجێکردنی هیچ شتێک ڕادەوەستێت. سێ کێشە هەیە:
- The name
Printis taken.printis a , a reserved word of PHP. Because function names ignore case,Printisprint, so PHP cannot accept it as a new function name. The error message says it found the tokenprintwhere it expected(. - The call
print();would not call your function anyway. It is read as theprintconstruct, which needs something to print. - 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 .
- ناوی
Printگیراوە.print، واتە وشەیەکی پارێزراوی PHP. چونکە ناوی گوێ بە قەبارەی پیت نادەن،Printهەمانprintە، بۆیە PHP وەک ناوی نوێ قبووڵی ناکات. پەیامی دەڵێتprintی دۆزیوەتەوە لە شوێنێکدا کە چاوەڕێی(بووە. - بانگکردنی
print();بە هەر حاڵ تۆ بانگ ناکات. وەک پێکهاتەیprintدەخوێنرێتەوە، کە پێویستی بە شتێکە بۆ چاپکردن. - خوار. بە
“دەست پێدەکات ( خوار کە نووسین دروستی دەکات) و بە"کۆتایی دێت. 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".)
<?php
/* Defining a PHP function */
function printMessage() {
echo "You are running a function without arguments.";
}
/* Calling the function */
printMessage();
?>You are running a function without arguments.
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() لە دێڕی ٥ هاتووە. کەچی هەر کار دەکات:
<?php
echo "my name is ";
myfunction();
function myfunction()
{
echo "Kak Rezan";
}
?>my name is 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دا هەردوو پێش ناساندنیان بانگ دەکات.
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 تەنها ئەو کاتە دروست دەکات کە ئەو بلۆکە بەڕاستی جێبەجێ دەکرێت. بانگکردنی پێش ئەو کاتە :
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!";
}
}
?>Fatal error: Uncaught Error: Call to undefined function sayHi() in C:\xampp\htdocs\function_array\index.php:4
Stack trace:
#0 {main}
thrown in C:\xampp\htdocs\function_array\index.php on line 4Explain 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.
تەنها کاتێک دروست دەکرێت کە ئەم بلۆکە جێبەجێ بکرێت.
<?php
$ready = true;
if ($ready) {
function sayHi() {
echo "Hi!";
}
}
sayHi(); // Works: the if block has already run
?>Hi!
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 بە ڕادەگرێت. هیچ شتێکی دوای ئەو دێڕە جێبەجێ ناکرێت:
<?php
function greet() {
echo "Hi!";
}
gret(); // typing mistake: the function is called greet
?>Fatal error: Uncaught Error: Call to undefined function gret() in C:\xampp\htdocs\function_array\index.php:6
Stack trace:
#0 {main}
thrown in C:\xampp\htdocs\function_array\index.php on line 6If 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 ناوی و بە پەیامێکی تر ڕادەوەستێت:
<?php
function greet() {
echo "Hi!";
}
greet; // the parentheses are missing
?>Fatal error: Uncaught Error: Undefined constant "greet" in C:\xampp\htdocs\function_array\index.php:6
Stack trace:
#0 {main}
thrown in C:\xampp\htdocs\function_array\index.php on line 6Both 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.
بۆ ئەوەی بەسوودتر بێت، دەتوانیت زانیاری بنێریتە ناوی. ئەمە بە دەکەیت. ڕێک وەک وایە: ناوێکی بە $ـەوە هەیە و لەناو وەک هەر تر بەکاری دەهێنیت. دوای ناوی و لەناو کەوانەکەدا دەنووسرێن. هەر چەندێکت بوێت دەتوانیت دایان بنێیت؛ بە کۆما لێکیان جیا بکەرەوە.
<?php
function myfunction($par1, $par2, $par3)
{
echo "This function received: $par1, $par2 and $par3";
}
myfunction("tea", "bread", "cheese");
?>This function received: tea, bread and 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"). ئەمە جیاوازییە وردەکەیە:
| 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ئەو میوەیەی دەیخەیتە ناوی |
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ـدا.
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 پڕ دەکاتەوە.
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. هەر بانگکردنێک ناوێکی جیاواز دەنێرێت، بۆیە هەمان هەر جارە دێڕێکی جیاواز چاپ دەکات:
<?php
function myname($firstName)
{
echo "my name is " . $firstName . "!<br />";
}
myname("Aram");
myname("Shilan");
myname("Karwan");
myname("Dilan");
?>my name is Aram! my name is Shilan! my name is Karwan! my name is Dilan!
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
my name is Aram!<br />my name is Shilan!<br />my name is Karwan!<br />my name is Dilan!<br />Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
$firstNameis the parameter: an empty box waiting for a value.$firstName: سندوقێکی بەتاڵ کە چاوەڕێی دەکات.- Lineدێڕی 4
Inside the body,
$firstNameis 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 بەپێی شوێن پێکیان دەگەیەنێت: یەکەم دەچێتە ناو یەکەم و دووەم دەچێتە ناو دووەم.
<?php
function myname($firstName, $lastName)
{
echo "Hello there " . $firstName . " " . $lastName . "!<br />";
}
myname("Aram", "Ali");
myname("Shilan", "Omar");
myname("Rezan", "Mahmood");
myname("Hana", " Bakr");
?>Hello there Aram Ali! Hello there Shilan Omar! Hello there Rezan Mahmood! Hello there Hana Bakr!
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Hello there Aram Ali!<br />Hello there Shilan Omar!<br />Hello there Rezan Mahmood!<br />Hello there Hana Bakr!<br />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);
?>first data is 20 second data is 30 your data is 50
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
first data is 20 <br>second data is 30 <br>your data is 50Explain 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ـدا بە چەند ") بە دوو دەناسێنێت. کۆ، (%) و ئەنجامی دابەشکردنی (/) دوو ژمارە چاپ دەکات. لە پەڕەی تاقیگەکەدا بەم شێوەیەیە:
<?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);
?>Parse error: syntax error, unexpected identifier "of", expecting "," or ";" in C:\xampp\htdocs\function_array\index.php on line 7
There are three problems, and PHP shows them one at a time:
سێ کێشە هەیە و PHP یەک لە دوای یەک پیشانیان دەدات:
- Curly quotes (lines 7 and 8). Two
echolines open their strings with“. PHP does not see a string at all: it reads“modas some unknown name and then stops at the wordof. Fix: straight quotes". - The wrong function is called (line 10). The function is defined as
calculate_Function, but the code callsaddFunction(20, 10). Once the quotes are fixed, PHP stops with this : - No separators. The three
echolines print no space and no<br>, so the three results run together on one line.
- خوار (دێڕی ٧ و ٨). دوو دێڕی
echoبە“دەکەنەوە. PHP هیچ نابینێت:“modوەک ناوێکی نەناسراو دەخوێنێتەوە و پاشان لە وشەیofدا ڕادەوەستێت. چارەسەر: ڕاست". - هەڵە بانگ کراوە (دێڕی ١٠). بە ناوی
calculate_Functionناسێنراوە، بەڵام کۆدەکەaddFunction(20, 10)بانگ دەکات. کاتێک چاک دەکرێنەوە، PHP بە ڕادەوەستێت: - هیچ جیاکەرەوەیەک نییە. سێ دێڕەکەی
echoنە چاپ دەکەن نە<br>، بۆیە سێ ئەنجامەکە بە یەکەوە لە یەک دێڕدا دەردەکەون.
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 10Parameters 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> کۆتایی پێدەهێنێت و دوو جار بانگ دەکات بۆ ئەوەی و ئەنجامێکی کەرتی پیشان بدات:
<?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);
?>Sum of the two numbers is : 30 mod of the two numbers is : 0 div of the two numbers is : 2 Sum of the two numbers is : 9 mod of the two numbers is : 1 div of the two numbers is : 3.5
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Sum of the two numbers is : 30<br>mod of the two numbers is : 0<br>div of the two numbers is : 2<br>Sum of the two numbers is : 9<br>mod of the two numbers is : 1<br>div of the two numbers is : 3.5<br>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> چاپی دەکات:
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ڕادەگرن.
<?php
function myname($firstName, $lastName)
{
echo "Hello there " . $firstName . " " . $lastName . "!<br />";
}
myname("Aram", "Ali", "extra"); // extra argument: ignored
myname("Aram"); // missing argument: error
?>Hello there Aram Ali!
Fatal error: Uncaught ArgumentCountError: Too few arguments to function myname(), 1 passed in C:\xampp\htdocs\function_array\index.php on line 8 and exactly 2 expected in C:\xampp\htdocs\function_array\index.php:2
Stack trace:
#0 C:\xampp\htdocs\function_array\index.php(8): myname('Aram')
#1 {main}
thrown in C:\xampp\htdocs\function_array\index.php on line 2Explain 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). ئەگەر هەندێک جار نەنێردرێت، بدەرێ، وەک لە بەشی داهاتوودا دەبینیت.
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() بە نرخێکی بنەڕەتیی 50index.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات<?php
function setHeight($minheight = 50) {
echo "The height is : $minheight <br>";
}
setHeight(350);
setHeight();
setHeight(135);
setHeight(80);
?>The height is : 350 The height is : 50 The height is : 135 The height is : 80
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
The height is : 350 <br>The height is : 50 <br>The height is : 135 <br>The height is : 80 <br>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
$minheight = 50: the default value.$minheight = 50: .- Lineدێڕی 3
$minheightinside double quotes is replaced by its value ().$minheightلەناو جووتەگوتەدا بە دەگۆڕدرێت ().- Lineدێڕی 7
No argument, so
$minheightis 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 لەمە ئاگادارت دەکاتەوە:
<?php
function greetUser($greeting = "Hello", $name) {
echo "$greeting, $name!";
}
greetUser("Hi", "Dilan");
?>Deprecated: greetUser(): Optional parameter $greeting declared before required parameter $name is implicitly treated as a required parameter in C:\xampp\htdocs\function_array\index.php on line 2 Hi, Dilan!
<?php
function greetUser($name, $greeting = "Hello") {
echo "$greeting, $name!<br>";
}
greetUser("Dilan"); // uses the default greeting
greetUser("Soran", "Hi"); // replaces the default
?>Hello, Dilan! Hi, Soran!
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Hello, Dilan!<br>Hi, Soran!<br>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. لە پەڕەی تاقیگەکەدا، {ێکی تەنها دوای هەردوو بانگکردنەکە هاتووە:
<?php
function printMe($param = NULL) {
print $param; }
printMe("This is test");
printMe();
{
?>Parse error: Unclosed '{' on line 6 in C:\xampp\htdocs\function_array\index.php on line 7Every { 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دا شاراوەتەوە، کە دروستە بەڵام بە ئاسانی نابینرێت.
<?php
function printMe($param = NULL) {
print $param;
}
printMe("This is test");
printMe(); // uses the default NULL, so nothing is printed
?>This is test
Only the first call prints something.
تەنها بانگکردنی یەکەم شتێک چاپ دەکات.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
$param = NULL: if no argument comes,$paramisNULL.$param = NULL: ئەگەر هیچ نەیەت،$paramدەبێتەNULL.- Lineدێڕی 3
printworks likeechohere.printلێرەدا وەکechoکار دەکات.- Lineدێڕی 7
No argument:
NULLis 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 ئەم کارە دەکات. دەبێتە خودی بانگکردنەکە، ڕێک لەو شوێنەی بانگکردنەکەی تێدایە لە کۆدەکەتدا.
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 هەروەها یەکسەر کۆتایی پێ دەهێنێت، بۆیە کۆدی دوای ئەو هەرگیز جێبەجێ ناکرێت.
<?php
function greeting() {
return "Hello";
}
print greeting() . " Glenn<br>";
print greeting() . " Sally<br>";
?>Hello Glenn Hello Sally
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Hello Glenn<br>Hello 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ـدا کە دەگەڕێننەوە") سەرەتا لە هەڵدەگرێت، پاشان چاپی دەکات:
<?php
function addFunction($num1, $num2) {
$sum = $num1 + $num2;
return $sum;
}
$return_value = addFunction(10, 20);
echo "Returned value from the function : $return_value";
?>Returned value from the function : 30
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);
?>The sum is 50 Double the sum is 100 Sum of three numbers: 6
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
The sum is 50<br>Double the sum is 100<br>Sum of three numbers: 6Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 4
The sum is returned, not printed.
کۆکە دەگەڕێنرێتەوە، چاپ ناکرێت.
- Lineدێڕی 7
Store the result.
ئەنجامەکە هەڵبگرە.
- Lineدێڕی 9
Calculate with it:
add(20, 30) * 2is 100.هەژماری پێ بکە:
add(20, 30) * 2یەکسانە بە 100.- Lineدێڕی 10
Nest calls: the inner
add(1, 2)gives 3, thenadd(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 ()، کە واتای هیچ نییە دەگەیەنێت:
return means NULLبێ return واتە NULLindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات<?php
function sayHello() {
echo "Hello! ";
}
$result = sayHello();
var_dump($result);
?>Hello! NULL
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 6
sayHello()prints "Hello! " but returns nothing, so$resultisNULL.sayHello()"Hello! " چاپ دەکات بەڵام هیچ ناگەڕێنێتەوە، بۆیە$resultدەبێتەNULL.- Lineدێڕی 7
var_dump()shows the type and value of a variable.var_dump()جۆر و پیشان دەدات.
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 لە هەمان هەرگیز جێبەجێ ناکرێت:
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;
?>Hey a variable
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Hey<br>a variableExplain 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
$xand a functionx()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()ی وانەکە ئەمە بەکاردەهێنێت بۆ سڵاوکردن بە زمانی جیاواز:
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";
?>Hola Glenn Bonjour Sally Hello Aram Hello Hana
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Hola Glenn<br>Bonjour Sally<br>Hello Aram<br>Hello HanaExplain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
If
$langis'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
ifmatched.بژاردەی کۆتایی: تەنها کاتێک دەگاتە ئێرە کە هیچ
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': بەراوردکردنی بە==.
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() و ! بڕوانە وانەی مەرجەکان).
<?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);
?>One of the two numbers is not set yet. Summation of 1 and 1 is 2
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
One of the two numbers is not set yet.<br>Summation of 1 and 1 is 2Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
!isset($num1)is true when$num1isNULL.||means or.!isset($num1)ڕاستە کاتێک$num1یەکسان بێت بەNULL.||واتە یان.- Lineدێڕی 6
The sum is printed right here, inside the function.
کۆکە هەر لێرە، لەناو چاپ دەکرێت.
- Lineدێڕی 10
nullis passed on purpose, so the message is printed.nullبە ئەنقەست دەنێردرێت، بۆیە پەیامەکە چاپ دەکرێت.
<?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);
?>Summation 1: 1 + 1 = 2 Summation 2: One of the two numbers is not set yet.
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Summation 1: 1 + 1 = 2<br />Summation 2: One of the two numbers is not set yet.Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 4, 6
returninstead ofecho: 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 />بە دەوری هەر ئەنجامێکدا زیاد دەکات.
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 function | the 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);:
- 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 ڕادەوەستێت. - Copy the arguments into the parametersکۆپیکردنی ئارگیومێنتەکان بۆ ناو پارامیتەرەکان
20 goes into
$num1and 30 into$num2, by position. A missing argument takes its default value; if there is none, PHP stops with anArgumentCountError.20 دەچێتە ناو
$num1و 30 دەچێتە ناو$num2، بەپێی شوێن. کە نەنێردرابێت خۆی وەردەگرێت؛ ئەگەر نەیبێت، PHP بەArgumentCountErrorڕادەوەستێت. - Run the bodyجێبەجێکردنی ناوەڕۆکەکە
The statements inside
{ }run from the top. The calling code waits. Variables made here, like$sum, are local.ناو
{ }یەک لە دوای یەک لە سەرەوە جێبەجێ دەکرێن. کۆدی بانگکەر چاوەڕێ دەکات. ئەو لێرە دروست دەکرێن، وەک$sum، ناوخۆیین. - Returnگەڕانەوە
return $sum;ends the function and sends 50 back. (Withoutreturn, the function ends at its closing}and gives backNULL.)return $sum;کۆتایی بە دەهێنێت و 50 دەنێرێتەوە. (بەبێreturn، لە}ی کۆتاییدا تەواو دەبێت وNULLدەگەڕێنێتەوە.) - Continue where you left offبەردەوامبوون لەو شوێنەی وەستابوو
The call
add(20, 30)is replaced by 50, so$totalbecomes 50, and the page continues with the next line.بانگکردنی
add(20, 30)دەبێتە 50، بۆیە$totalدەبێتە 50 و لاپەڕەکە لە دێڕی دواترەوە بەردەوام دەبێت. - Forget the local variablesلەبیرکردنی گۆڕاوە ناوخۆییەکان
$num1,$num2and$sumdisappear. 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ـیە: . هەندێک جار دەتەوێت خودی بانگکەر بگۆڕێت. ئەو کاتە بەکاردەهێنیت، کە بە & نیشانە دەکرێت. وێنەکە جیاوازییەکە پیشان دەدات:
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.
لە هیچ شتێک بەڕاستی نادرێت بە کەس: بانگکەر و تەنها دوو ناو بۆ یەک بەکاردەهێنن.
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 یان ، چونکە وەردەگرێت، نەک خودی .
<?php
function double($alias) {
$alias = $alias * 2;
return $alias;
}
$val = 10;
$dval = double($val);
echo "Value = $val Doubled = $dval";
?>Value = 10 Doubled = 20
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
$aliasreceives a copy of 10.$aliasکۆپییەکی 10 وەردەگرێت.- Lineدێڕی 3
Only the copy becomes 20.
تەنها کۆپییەکە دەبێتە 20.
- Lineدێڕی 8
$dvalgets the returned 20.$dvalژمارە 20ی گەڕاوە وەردەگرێت.- Lineدێڕی 9
$valis still 10.$valهێشتا 10ـە.
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ـە، یان .
<?php
function triple(&$realthing) {
$realthing = $realthing * 3;
}
$val = 10;
triple($val);
echo "Triple = $val";
?>Triple = 30
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
&$realthing: a reference to the caller's variable, not a copy.&$realthing: بۆ بانگکەر، نەک کۆپی.- Lineدێڕی 3
This changes
$valitself.ئەمە خودی
$valدەگۆڕێت.- Lineدێڕی 7
No
returnand no assignment needed:$valis 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() بە . پەڕەی تاقیگەکە تەنها هەردوو ناساندنەکە پیشان دەدات، بێ هیچ بانگکردنێک، بۆیە جیاوازییەکە نابینیت (شێوەی دانانی کۆدەکەش لە پەڕەکەدا تێکچووە). لێرەدا بانگکردنمان بۆ زیاد کردوون:
<?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>";
?>After addFive: 10 After addSix: 16
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
After addFive: 10<br>After addSix: 16<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
$orignumstays 10.$orignumهەر 10 دەمێنێتەوە.- Lineدێڕی 14
$orignumbecomes 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";
?>Parse error: syntax error, unexpected token "&" in C:\xampp\htdocs\function_array\index.php on line 7
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 نییە؛ هیچ شتێک نییە بیگۆڕێت. بۆیە ناردنی ژمارەیەکی ڕاستەوخۆ بۆ کە بە وەردەگرێت :
<?php
function addFive($num) {
return $num += 5;
}
function addSix(&$num) {
return $num += 6;
}
echo addsix(5); // Fatal Error
?>Fatal error: Uncaught Error: addSix(): Argument #1 ($num) could not be passed by reference in C:\xampp\htdocs\function_array\index.php:10
Stack trace:
#0 {main}
thrown in C:\xampp\htdocs\function_array\index.php on line 10Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 6-8
addSix()wants a variable it can change.addSix()دەوێت کە بتوانێت بیگۆڕێت.- Lineدێڕی 10
5is a literal, so PHP stops. (Note alsoaddsixforaddSix: names ignore case.)5ڕاستەوخۆیە، بۆیە PHP ڕادەوەستێت. (هەروەها سەرنج بدەaddsixلە جیاتیaddSix: ناوەکان گوێ بە قەبارەی پیت نادەن.)
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 بە تێبینییەک ڕێگەی پێدەدات و لەسەر کۆپییەکی کاتی کار دەکات:
<?php
function addFive($num) {
return $num += 5;
}
function addSix(&$num) {
return $num += 6;
}
echo addsix(ADDFive(5));
?>Notice: Only variables should be passed by reference in C:\xampp\htdocs\function_array\index.php on line 10 16
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:
سەرەتا ئەنجامەکە لە هەڵبگرە. ئەو کاتە ڕاستەقینەی هەیە بۆ کارکردن لەسەری و دەبینیت کە گۆڕاوە:
<?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
?>16 16
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
16<br>16One 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
?>Fatal error: Cannot redeclare function A() (previously declared in C:\xampp\htdocs\function_array\index.php:2) in C:\xampp\htdocs\function_array\index.php on line 3
Stack trace:
#0 {main}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;
}
?>Fatal error: Cannot redeclare function strlen() in C:\xampp\htdocs\function_array\index.php on line 2
Stack trace:
#0 {main}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.
هەر جیهانێکی بچووکی تایبەت بە خۆی هەیە بۆ ، کە پێی دەگوترێت . ئەو لەناو دروست دەکرێت : تەنها لە کاتی جێبەجێبوونی هەیە و بەشەکانی تری لاپەڕەکە نایبینن. .
<?php
function makeTea() {
$cups = 3; // a local variable
echo "Making $cups cups of tea.<br>";
}
makeTea();
echo "Cups outside: $cups";
?>Making 3 cups of tea. Warning: Undefined variable $cups in C:\xampp\htdocs\function_array\index.php on line 8 Cups outside:
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Making 3 cups of tea.<br>
Warning: Undefined variable $cups in C:\xampp\htdocs\function_array\index.php on line 8
Cups outside:Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
$cupsis created insidemakeTea().$cupsلەناوmakeTea()دا دروست دەکرێت.- Lineدێڕی 8
Outside the function
$cupsdoes 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:
پێچەوانەکەشی ڕاستە. ئەو لە دەرەوەی هەموو دروست دەکرێت لە دەژیت و لەناو نابینرێت:
<?php
$city = "Sulaymaniyah";
function showCity() {
echo "City: $city"; // $city is not visible here
}
showCity();
?>Warning: Undefined variable $city in C:\xampp\htdocs\function_array\index.php on line 5 City:
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:
چارەسەرە پاکەکە ئەوەیە کە وەک بنێریتە ناوەوە:
<?php
$city = "Sulaymaniyah";
function showCity($place) {
echo "City: $place";
}
showCity($city);
?>City: Sulaymaniyah
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 بەکاردەهێنێت:
<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>Even numbers: 2, 4, 6, 8, 10, Odd numbers: 1, 3, 5, 7, 9,
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<html>
<body>
Even numbers: 2, 4, 6, 8, 10, <br />Odd numbers: 1, 3, 5, 7, 9, </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$counthere 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ـە.
<?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";
?>2, 4, 6, 8, 10, After the call, $count is 11
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
2, 4, 6, 8, 10, <br>After the call, $count is 11Common 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 دروستی کردوون.
| 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 6 | Match 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 reference | Pass a variable بنێرە |
| Using an outside variable inside a functionبەکارهێنانی دەرەوە لەناو | Warning: Undefined variable $city | Pass it in as an argumentوەک بینێرە ناوەوە |
Code placed after returnکۆد لە دوای return | No 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") داوات لێدەکات نموونەکانی بنووسیت و جێبەجێیان بکەیت. لە خوارەوە ئەرکەکانی تاقیگەکە، کاتی تاقیگەکە و چەند ڕاهێنانێکی نوێ هەن. پێش کردنەوەی چارەسەرەکە، هەر یەکێکیان خۆت تاقی بکەرەوە.
- Start the serverدەستپێکردنی ڕاژەکارەکە
Open the Control Panel and start Apache (see XAMPP).
بکەرەوە و Apache دەست پێبکە (بڕوانە وانەی XAMPP).
- Make a project folderدروستکردنی فۆڵدەرێک بۆ پڕۆژەکە
Inside , create a called
function_array, for exampleC:\xampp\htdocs\function_array.لەناو بە ناوی
function_arrayدروست بکە، بۆ نموونەC:\xampp\htdocs\function_array. - Create
index.phpدروستکردنیindex.phpCreate the file
index.phpin 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 بکاتەوە؛ متمانە بە بکە بۆ ئەوەی هەموو توانستەکانی چالاک بن.) - Add the PHP tagsزیادکردنی تاگەکانی PHP
The lab snippets have no
<?php ... ?>tags. Write each example between them.پارچە کۆدەکانی تاقیگەکە
<?php ... ?>ـیان تێدا نییە. هەر نموونەیەک لە نێوانیاندا بنووسە. - 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تاقیگەی ٤: نموونەکانی کردار
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وەڵامەکە پیشان بدە
<?php
/* Defining a PHP function */
function printMessage() {
echo "You are running a function without arguments.";
}
/* Calling the function */
printMessage();
?>You are running a function without arguments.
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 ناتوانرێت وەک ناوی نوێ بەکاربهێنرێت. بڕوانە چاککردنی ١.
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وەڵامەکە پیشان بدە
<?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>";
?>After addFive: 10 After addSix: 16
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
After addFive: 10<br>After addSix: 16<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.
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وەڵامەکە پیشان بدە
<?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);
?>Sum of the two numbers is : 30 mod of the two numbers is : 0 div of the two numbers is : 2 Sum of the two numbers is : 9 mod of the two numbers is : 1 div of the two numbers is : 3.5
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Sum of the two numbers is : 30<br>mod of the two numbers is : 0<br>div of the two numbers is : 2<br>Sum of the two numbers is : 9<br>mod of the two numbers is : 1<br>div of the two numbers is : 3.5<br>Call the function by the name you defined. The lab sheet calls addFunction(20, 10) by mistake; see Lab fix 2.
بەو ناوە بانگ بکە کە ناساندووتە. پەڕەی تاقیگەکە بە هەڵە addFunction(20, 10) بانگ دەکات؛ بڕوانە چاککردنی ٢.
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وەڵامەکە پیشان بدە
<?php
function addFunction($num1, $num2) {
$sum = $num1 + $num2;
return $sum;
}
$return_value = addFunction(10, 20);
echo "Returned value from the function : $return_value";
?>Returned value from the function : 30
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وەڵامەکە پیشان بدە
<?php
function printMe($param = NULL) {
print $param;
}
printMe("This is test");
printMe(); // uses the default NULL, so nothing is printed
?>This is test
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بەرنامەکانی کاتی تاقیگە
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 باز دەدات، بۆیە هیچ پێویستی بە تاقیکردنەوەی % نییە.
<?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);
?>Even numbers: 2, 4, 6, 8, 10 Odd numbers: 1, 3, 5, 7, 9
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Even numbers: 2, 4, 6, 8, 10<br>Odd numbers: 1, 3, 5, 7, 9Explain 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.
باز بدە بۆ ژمارەی جووتی (یان تاکی) دواتر.
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وەڵامەکە پیشان بدە
<?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);
?>One of the two numbers is not set yet. Summation of 1 and 1 is 2
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
One of the two numbers is not set yet.<br>Summation of 1 and 1 is 2<?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);
?>Summation 1: 1 + 1 = 2 Summation 2: One of the two numbers is not set yet.
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Summation 1: 1 + 1 = 2<br />Summation 2: One of the two numbers is not set yet.See Echo or return? for why the second version is more flexible, and for the parentheses around $num1 + $num2.
بۆ ئەوەی بزانیت بۆچی وەشانی دووەم نەرمترە و بۆ کەوانەکانی دەوری $num1 + $num2، بڕوانە echo یان return؟.
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وەڵامەکە پیشان بدە
<?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);
?>Row num: 0 • Col num (0): 0 • Col num (1): 3 • Col num (2): 6 Row num: 1 • Col num (0): 5 • Col num (1): 7 • Col num (2): 9
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<p>Row num: 0</p><ul><li>Col num (0): 0</li><li>Col num (1): 3</li><li>Col num (2): 6</li></ul><p>Row num: 1</p><ul><li>Col num (0): 5</li><li>Col num (1): 7</li><li>Col num (2): 9</li></ul>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ڕاهێنانی زیاتر
add($a, $b)کردارێک بە ناوی add($a, $b)Write add($a, $b) and call it three times with different numbers.
add($a, $b) بنووسە و سێ جار بە ژمارەی جیاواز بانگی بکە.
Show answerوەڵامەکە پیشان بدە
<?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);
?>5 + 3 = 8 20 + 30 = 50 -4 + 1.5 = -2.5
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
5 + 3 = 8<br>20 + 30 = 50<br>-4 + 1.5 = -2.5One definition, three calls. Because add() returns the sum, each call can be joined into a sentence with ..
یەک ناساندن، سێ بانگکردن. چونکە add() کۆکە دەگەڕێنێتەوە، هەر بانگکردنێک دەتوانرێت بە . بلکێنرێت بە ڕستەیەکەوە.
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وەڵامەکە پیشان بدە
<?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>";
}
}
?>1 is odd 2 is even 3 is odd 4 is even
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
1 is odd<br>2 is even<br>3 is odd<br>4 is even<br>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
The comparison itself is
trueorfalse, so it can be returned directly.بەراوردکردنەکە خۆی
trueیانfalseـە، بۆیە ڕاستەوخۆ دەگەڕێنرێتەوە.
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وەڵامەکە پیشان بدە
<?php
function rectangleArea($width, $height = 1) {
return $width * $height;
}
echo rectangleArea(4, 5) . "<br>";
echo rectangleArea(7);
?>20 7
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
20<br>7This 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! چاپ بکات.
<?php
function Greet($name) {
return "Hello, $name!";
echo "Welcome!";
}
function greet($name) {
echo "Hi";
}
echo greet();
?>Fatal error: Cannot redeclare function greet() (previously declared in C:\xampp\htdocs\function_array\index.php:2) in C:\xampp\htdocs\function_array\index.php on line 7
Stack trace:
#0 {main}Show answerوەڵامەکە پیشان بدە
greet()is defined twice (Greetandgreetare the same name): Cannot redeclare function greet().echo "Welcome!";comes afterreturn, so it would never run.greet()is called without the required argument: anArgumentCountError.
greet()دوو جار ناسێنراوە (Greetوgreetهەمان ناون): Cannot redeclare function greet().echo "Welcome!";دوایreturnهاتووە، بۆیە هەرگیز جێبەجێ ناکرێت.greet()بێ پێویستەکە بانگ کراوە:ArgumentCountError.
<?php
function greet($name) {
echo "Welcome!<br>";
return "Hello, $name!";
}
echo greet("Karwan");
?>Welcome! Hello, Karwan!
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Welcome!<br>Hello, Karwan!Lab 4 and exercisesتاقیگەی ٤ و ڕاهێنانەکان
Practice: values and referencesڕاهێنان: نرخ و ئاماژە
Without running it, what does this code print? Explain each of the three numbers.
بێ جێبەجێکردن، ئەم کۆدە چی چاپ دەکات؟ هەر سێ ژمارەکە ڕوون بکەرەوە.
<?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وەڵامەکە پیشان بدە
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.
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وەڵامەکە پیشان بدە
<?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";
?>Aram: 85 Shilan: 80
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Aram: 85<br>Shilan: 80Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
&$scorechanges the caller's variable;$bonusis optional.&$scoreبانگکەر دەگۆڕێت؛$bonusئارەزوومەندانەیە.
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وەڵامەکە پیشان بدە
<?php
function swap(&$a, &$b) {
$temp = $a;
$a = $b;
$b = $temp;
}
$first = "tea";
$second = "coffee";
swap($first, $second);
echo "first: $first, second: $second";
?>first: coffee, second: tea
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 withname(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. returnsends a value back and ends the function; code after it never runs. NoreturnmeansNULL. 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بەکاربهێنیت، کە باشترە لێی دوور بکەویتەوە).
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.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.
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زاراوە سەرەکییەکان
Arrays in PHPڕیزەکەکان لە PHPـدا
Related lecturesوانە پەیوەندیدارەکان
References and further readingسەرچاوەکان و خوێندنەوەی زیاتر
This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.