Chapterبەش 5 · Lectureوانە 5.6
Constants and variable scopeنەگۆڕەکان و مەودای گۆڕاوەکان
Constants hold values that never change, and they are visible everywhere. Variables are different: their scope decides where they can be used. Learn define() and const, local and global scope, the global keyword and static variables, with real outputs from PHP 8.
ئەو هەڵدەگرن کە هەرگیز ناگۆڕێن و لە هەموو شوێنێک دیارن. جیاوازن: دیاری دەکات لە کوێ بەکاربێن. فێری define() و const، و ، global و بە، لەگەڵ ڕاستەقینەی PHP 8.
What you will learnچی فێر دەبیت
- Create constants with
define()andconst, and use them correctly. - Explain why case-insensitive constants no longer work in PHP 8.
- Tell local scope from global scope and predict "Undefined variable" warnings.
- Use the
globalkeyword correctly. - Use static variables to remember values between function calls.
- بە
define()وconstدروست بکەیت و بە دروستی بەکاریان بهێنیت. - ڕوون بکەیتەوە بۆچی لە PHP 8ـدا ناتوانیت دروست بکەیت کە نەبێت.
- لە جیا بکەیتەوە و «Undefined variable» پێشبینی بکەیت.
-
globalبە دروستی بەکاربهێنیت. - بەکاربهێنیت بۆ لەبیرمانی لە نێوان .
Two ideas in this lectureدوو بیرۆکە لەم وانەیەدا
This lecture answers two questions. First: how do you store a value that must never change, such as the value of π or the name of your website? You use a . Second: where in a script can a variable be used? That is its , and it matters as soon as you write functions.
ئەم وانەیە وەڵامی دوو پرسیار دەداتەوە. یەکەم: چۆن هەڵدەگریت کە هەرگیز نەگۆڕێت، وەک π یان ناوی ؟ بەکاردەهێنیت. دووەم: لە کوێی بەکاردێت؟ ئەمە و هەر کە بنووسیت گرنگ دەبێت.
Functions in one minute
کردارەکان لە یەک خولەکدا
Scope only makes sense with functions, which you will study fully in Functions. For now you need just this: a is a named block of code that you write once and run whenever you need it. function name() { … } defines it, and name(); runs (calls) it.
تەنها لەگەڵ واتای هەیە، کە بە تەواوی لە وانەی کردارەکان دەیانخوێنیت. ئێستا تەنها ئەمەت پێویستە: ناونراوە کە یەک جار دەینووسیت و هەر کاتێک پێویستت بوو جێبەجێی دەکەیت. function name() { … } دەیناسێنێت و name(); جێبەجێی دەکات (بانگی دەکات).
<?php
function sayHello() { // define the function
echo "Hello!<br>";
}
sayHello(); // call it: the code inside runs
sayHello(); // call it again
?>Hello! Hello!
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Hello!<br>Hello!<br>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2-4
The : nothing runs yet.
: هێشتا هیچ جێبەجێ نابێت.
- Lineدێڕی 6-7
Each runs the code inside the braces.
هەر کۆدی ناو کەوانەکان جێبەجێ دەکات.
Constantsنەگۆڕەکان
If you want a value that is set once and then never changes, what you want is called a . The course summary describes it as "a container that contains an unchanging value".
ئەگەر دەوێت کە یەک جار دابنرێت و ئیتر هەرگیز نەگۆڕێت، ئەوەی دەتەوێت پێی دەوترێت . کورتەی کۆرسەکە بەمە وەسفی دەکات: «لەخۆگرێک کە نەگۆڕی تێدایە».
A name for a value that must not change while the program runs. PHP makes one with define() or const, Java with final; Python has no real constants, so programmers write the name in capitals, such as PI = 3.14159, as a promise not to change it.
ناوێکە بۆ کە نابێت لە کاتی کارکردنی بگۆڕێت. PHP بە define() یان const دروستی دەکات و Java بە final؛ Python نەگۆڕی ڕاستەقینەی نییە، بۆیە ناوەکە بە پیتی گەورە دەنووسن، وەک PI = 3.14159، وەک بەڵێنێک کە نایگۆڕن.
Think of the official price board on the wall of a bakery, stamped with the municipality's seal. Everyone in the bazaar can read it, from any shop, but nobody can change the prices on it during the day. A constant is like that: a name with a value that anyone in the script can read, and nobody can change.
تابلۆی فەرمیی نرخەکان لەسەر دیواری نانەواخانەیەک بهێنە پێش چاوت، کە مۆری شارەوانیی لێدراوە. هەموو کەس لە بازاڕدا دەتوانێت بیخوێنێتەوە، لە هەر دوکانێکەوە بێت، بەڵام کەس ناتوانێت لە ماوەی ڕۆژدا نرخەکانی سەری بگۆڕێت. بەم شێوەیەیە: ناوێک بە کە هەموو بەشێکی دەتوانێت بیخوێنێتەوە و کەس ناتوانێت بیگۆڕێت.
Where the analogy stops workingسنووری ئەم نموونەیە
A real sign is there before you arrive. A constant only exists after the line that defines it has run, so you cannot use it earlier in the script. And it is fixed only for one run: the next time the script runs, it is created again (and could get a different value).
تابلۆیەکی ڕاستەقینە پێش هاتنی تۆ لەوێیە. بەڵام تەنها دوای جێبەجێبوونی ئەو دێڕەی دەیناسێنێت هەیە، بۆیە ناتوانیت پێشتر لە بەکاری بهێنیت. هەروەها تەنها بۆ یەک جار کارکردن جێگیرە: جاری داهاتوو کە کار دەکات، دووبارە دروست دەکرێتەوە (و دەکرێت جیاواز وەربگرێت).
- Constants do not start with a dollar sign
$. - By convention, constant names are written in all capital letters, such as
PIorTAX_RATE. - The rule for creating one:
define(name, value);The name is a string in quotes. - Constant names are case-sensitive.
- Constants are automatically global: they can be used anywhere in the script, even inside functions.
- Once defined, a constant cannot be changed or defined again.
- بە هێمای دۆلار
$دەست پێناکەن. - بە نەریت، ناوی هەمووی بە پیتی گەورە دەنووسرێت، وەک
PIیانTAX_RATE. - یاسای دروستکردنی:
define(name, value);ناوەکە لەناو . - ناوی .
- خۆکارانە گشتین: لە هەر شوێنێکی بەکاردێن، تەنانەت لەناو .
- کاتێک ناسێنرا، ناگۆڕدرێت و ناتوانرێت دووبارە بناسێنرێتەوە.
<?php
define('PI', 3.14159);
echo PI;
?>3.14159
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
define()creates the constantPIwith the value 3.14159.define()PIبە 3.14159 دروست دەکات.- Lineدێڕی 3
Use the name with no
$and no quotes.ناوەکە بێ
$و بێ بەکاربهێنە.
Learn to make constants with define() and const, how to name, check and print them, and when to use a constant.
فێر دەبیت چۆن بە define() و const دروست بکەیت، چۆن ناویان لێ بنێیت، بیانپشکنیت و چاپیان بکەیت و کەی بەکاربهێنیت.
Watch to 5:38. The later part (magic constants, variable variables) is not in this lesson.
تا 5:38 سەیر بکە. بەشەکەی دواتر (magic constants و variable variables) لەم وانەیەدا نییە.
Constantsنەگۆڕەکان
The const keywordوشە کلیلەییەکەی const
There is a second way to create a constant: the const keyword. It is shorter and works at the top level of a file (and inside classes). define() is an ordinary function call, so you can also use it in places such as an if block. Both create the same kind of constant.
ڕێگایەکی دووەم بۆ دروستکردنی هەیە: const. کورتترە و لە ئاستی سەرەوەی (و لەناو ) کار دەکات. define() ئاساییە، بۆیە دەتوانیت لە شوێنی وەک بلۆکی ifیش بەکاری بهێنیت. هەردووکیان هەمان جۆری دروست دەکەن.
constconst.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات<?php
const PI = 3.14159;
const SITE_NAME = "CêIT";
echo PI . "<br>" . SITE_NAME;
?>3.14159 CêIT
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
3.14159<br>CêITConstantsنەگۆڕەکان
Using a constant: no $ and no quotesبەکارهێنانی نەگۆڕێک: بێ $ و بێ نیشانەی گوتە
To read a constant, write its name without $ and without quotes. With quotes it is just a piece of text, and with $ it is a (different, undefined) variable. This student note shows the first trap: the constant w4 exists, but echo "w4"; prints the two characters w4.
بۆ خوێندنەوەی ، ناوەکەی بەبێ $ و بەبێ بنووسە. لەگەڵ تەنها پارچە دەقێکە و لەگەڵ $ (جیاواز و نەناسراو). ئەم تێبینییەی خوێندکارێک یەکەم تەڵە پیشان دەدات: w4 هەیە، بەڵام echo "w4"; دوو w4 دەنووسێت.
<?php
define("w4", 3.14);
echo "w4";
//Output: w4
?>w4
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
Creates the constant
w4with the value 3.14.w4بە 3.14 دروست دەکات.- Lineدێڕی 3
"w4"is a string, soechoprints the text w4."w4"، بۆیەechoدەقی w4 دەنووسێت.
<?php
define("w4", 3.14);
echo "w4"; // in quotes: the text w4
echo "<br>";
echo w4; // no quotes, no $: the constant
?>w4 3.14
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
w4<br>3.14$ دەبێتە گۆڕاوdollar.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات<?php
define("MAX_USERS", 50);
echo $MAX_USERS;
?>Warning: Undefined variable $MAX_USERS in C:\xampp\htdocs\dollar.php on line 3
The note also names its constant in lowercase (w4). That works, but the convention is capital letters, so that constants stand out from variables.
تێبینییەکە ناوی بە پیتی بچووک نووسیوە (w4). ئەمە کار دەکات، بەڵام نەریت پیتی گەورەیە، تاکو لە جیا بکرێنەوە.
Constantsنەگۆڕەکان
Constant names are case-sensitiveناوی نەگۆڕەکان هەستیار بە قەبارەی پیتن
Like variable names, constant names are case-sensitive: Cons and cons are two different names. Using a constant that does not exist is a fatal error in PHP 8: the script stops.
وەک ناوی ، ناوی : Cons و cons دوو ناوی جیاوازن. بەکارهێنانی کە بوونی نییە، لە PHP 8ـدا : دەوەستێت.
| Codeکۆد | Output in PHP 8 لە PHP 8ـدا |
|---|---|
define("Cons", 2);
echo cons; | Fatal error: Uncaught Error: Undefined constant "cons" |
define("cons", 2);
echo cons; | 2 |
define('', 5); | No error: define() accepts it (see below).هیچ نییە: define() وەریدەگرێت (بڕوانە خوارەوە). |
<?php
define("Cons", 2);
echo cons;
?>Fatal error: Uncaught Error: Undefined constant "cons" in C:\xampp\htdocs\cons.php:3
Stack trace:
#0 {main}
thrown in C:\xampp\htdocs\cons.php on line 3Case-insensitive constants were removedئیتر هەموو نەگۆڕەکان هەستیار بە قەبارەی پیتن
Old PHP had a third argument for define(): true made the constant case-insensitive. Lab 2, Example 12 uses it. It was deprecated in PHP 7.3 and removed in PHP 8.0. Today PHP ignores the true with a warning, and greeting (small letters) is an undefined constant:
PHPی کۆن سێیەمی بۆ define() هەبوو: true وای دەکرد نەبێت. نموونەی 12ی تاقیگەی 2 بەکاری دەهێنێت. لە PHP 7.3 کرایە و لە PHP 8.0 لابرا. ئەمڕۆ PHP بە trueـەکە فەرامۆش دەکات و greeting (بە پیتی بچووک) نەناسراوە:
<?php
define("GREETING", "Welcome to W3Schools.com!",
true);
echo greeting;
?>Warning: define(): Argument #3 ($case_insensitive) is ignored since declaration of case-insensitive constants is no longer supported in C:\xampp\htdocs\example12.php on line 2
Fatal error: Uncaught Error: Undefined constant "greeting" in C:\xampp\htdocs\example12.php:4
Stack trace:
#0 {main}
thrown in C:\xampp\htdocs\example12.php on line 4<?php
define("GREETING", "Welcome to our website!");
echo GREETING;
?>Welcome to our website!
Constantsنەگۆڕەکان
A constant cannot be changedنەگۆڕ ناگۆڕدرێت
A constant gets its value once. You cannot assign a new value with =: PHP refuses the whole file with a parse error.
تەنها یەک جار وەردەگرێت. ناتوانیت بە = بدەیتێ: PHP بە هەموو ڕەت دەکاتەوە.
<?php
define("TAX_RATE", 0.15);
echo TAX_RATE . "<br>";
TAX_RATE = 0.2;
?>Parse error: syntax error, unexpected token "=" in C:\xampp\htdocs\tax.php on line 4
Even line 3 does not run: a parse error stops the file before anything runs.
تەنانەت دێڕی 3یش جێبەجێ نابێت: پێش جێبەجێبوونی هەر شتێک ڕادەگرێت.
Defining the same constant a second time does not change it either. PHP keeps the first value and warns you; in PHP 9 this will become an error.
ناساندنەوەی هەمان بۆ جاری دووەمیش نایگۆڕێت. PHP دەهێڵێتەوە و ئاگادارت دەکاتەوە؛ لە PHP 9ـدا ئەمە دەبێتە .
<?php
define("SITE_NAME", "CêIT");
define("SITE_NAME", "Another name"); // try to change it
echo SITE_NAME;
?>Warning: Constant SITE_NAME already defined, this will be an error in PHP 9 in C:\xampp\htdocs\site.php on line 3 CêIT
Use defined("NAME") to check whether a constant exists before you use it. Since PHP 7 a constant can also hold an array:
defined("NAME") بەکاربهێنە بۆ ئەوەی پێش بەکارهێنان بزانیت هەیە یان نا. لە PHP 7ـەوە دەتوانێت هەڵبگرێت:
<?php
define("DAYS", ["Saturday", "Sunday", "Monday"]);
echo DAYS[0] . "<br>";
var_dump(defined("DAYS"));
var_dump(defined("MONTHS"));
?>Saturday bool(true) bool(false)
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Saturday<br>bool(true)
bool(false)A strange line from the course summaryدێڕێکی سەیر لە کورتەی کۆرسەکەوە
The course summary notes that define('', 5); "works without errors". That is true: define() accepts any string as a name, even an empty one, and returns true. But such a constant cannot be written in code; it can only be read with the constant() function. It is useless in practice. Give constants real names that follow the rules for identifiers: a letter or underscore first, then letters, numbers and underscores.
کورتەی کۆرسەکە دەڵێت define('', 5); «بێ هیچ کار دەکات». ئەمە ڕاستە: define() هەر وەک ناو وەردەگرێت، تەنانەت بەتاڵیش و true دەگەڕێنێتەوە. بەڵام وا ناتوانرێت لە کۆددا بنووسرێت؛ تەنها بە constant() دەخوێنرێتەوە. لە کرداردا هیچ سوودێکی نییە. ناوی ڕاستەقینە بە بدە کە یاساکانی بپارێزن: سەرەتا پیتێک یان ، پاشان پیت و ژمارە و .
<?php
var_dump(define('', 5));
echo constant('');
?>bool(true) 5
Constantsنەگۆڕەکان
Constants are automatically globalنەگۆڕەکان خۆکارانە گشتین
Constants are automatically global and can be used across the entire script, including inside functions. You do not need the global keyword for them. This is a big difference from variables, as the next section shows.
خۆکارانە گشتین و لە هەموو بەکاردێن، لەناو . پێویستت بە global نییە بۆیان. ئەمە جیاوازییەکی گەورەیە لەگەڵ ، وەک بەشی داهاتوو پیشانی دەدات.
<?php
define("SITE_NAME", "CêIT");
function showFooter() {
// no "global" needed: constants are visible everywhere
echo "<p>Welcome to " . SITE_NAME . "</p>";
}
showFooter();
echo "<p>" . SITE_NAME . " is free.</p>";
?>Welcome to CêIT CêIT is free.
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<p>Welcome to CêIT</p><p>CêIT is free.</p>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 6
The function reads
SITE_NAMEdirectly. A variable would needglobalhere.ڕاستەوخۆ
SITE_NAMEدەخوێنێتەوە. لێرەدا پێویستی بەglobalدەبوو.
Variable scopeمەودای گۆڕاوەکان
In PHP, variables can be declared anywhere in the script. The of a variable is the part of the script where the variable can be referenced (used). PHP has three different variable scopes: local, global and static.
لە PHPـدا لە هەر شوێنێکی دروست دەکرێن. ئەو بەشەی کە تێیدا بەکاردێت. PHP سێ جۆری بۆ هەیە: ناوخۆیی ()، گشتی () و ستاتیک ().
The part of a program where a name, such as a variable, can be used. A variable made inside a has ; one made outside all functions has .
ئەو بەشەی کە ناوێک، وەک ، تێیدا بەکار دێت. گۆڕاوێک کە لەناو دروست دەکرێت، تەنها لەناو دەناسرێت؛ گۆڕاوێک کە لە دەرەوەی هەموو دروست دەکرێت، دەکەوێتە ناو .
Think of a family house. The main script is the hall: the family's things there are the global variables. Each function is a room with its own cupboard: its local variables. From inside a room you cannot see the things in the hall, and from the hall you cannot see inside a room's cupboard. The global keyword is like opening a door so that the room can use a thing from the hall. And constants are like the family name on the front door: everyone sees it from everywhere.
ماڵی خێزانێک بهێنە پێش چاوت. هۆڵەکەیە: کەلوپەلی خێزان لەوێ . هەر ژوورێکە بە دۆڵابی خۆیەوە: . لەناو ژوورێکەوە ناتوانیت کەلوپەلی هۆڵەکە ببینیت و لە هۆڵەکەشەوە ناتوانیت ناو دۆڵابی ژوورێک ببینیت. global وەک کردنەوەی دەرگایەکە بۆ ئەوەی ژوورەکە شتێکی هۆڵەکە بەکاربهێنێت. وەک ناوی خێزانن لەسەر دەرگای سەرەکی: هەموو کەس لە هەموو شوێنێکەوە دەیبینێت.
Where the analogy stops workingسنووری ئەم نموونەیە
In a real house you can walk from room to room. In PHP a function cannot see global variables at all unless it asks with global (or $GLOBALS), and the hall can never see a room's local variables. Also, a room is emptied every time you leave it (each call), except for its static variables.
لە ماڵێکی ڕاستەقینەدا دەتوانیت لە ژوورێکەوە بڕۆیت بۆ ژوورێکی تر. لە PHPـدا بە هیچ شێوەیەک نابینێت مەگەر بە global (یان $GLOBALS) داوایان بکات و هۆڵەکەش هەرگیز ژوورێک نابینێت. هەروەها هەر جارێک لە ژوورێک دەچیتە دەرەوە (هەر بانگکردنێک)، ژوورەکە بەتاڵ دەکرێتەوە، جگە لە .
global opens a door, and constants are seen everywhere. ماڵەکەیە؛ هەر ژوورێکە. تەنها global دەرگا دەکاتەوە و لە هەموو شوێنێک دەبینرێن.CêITSee global and local scope, the global keyword and $GLOBALS, and how a static variable keeps its value between function calls.
و ، global و $GLOBALS پیشان دەدات و چۆن لە نێوان دەپارێزێت.
Watch 0:00 to 3:13: global and local scope and the global keyword.
لە 0:00 تا 3:13 سەیر بکە: و و global.
Variable scopeمەودای گۆڕاوەکان
Global scopeمەودای گشتی
A variable declared outside any function has a . It can be used in the main part of the script, but not inside a function, unless you ask for it specially.
کە لە دەرەوەی هەموو دروست دەکرێت، هەیە. لە بەشی سەرەکیی بەکاردێت، بەڵام لەناو بەکارنایەت، مەگەر بە تایبەتی داوای بکەیت.
<?php
$x = 5; // global scope
function myTest() {
// using x inside this function will generate a warning
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
echo "<p>Variable x outside function is: $x</p>";
?>Warning: Undefined variable $x in C:\xampp\htdocs\global.php on line 6 Variable x inside function is: Variable x outside function is: 5
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<br />
<b>Warning</b>: Undefined variable $x in <b>C:\xampp\htdocs\global.php</b> on line <b>6</b><br />
<p>Variable x inside function is: </p><p>Variable x outside function is: 5</p>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
$xis created outside any function: it is global.$xلە دەرەوەی هەموو دروست دەکرێت: گشتییە.- Lineدێڕی 6
Inside
myTest()there is no$x, so PHP warns and prints nothing for it.لەناو
myTest()هیچ$xێک نییە، بۆیە PHP ئاگادار دەکاتەوە و هیچ شتێکی بۆ نانووسێت.- Lineدێڕی 10
Outside the function
$xis visible again: 5.لە دەرەوەی
$xدیسان دەبینرێت: 5.
Variable scopeمەودای گۆڕاوەکان
Local scopeمەودای ناوخۆیی
A variable declared inside a function has a . It can only be used inside that function. When the function ends, the variable is deleted.
کە لە ناو دروست دەکرێت، هەیە. تەنها لەناو ئەو بەکاردێت. کاتێک تەواو دەبێت، دەسڕدرێتەوە.
<?php
function myTest() {
$x = 5; // local scope
echo "<p>Variable x inside function is: $x</p>";
}
myTest();
// using x outside the function will generate a warning
echo "<p>Variable x outside function is: $x</p>";
?>Variable x inside function is: 5 Warning: Undefined variable $x in C:\xampp\htdocs\local.php on line 9 Variable x outside function is:
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<p>Variable x inside function is: 5</p><br />
<b>Warning</b>: Undefined variable $x in <b>C:\xampp\htdocs\local.php</b> on line <b>9</b><br />
<p>Variable x outside function is: </p>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
$xis created inside the function: it is local.$xلەناو دروست دەکرێت: ناوخۆییە.- Lineدێڕی 9
Outside the function there is no
$x: a warning, and an empty value.لە دەرەوەی هیچ
$xێک نییە: و بەتاڵ.
Because the rooms are separate, a function may use the same name as a global variable. They are still two different variables. This example comes from a student's Lab 2 program:
لەبەر ئەوەی ژوورەکان جیان، دەتوانێت هەمان ناوی بەکاربهێنێت. هێشتا دوو جیاوازن. ئەم نموونەیە لە تاقیگەی 2ی خوێندکارێکەوە هاتووە:
$xsame-name.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات<?php
$x = 10; // global $x
function test() {
$x = 18; // a different, local $x
echo "<p>Inside the function: $x</p>";
}
test();
echo "<p>Outside the function: $x</p>";
?>Inside the function: 18 Outside the function: 10
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<p>Inside the function: 18</p><p>Outside the function: 10</p>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
The global
$xholds 10.$xی گشتی 10ی تێدایە.- Lineدێڕی 5
A new, local
$xholds 18. It does not touch the global one.$xێکی نوێ و ناوخۆیی 18ی تێدایە. دەستی بەر$xە گشتییەکە ناکەوێت.
Variable scopeمەودای گۆڕاوەکان
The global keywordوشە کلیلەییەکەی global
The global keyword lets a function use a global variable. Write global and the variable names inside the function, before you use them. Several names can share one global line, separated by commas.
global ڕێگە دەدات بەکاربهێنێت. global و ناوی لەناو بنووسە، پێش ئەوەی بەکاریان بهێنیت. چەند ناوێک دەتوانن یەک دێڕی global بەکاربهێنن، بە فاریزە لێک جیا دەکرێنەوە.
<?php
$x = 5;
$y = 10;
function myTest() {
global $x, $y;
$y = $x + $y;
}
myTest(); // run function
echo $y; // output the new value for variable $y
?>15
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 6
global $x, $y;connects the names$xand$yinside the function to the global variables.global $x, $y;ناوەکانی$xو$yلەناو بە دەبەستێتەوە.- Lineدێڕی 7
This changes the global
$y: 5 + 10 = 15.ئەمە
$yی گشتی دەگۆڕێت: 5 + 10 = 15.- Lineدێڕی 11-12
Run the function, then print the new value of
$y.جێبەجێ بکە، پاشان
$yبنووسە.
A student note: global cannot give a valueتێبینیی خوێندکارێک: global ناتوانێت نرخ بدات
A student tried to connect to the global $x and give it a new value in one line. As the note in the code says, global only declares the name; it cannot initialise it:
خوێندکارێک هەوڵی دا لە یەک دێڕدا بە $xی گشتییەوە ببەسترێت و بداتێ. وەک ناو کۆدەکە دەڵێت، global تەنها ناوەکە ڕادەگەیەنێت؛ ناتوانێت پێ بدات:
global $x = 7; ڕێگەپێدراو نییەtest.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات<?php
$x = 5;
test();
function test() {
global $x = 7;
/*
Error: because 'global' keyword works
only with declaration not initialization.
*/
echo $x;
}
echo $x;
?>Parse error: syntax error, unexpected token "=", expecting "," or ";" in C:\xampp\htdocs\test.php on line 6
The student's explanation is right. To be exact, this is a , so nothing in the file runs, not even the first echo. Split it into two lines:
ڕوونکردنەوەکەی خوێندکارەکە ڕاستە. بە وردی، ئەمە ، بۆیە هیچ شتێکی ناو جێبەجێ نابێت، تەنانەت echoی یەکەمیش. بیکە بە دوو دێڕ:
global، پاشان نرخە نوێیەکەtest.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات<?php
$x = 5;
test();
function test() {
global $x; // first: connect to the global $x
$x = 7; // then: give it a new value
echo $x;
}
echo $x;
?>77
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
The function is called before its definition. This works: PHP reads the functions of a file before it runs the file.
پێش ناساندنەکەی بانگ دەکرێت. ئەمە کار دەکات: PHP پێش جێبەجێکردنی ، ئەو دەخوێنێتەوە.
- Lineدێڕی 7
The global
$xbecomes 7, so bothecholines print 7.$xی گشتی دەبێتە 7، بۆیە هەردوو دێڕیechoژمارە 7 دەنووسن.
A student note: importing a variable that does not existتێبینیی خوێندکارێک: هێنانی گۆڕاوێک کە بوونی نییە
Another note lists $y in the global line although $y was never created. The comment says it "works": there is no error. The note gives no output; the real output is 55 (the function prints 5, then the main script prints 5).
تێبینییەکی تر $y لە دێڕی globalدا دەنووسێت، هەرچەندە $y هەرگیز دروست نەکراوە. دەڵێت «کار دەکات»: هیچ نییە. تێبینییەکە نانووسێت؛ 55ـە ( 5 دەنووسێت، پاشان 5 دەنووسێت).
<?php
$x = 5;
test();
function test() {
global $x, $y;
/*
'y' works, though we
haven't defined it yet.
*/
echo $x;
}
echo $x;
?>55
What happens to $y? global $y creates the global variable with the value NULL, without any warning. If the function then gives it a value, the main script can use it:
$y چی بەسەر دێت؟ global $y بە NULL دروست دەکات، بێ هیچ . ئەگەر دواتر پێ بدات، دەتوانێت بەکاری بهێنێت:
<?php
$x = 5;
test();
function test() {
global $x, $y;
echo $x . "<br>";
var_dump($y); // $y did not exist: now it is NULL
$y = "made inside the function";
}
echo $y;
?>5 NULL made inside the function
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
5<br>NULL
made inside the functionVariable scopeمەودای گۆڕاوەکان
Static variablesگۆڕاوە ستاتیکەکان
Normally, when a function finishes, all of its local variables are deleted. The next call starts again from nothing. However, sometimes we want a local variable not to be deleted, because we need its value for a further job. To do this, use the static keyword when you first declare the variable. Such a variable is called a .
بە ئاسایی، کاتێک تەواو دەبێت، هەموو دەسڕدرێنەوە. بانگکردنی داهاتوو دیسان لە هیچەوە دەست پێدەکات. بەڵام هەندێک جار دەمانەوێت نەسڕدرێتەوە، چونکە بۆ کارێکی تر پێویستە. بۆ ئەمە، کاتێک یەکەم جار دەناسێنیت، static بەکاربهێنە. بەم دەوترێت .
<?php
function myTest() {
static $x = 0;
echo $x;
$x++;
}
myTest();
echo "<br>";
myTest();
echo "<br>";
myTest();
?>0 1 2
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
0<br>1<br>2Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
static $x = 0;runs only the first time. Later calls keep the old value.static $x = 0;تەنها یەکەم جار جێبەجێ دەبێت. بانگکردنەکانی دواتر دەپارێزن.- Lineدێڕی 4-5
Print
$x, then add 1 to it.$xبنووسە، پاشان 1ی بخەرە سەر.
Compare the same function without static. Now $x is created again at 0 on every call:
هەمان بەبێ static بەراورد بکە. ئێستا $x لە هەر بانگکردنێکدا دووبارە بە 0 دروست دەبێتەوە:
<?php
function myTest() {
$x = 0; // no "static": a fresh $x every call
echo $x;
$x++;
}
myTest();
echo "<br>";
myTest();
echo "<br>";
myTest();
?>0 0 0
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
0<br>0<br>0A waiter in a tea house counts the glasses of tea each customer drinks. If he writes the count on a napkin and throws it away after every glass, he always starts again at zero: that is a normal local variable. If he keeps the count on a small notebook in his pocket, it is still there at the next glass: that is a static variable. Nobody else can read his notebook: it stays local to him.
گارسۆنێکی چایخانە ژمارەی ئەو پەرداخە چایانە دەژمێرێت کە هەر کڕیارێک دەیخواتەوە. ئەگەر ژمارەکە لەسەر دەستەسڕێک بنووسێت و دوای هەر پەرداخێک فڕێی بدات، هەمیشە لە سفرەوە دەست پێدەکاتەوە: ئەمە ئاساییە. ئەگەر ژمارەکە لە دەفتەرێکی بچووکی ناو گیرفانیدا هەڵبگرێت، لە پەرداخی داهاتوودا هێشتا ماوە: ئەمە . کەسی تر ناتوانێت دەفتەرەکەی بخوێنێتەوە: هەر بۆ خۆی ناوخۆیی دەمێنێتەوە.
Where the analogy stops workingسنووری ئەم نموونەیە
The notebook survives between calls, but not between page requests: when the script ends, even static variables are gone.
دەفتەرەکە لە نێوان بانگکردنەکاندا دەمێنێتەوە، بەڵام لە نێوان لاپەڕەدا نامێنێت: کاتێک تەواو دەبێت، نامێنن.
A tidy way to see the difference is a counter. The study notes put it simply: static is used when we want the function's variables, "also known as local variables", not to be deleted:
ڕێگایەکی ڕێک بۆ بینینی جیاوازییەکە . تێبینییەکانی خوێندن بە سادەیی دەڵێن: static بەکاردێت کاتێک دەمانەوێت ، کە «پێیان دەوترێت »، نەسڕدرێنەوە:
<?php
function counter() {
static $n = 0;
$n++;
return $n;
}
function counter2() {
$n = 0;
$n++;
return $n;
}
echo counter(), counter(), counter(), " ", counter2(), counter2(), counter2();
?>123 111
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
The static
$nremembers 1, 2, 3 …$nی ستاتیک 1، 2، 3 … هەڵدەگرێت و لەبیری ناچێت.- Lineدێڕی 9
The normal
$nstarts at 0 every time, so it always returns 1.$nی ئاسایی هەموو جارێک لە 0 دەست پێدەکات، بۆیە هەمیشە 1 دەگەڕێنێتەوە.
See global and local scope, the global keyword and $GLOBALS, and how a static variable keeps its value between function calls.
و ، global و $GLOBALS پیشان دەدات و چۆن لە نێوان دەپارێزێت.
Watch from 3:13: static variables.
لە 3:13 بەدواوە سەیر بکە: .
Two student experimentsدوو تاقیکردنەوەی خوێندکار
These two programs were photographed on a student's laptop while practising static. Predict the output before you read it. Remember: static $x=1; runs only on the first call, and $x keeps whatever value it had when the previous call ended.
ئەم دوو لەسەر خوێندکارێک لە کاتی ڕاهێنان لەسەر static وێنەیان گیراوە. پێش خوێندنەوەی ، پێشبینیی بکە. لەبیرت بێت: static $x=1; تەنها لە یەکەم بانگکردندا جێبەجێ دەبێت و $x هەر لە کۆتایی بانگکردنی پێشوودا هەبوو دەیپارێزێت.
++ پاشان --static-a.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات<?php
function aa(){
static $x=1;
echo $x."<br>";
$x++;
echo $x."<br>";
$x--;
echo $x."<br>";
}
aa();
echo "<br>";
aa();
echo "<br>";
aa();
echo "<br>";
?>1 2 1 1 2 1 1 2 1
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
1<br>2<br>1<br><br>1<br>2<br>1<br><br>1<br>2<br>1<br><br>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 4-8
Each call prints 1, adds 1 (2), prints 2, takes 1 away (1) and prints 1.
هەر بانگکردنێک 1 دەنووسێت، 1ی دەخاتە سەر (2)، 2 دەنووسێت، 1ی لێ کەم دەکاتەوە (1) و 1 دەنووسێت.
- Lineدێڕی 11-16
Every call ends with
$xback at 1, so the next call starts at 1 again: 1 2 1, three times.هەر بانگکردنێک بە
$x= 1 کۆتایی دێت، بۆیە بانگکردنی داهاتوو دیسان لە 1ـەوە دەست پێدەکات: 1 2 1، سێ جار.
++static-b.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات<?php
function aa(){
static $x=1;
echo $x."<br>";
$x++;
echo $x."<br>";
$x++;
echo $x."<br>";
}
aa();
echo "<br>";
aa();
echo "<br>";
aa();
echo "<br>";
?>1 2 3 3 4 5 5 6 7
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
1<br>2<br>3<br><br>3<br>4<br>5<br><br>5<br>6<br>7<br><br>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 4-8
Each call prints the value, adds 1, prints, adds 1 again and prints.
هەر بانگکردنێک دەنووسێت، 1ی دەخاتە سەر، دەینووسێت، دیسان 1ی دەخاتە سەر و دەینووسێت.
- Lineدێڕی 11-16
The first call ends with
$x= 3, so the second call starts at 3 (3 4 5) and the third at 5 (5 6 7).بانگکردنی یەکەم بە
$x= 3 کۆتایی دێت، بۆیە بانگکردنی دووەم لە 3ـەوە دەست پێدەکات (3 4 5) و سێیەم لە 5ـەوە (5 6 7).
Reading real lab programsخوێندنەوەی بەرنامەکانی ڕاستەقینەی تاقیگە
These three programs were written by students during Lab 2 and photographed on screen. They mix many ideas from this chapter. Names and personal details have been changed, and the string functions they use are explained fully in Strings. Read each program, predict the output, then read the notes. Each program was saved in a folder inside XAMPP's htdocs folder and opened through localhost: for example, C:\xampp\htdocs\lab2\index.php opens at http://localhost/lab2/.
ئەم سێ لە کاتی تاقیگەی 2دا لەلایەن خوێندکارانەوە نووسراون و لەسەر وێنەیان گیراوە. زۆر بیرۆکەی ئەم بەشە تێکەڵ دەکەن. ناو و زانیاریی کەسی گۆڕدراون و بە تەواوی لە وانەی زنجیرەنووسەکان ڕوون کراونەتەوە. هەر بخوێنەرەوە، پێشبینی بکە، پاشان تێبینییەکان بخوێنەرەوە. هەر لە بوخچەیەکی ناو بوخچەی htdocsی XAMPPـدا پاشەکەوت کرابوو و لە ڕێگەی localhostـەوە کرابووەوە: بۆ نموونە، C:\xampp\htdocs\lab2\index.php لە http://localhost/lab2/ دەکرێتەوە.
Reading real lab programsخوێندنەوەی بەرنامەکانی ڕاستەقینەی تاقیگە
Program 1: global, quotes and ucwords()بەرنامەی 1: global، نیشانەی گوتە و ucwords()
<?php
$hey = 5;
$test = "hey" . " hey2" . " hey3";
function func() {
global $hey;
global $test;
ECHO "<p>Slaw!</p>";
echo '<p>$hey</p>';
echo "<p>$hey</p>";
echo ucwords("<p>$test</p>");
}
func();
function addTwoNums($num1, $num2) {
echo "<p>Summation of both numbers: " . $num1 + $num2 . "</p>";
}
echo addTwoNums(5, 4);
?>Slaw! $hey 5 hey Hey2 Hey3 Summation of both numbers: 9
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<p>Slaw!</p><p>$hey</p><p>5</p><p>hey Hey2 Hey3</p><p>Summation of both numbers: 9</p>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 6-7
globalgives the function access to$heyand$test.globalڕێگە دەدات بگاتە$heyو$test.- Lineدێڕی 8
ECHOworks in capitals: keywords are not case-sensitive.ECHOبە پیتی گەورەش کار دەکات: نین.- Lineدێڕی 9
Single quotes:
$heyis printed literally.تاک:
$heyوەک خۆی دەنووسرێت.- Lineدێڕی 10
Double quotes:
$heybecomes 5.جووت:
$heyدەبێتە 5.- Lineدێڕی 11
ucwords()capitalises the first letter after each space. The first "word" is<p>hey, which starts with<, so hey stays small. Tellingucwords()that>also separates words fixes it:ucwords("<p>$test</p>", " >")gives<p>Hey Hey2 Hey3</p>.ucwords()یەکەم پیتی دوای هەر گەورە دەکات. یەکەم «وشە»<p>heyـە کە بە<دەست پێدەکات، بۆیە hey بچووک دەمێنێتەوە. ئەگەر بەucwords()بڵێیت>یش وشەکان لێک جیا دەکاتەوە، چاک دەبێت:ucwords("<p>$test</p>", " >")ئەنجامی<p>Hey Hey2 Hey3</p>دەدات.- Lineدێڕی 16
This works only because PHP 8 gives
+higher priority than.. In PHP 7 the same line gave a wrong result and a warning. Always use brackets:"…" . ($num1 + $num2) . "…".ئەمە تەنها کار دەکات چونکە لە PHP 8ـدا
+پێش.جێبەجێ دەکرێت (). لە PHP 7ـدا هەمان دێڕ ئەنجامێکی هەڵە و دەدا. هەمیشە کەوانە بەکاربهێنە:"…" . ($num1 + $num2) . "…".- Lineدێڕی 18
The function prints the sum itself and returns nothing (NULL), so this
echoadds nothing.خۆی کۆکە دەنووسێت و هیچ ناگەڕێنێتەوە (NULL)، بۆیە ئەم
echoـە هیچ زیاد ناکات.
<?php
$hey = 5;
$test = "hey" . " hey2" . " hey3";
function func() {
global $hey, $test;
echo "<p>Slaw!</p>";
echo '<p>$hey</p>'; // single quotes: no variables inside
echo "<p>$hey</p>"; // double quotes: $hey becomes 5
echo "<p>" . ucwords($test) . "</p>";
}
func();
function addTwoNums($num1, $num2) {
return $num1 + $num2; // give the answer back
}
echo "<p>Summation of both numbers: " . addTwoNums(5, 4) . "</p>";
?>Slaw! $hey 5 Hey Hey2 Hey3 Summation of both numbers: 9
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<p>Slaw!</p><p>$hey</p><p>5</p><p>Hey Hey2 Hey3</p><p>Summation of both numbers: 9</p>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 10
Capitalise the words first, then add the HTML tags.
سەرەتا وشەکان گەورە بکە، پاشان HTML زیاد بکە.
- Lineدێڕی 15
returngives the answer back to the caller.returnوەڵامەکە دەگەڕێنێتەوە بۆ بانگکەرەکە.
Reading real lab programsخوێندنەوەی بەرنامەکانی ڕاستەقینەی تاقیگە
Program 2: a missing echoبەرنامەی 2: echoێکی ونبوو
Twelve minutes later the same student changed the end of the program:
دوازدە خولەک دواتر هەمان خوێندکار کۆتایی گۆڕی:
<?php
function addTwoNums($num1, $num2) {
echo $num1 + $num2;
}
echo "<p>Summa: "; addTwoNums(5, 4); "</p>";
?>Summa: 9
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<p>Summa: 9Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 5
"</p>";has noecho. It is a statement that does nothing, so</p>is never sent. The browser closes the paragraph by itself."</p>";هیچechoی لەگەڵ نییە. کە هیچ ناکات، بۆیە</p>هەرگیز نانێردرێت. خۆی بڕگەکە دادەخات.
<?php
function addTwoNums($num1, $num2) {
echo $num1 + $num2;
}
echo "<p>Summa: "; addTwoNums(5, 4); echo "</p>";
?>Summa: 9
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<p>Summa: 9</p>Reading real lab programsخوێندنەوەی بەرنامەکانی ڕاستەقینەی تاقیگە
Program 3: a constant, string functions and two $x variablesبەرنامەی 3: نەگۆڕێک، کردارەکانی زنجیرەنووسە و دوو $x
<?php
$a = "Karwan Ahmad " . "<br />";
echo $a;
define('PI', 3.14);
echo PI;
echo "<br />";
echo strtolower($a);
echo strtoupper($a);
echo str_replace("a", "aa", $a);
echo trim(" kar wa an ");
$x = 10;
function showX() {
$x = 18;
echo "<p>Inside the function: $x</p>";
}
showX();
echo "<p>Outside the function: $x</p>";
$firstName = "Karwan";
$space = " ";
$fatherName = "Ahmad";
$grandfatherName = "Rasul";
$fullName = $firstName . $space . $fatherName . $space . $grandfatherName;
echo $fullName;
?>Karwan Ahmad 3.14 karwan ahmad KARWAN AHMAD Kaarwaan Ahmaad kar wa an Inside the function: 18 Outside the function: 10 Karwan Ahmad Rasul
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Karwan Ahmad <br />3.14<br />karwan ahmad <br />KARWAN AHMAD <BR />Kaarwaan Ahmaad <br />kar wa an<p>Inside the function: 18</p><p>Outside the function: 10</p>Karwan Ahmad RasulLook at the HTML source: trim() left kar wa an with all its inner spaces.
سەیری کۆدی HTMLـەکە بکە: trim() kar wa anی بە هەموو ناوەوەیەوە هێشتووەتەوە.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2-3
A name followed by a
<br />tag, kept in one string.ناوێک و پاشان
<br />، لە یەک .- Lineدێڕی 5-6
define()andecho PI;print 3.14.define()وecho PI;3.14 دەنووسن.- Lineدێڕی 9-10
strtolower()andstrtoupper()change the whole string, so<br />becomes<BR />, which still works in HTML.strtolower()وstrtoupper()هەموو دەگۆڕن، بۆیە<br />دەبێتە<BR />، کە هێشتا لە HTMLـدا کار دەکات.- Lineدێڕی 12
str_replace("a", "aa", …)doubles every small a. It is case-sensitive, so the capital A of Ahmad stays single.str_replace("a", "aa", …)هەموو aیەکی بچووک دوو هێندە دەکات. ، بۆیە Aی گەورەی Ahmad وەک خۆی دەمێنێتەوە.- Lineدێڕی 13
trim()removes spaces only at the start and the end. The inner spaces stay; the browser just shows them as one.trim()تەنها سەرەتا و کۆتایی لادەبات. ناوەوە دەمێننەوە؛ تەنها وەک یەک پیشانیان دەدات.- Lineدێڕی 15-21
A global
$x(10) and a different, local$x(18).$xێکی گشتی (10) و$xێکی جیاوازی ناوخۆیی (18).- Lineدێڕی 23-28
A Kurdish three-part name: first name, father's name, grandfather's name, joined with a space variable.
ناوێکی کوردیی سێ بەشی: ناوی خۆی، ناوی باوک، ناوی باپیر، کە بە پێکەوە لکێنراون.
Common mistakesهەڵە باوەکان
| Mistakeهەڵە | What happensچی ڕوودەدات | Fixچاککردن |
|---|---|---|
echo "PI"; | Prints the text PI.دەقی PI دەنووسێت. | echo PI; |
echo $PI; | Warning: Undefined variable $PI | echo PI; |
define("Cons", 2); echo cons; | Error: Undefined constant "cons" | Use exactly the same case: echo Cons;هەمان قەبارەی پیت بەکاربهێنە: echo Cons; |
define("X", 1, true); | Warning: the third argument is ignored (removed in PHP 8).: سێیەم فەرامۆش دەکرێت (لە PHP 8ـدا لابراوە). | define("X", 1); |
PI = 3.14; | Parse error: a constant cannot be assigned.: پێ نادرێت. | Choose the value once, in define(). یەک جار لە define()دا هەڵبژێرە. |
| Using a global variable inside a functionبەکارهێنانی لەناو | Warning: Undefined variable $x | Add global $x; inside the function, or pass the value as a parameter.global $x; لەناو زیاد بکە، یان وەک بنێرە. |
| Using a local variable outside its functionبەکارهێنانی لە دەرەوەی | Warning: Undefined variable $x | Return the value from the function. لە بگەڕێنەوە. |
global $x = 7; | Parse error: syntax error, unexpected token "=" | global $x;
$x = 7; |
| Expecting a normal local variable to remember its valueچاوەڕوانی ئەوەی ئاسایی لەبیر بێت | It starts again on every call.لە هەر بانگکردنێکدا لە سەرەتاوە دەست پێدەکاتەوە. | static $count = 0; |
Practiceڕاهێنان
Exercises
ڕاهێنانەکان
Lab 2, Example 12 ("constant") defines GREETING with a third argument true and then prints greeting in small letters. Run it in PHP 8, explain the two messages, and fix it.
نموونەی 12ی تاقیگەی 2 («constant») GREETING بە سێیەمی true دەناسێنێت و پاشان greeting بە پیتی بچووک دەنووسێت. لە PHP 8ـدا کاری پێبکە، هەردوو پەیامەکە ڕوون بکەرەوە و چاکی بکە.
<?php
define("GREETING", "Welcome to W3Schools.com!",
true);
echo greeting;
?>Show answerوەڵامەکە پیشان بدە
The warning says the third argument is ignored, because case-insensitive constants no longer exist. The fatal error says greeting is undefined, because only GREETING exists. Remove the third argument and use the exact name:
دەڵێت سێیەم فەرامۆش کراوە، چونکە ئیتر ناتوانرێت دروست بکرێت کە نەبێت. دەڵێت greeting نەناسراوە، چونکە تەنها GREETING هەیە. سێیەم لاببە و ناوە ڕێکەکە بەکاربهێنە:
<?php
define("GREETING", "Welcome to our website!");
echo GREETING;
?>Welcome to our website!
What do these two scripts print? They differ in only one line.
ئەم دوو چی دەنووسن؟ تەنها لە یەک دێڕدا جیاوازن.
<?php
$x = 1;
function change() {
$x = 2;
echo $x;
}
change();
echo $x;
?><?php
$x = 1;
function change() {
global $x;
$x = 2;
echo $x;
}
change();
echo $x;
?>Show answerوەڵامەکە پیشان بدە
The first prints 21: the function changes its own local $x (2), and the global $x is still 1. The second prints 22: global $x; makes the function change the global $x.
یەکەمیان 21 دەنووسێت: $xی ناوخۆیی خۆی دەگۆڕێت (2) و $xی گشتی هێشتا 1ـە. دووەمیان 22 دەنووسێت: global $x; وا دەکات $xی گشتی بگۆڕێت.
21
22
This is a variation of the student experiments: the static variable starts at 5 and goes down. What does it print?
ئەمە وەشانێکی تری تاقیکردنەوەکانی خوێندکارەکانە: لە 5ـەوە دەست پێدەکات و دادەبەزێت. چی دەنووسێت؟
<?php
function aa(){
static $x=5;
echo $x."<br>";
$x--;
echo $x."<br>";
$x--;
echo $x."<br>";
}
aa();
echo "<br>";
aa();
?>Show answerوەڵامەکە پیشان بدە
The first call prints 5, 4, 3 and leaves $x at 3. The second call does not reset it, so it prints 3, 2, 1.
بانگکردنی یەکەم 5، 4، 3 دەنووسێت و $x لە 3دا دەهێڵێتەوە. بانگکردنی دووەم دووبارە دەستی پێناکاتەوە، بۆیە 3، 2، 1 دەنووسێت.
5 4 3 3 2 1
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
5<br>4<br>3<br><br>3<br>2<br>1<br>Write a function countVisitors() that prints Visitor number 1, Visitor number 2, … each time it is called. Do not use a global variable.
بە ناوی countVisitors() بنووسە کە هەر جارێک بانگ بکرێت Visitor number 1، Visitor number 2، … بنووسێت. بەکارمەهێنە.
Hintئاماژە
A static variable remembers its value between calls.
لە نێوان بانگکردنەکاندا لەبیر دەمێنێت.
Show answerوەڵامەکە پیشان بدە
<?php
function countVisitors() {
static $visitors = 0;
$visitors++;
echo "Visitor number $visitors<br>";
}
countVisitors();
countVisitors();
countVisitors();
?>Visitor number 1 Visitor number 2 Visitor number 3
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Visitor number 1<br>Visitor number 2<br>Visitor number 3<br>Define a constant SITE_NAME with your website's name. Write a function showFooter() that prints it inside a paragraph, call the function, and also print the constant outside the function. Do you need global?
بە ناوی SITE_NAME بە ناوی بناسێنە. بە ناوی showFooter() بنووسە کە لەناو بڕگەیەکدا بینووسێت، بانگی بکە و لە دەرەوەی بنووسە. ئایا پێویستت بە global هەیە؟
Show answerوەڵامەکە پیشان بدە
<?php
define("SITE_NAME", "CêIT");
function showFooter() {
// no "global" needed: constants are visible everywhere
echo "<p>Welcome to " . SITE_NAME . "</p>";
}
showFooter();
echo "<p>" . SITE_NAME . " is free.</p>";
?>Welcome to CêIT CêIT is free.
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<p>Welcome to CêIT</p><p>CêIT is free.</p>No: constants are automatically global.
نەخێر: خۆکارانە گشتین.
Reviewپێداچوونەوە
How is a constant different from a variable?نەگۆڕ چ جیاوازییەکی لەگەڵ گۆڕاو هەیە؟
A constant has no $, gets its value once with define() or const, cannot be changed, and is automatically global.
$ی نییە، یەک جار بە define() یان const وەردەگرێت، ناگۆڕدرێت و خۆکارانە گشتییە.
Why is define("NAME", "x", true) wrong today?بۆچی ئەمڕۆ define("NAME", "x", true) هەڵەیە؟
Case-insensitive constants were removed in PHP 8.0. The third argument is ignored with a warning.
ئەو نەبوون، لە PHP 8.0ـدا لابران. سێیەم بە فەرامۆش دەکرێت.
What does echo "PI"; print, and why?echo "PI"; چی دەنووسێت و بۆچی؟
The text PI: in quotes it is a string, not the constant.
دەقی PI: لەناو ، نەک .
What is the scope of a variable?مەودای گۆڕاوێک چییە؟
The part of the script where the variable can be used.
ئەو بەشەی کە تێیدا بەکاردێت.
What is the difference between local and global scope?جیاوازیی نێوان مەودای ناوخۆیی و مەودای گشتی چییە؟
A global variable is created outside functions and is used outside them; a local variable is created inside a function and exists only there, until the function ends.
لە دەرەوەی دروست دەکرێت و لە دەرەوەیان بەکاردێت؛ لەناو دروست دەکرێت و تەنها لەوێ هەیە، تا تەواو دەبێت.
How can a function use a global variable?کردار چۆن دەتوانێت گۆڕاوێکی گشتی بەکاربهێنێت؟
With global $x; inside the function (on its own line, without a value), or with $GLOBALS['x'].
بە global $x; لەناو (لە دێڕی خۆیدا، بێ )، یان بە $GLOBALS['x'].
What does static do to a local variable?static چی بە گۆڕاوێکی ناوخۆیی دەکات؟
It keeps the value between calls; the initial value is set only on the first call. The variable is still visible only inside the function.
لە نێوان بانگکردنەکاندا دەپارێزێت؛ تەنها لە یەکەم بانگکردندا دادەنرێت. هێشتا تەنها لەناو دەبینرێت.
Is "Undefined variable $x" inside a function a fatal error?ئایا «Undefined variable $x» لەناو کردارێک هەڵەی کوشندەیە؟
No, it is a warning in PHP 8. The script continues with an empty value.
نەخێر، لە PHP 8ـدا . بە بەتاڵ بەردەوام دەبێت.
Key points
خاڵە سەرەکییەکان
- Constants:
define("NAME", value);orconst NAME = value;, no$, capitals by convention, case-sensitive, unchangeable, automatically global. define()withtrueas a third argument no longer works (PHP 8).- Global variables live outside functions; local variables live inside one function.
- Use
global $x;(or$GLOBALS) to reach a global variable from a function, and use it sparingly. statickeeps a local variable's value between calls.
- :
define("NAME", value);یانconst NAME = value;، بێ$، بە نەریت پیتی گەورە، ، نەگۆڕاو، خۆکارانە گشتی. define()لەگەڵtrueوەک سێیەم ئیتر کار ناکات (PHP 8).- لە دەرەوەی دەژین؛ لەناو یەک دەژین.
global $x;(یان$GLOBALS) بەکاربهێنە بۆ گەیشتن بە لە و بە کەمی بەکاری بهێنە.staticلە نێوان بانگکردنەکاندا دەپارێزێت.
Lesson complete!وانەکە تەواو بوو!
Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.
Key termsزاراوە سەرەکییەکان
Strings and string functionsزنجیرەنووسەکان و کردارەکانی زنجیرەنووسە
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 ن.