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⁩.

23 min read21 خولەک خوێندنەوە · 19 stepsهەنگاو

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

  • Create constants with define() and const, 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 global keyword 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(); جێبەجێی دەکات (بانگی دەکات).

Defining and calling a functionناساندنی کردار و بانگکردنی کردارhello.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function sayHello() {        // define the function
    echo "Hello!<br>";
}

sayHello();                  // call it: the code inside runs
sayHello();                  // call it again
?>
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".

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

Definitionپێناسە

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، وەک بەڵێنێک کە نایگۆڕن.

A constant is a sealed signنەگۆڕ وەک تابلۆیەکی مۆرکراو

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 PI or TAX_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); ناوەکە لەناو .
  • ناوی .
  • خۆکارانە گشتین: لە هەر شوێنێکی بەکاردێن، تەنانەت لەناو .
  • کاتێک ناسێنرا، ناگۆڕدرێت و ناتوانرێت دووبارە بناسێنرێتەوە.
Defining and using a constantناساندن و بەکارهێنانی نەگۆڕێکpi.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
define('PI', 3.14159);
echo PI;
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

define() creates the constant PI with the value 3.14159.

define() PI بە ⁦3.14159⁩ دروست دەکات.

Lineدێڕی 3

Use the name with no $ and no quotes.

ناوەکە بێ $ و بێ بەکاربهێنە.

What Are Constants & Variable Variables In PHP - Full PHP 8 TutorialProgram With Gio · Englishئینگلیزی · 160K viewsبینین · 4.1K likesلایک · Partبەشی 0:00–5:38 (ofلە 8:07)

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یش بەکاری بهێنیت. هەردووکیان هەمان جۆری دروست دەکەن.

Constants made with constنەگۆڕەکان بە constconst.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
const PI = 3.14159;
const SITE_NAME = "CêIT";
echo PI . "<br>" . SITE_NAME;
?>

Constantsنەگۆڕەکان

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

A student note: the name in quotesتێبینیی خوێندکارێک: ناوەکە لەناو نیشانەی گوتەداw4.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
    define("w4", 3.14);
    echo "w4";
    //Output: w4
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

Creates the constant w4 with the value 3.14.

w4 بە ⁦3.14⁩ دروست دەکات.

Lineدێڕی 3

"w4" is a string, so echo prints the text w4.

"w4" ، بۆیە echo دەقی ⁦w4⁩ دەنووسێت.

Text versus constantدەق بەرامبەر نەگۆڕw4.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
define("w4", 3.14);
echo "w4";     // in quotes: the text w4
echo "<br>";
echo w4;       // no quotes, no $: the constant
?>
A constant name with $ is a variableناوی نەگۆڕ لەگەڵ $ دەبێتە گۆڕاوdollar.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
define("MAX_USERS", 50);
echo $MAX_USERS;
?>

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). ئەمە کار دەکات، بەڵام نەریت پیتی گەورەیە، تاکو لە جیا بکرێنەوە.

Quick checkپشکنینی خێراAfter define("CITY", "Erbil");, which line prints Erbil?دوای define("CITY", "Erbil");، کام دێڕ Erbil دەنووسێت؟

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

The constants table from the course summary, checked with PHP 8.5خشتەی لە کورتەی کۆرسەکەوە، تاقیکراوە بە ⁦PHP 8.5⁩
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() وەریدەگرێت (بڕوانە خوارەوە).
Wrong case: the script stopsقەبارەی هەڵەی پیت: دەسنووسەکە دەوەستێتcons.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
define("Cons", 2);
echo cons;
?>

Case-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 (بە پیتی بچووک) نەناسراوە:

Lab 2, Example 12 as written (fails in PHP 8)نموونەی ⁦12⁩ی تاقیگەی 2 وەک خۆی (لە ⁦PHP 8⁩ـدا سەرناکەوێت)example12.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
 define("GREETING", "Welcome to W3Schools.com!",
true);
 echo greeting;
 ?>
Example 12 for PHP 8نموونەی ⁦12⁩ بۆ ⁦PHP 8⁩example12.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
define("GREETING", "Welcome to our website!");
echo GREETING;
?>
Quick checkپشکنینی خێراWhat is true about define("X", 1, true); in PHP 8?لە ⁦PHP 8⁩ـدا چی ڕاستە دەربارەی define("X", 1, true);؟

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 بە هەموو ڕەت دەکاتەوە.

Trying to assign to a constantهەوڵدان بۆ دانانی نرخ بۆ نەگۆڕێکtax.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
define("TAX_RATE", 0.15);
echo TAX_RATE . "<br>";
TAX_RATE = 0.2;
?>

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⁩ـدا ئەمە دەبێتە .

Defining a constant twiceدوو جار ناساندنی نەگۆڕێکsite.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
define("SITE_NAME", "CêIT");
define("SITE_NAME", "Another name");   // try to change it
echo SITE_NAME;
?>
Quick checkپشکنینی خێراWhat does this print? define("A", 1); define("A", 2); echo A;ئەمە چی دەنووسێت؟ define("A", 1); define("A", 2); echo A;

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⁩ـەوە دەتوانێت هەڵبگرێت:

days.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
define("DAYS", ["Saturday", "Sunday", "Monday"]);
echo DAYS[0] . "<br>";
var_dump(defined("DAYS"));
var_dump(defined("MONTHS"));
?>

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() دەخوێنرێتەوە. لە کرداردا هیچ سوودێکی نییە. ناوی ڕاستەقینە بە بدە کە یاساکانی بپارێزن: سەرەتا پیتێک یان ، پاشان پیت و ژمارە و .

empty-name.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
var_dump(define('', 5));
echo constant('');
?>

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

A constant used inside a functionنەگۆڕێک لەناو کردارێکfooter.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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>";
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 6

The function reads SITE_NAME directly. A variable would need global here.

ڕاستەوخۆ SITE_NAME دەخوێنێتەوە. لێرەدا پێویستی بە global دەبوو.

Quick checkپشکنینی خێراA constant SITE is defined at the top of a file. What must a function do to print it? بە ناوی SITE لە سەرەتای ناسێنراوە. دەبێت چی بکات بۆ ئەوەی بینووسێت؟

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 سێ جۆری بۆ هەیە: ناوخۆیی ()، گشتی () و ستاتیک ().

Definitionپێناسە

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 .

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

Rooms in a houseژوورەکانی ماڵێک

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) داوایان بکات و هۆڵەکەش هەرگیز ژوورێک نابینێت. هەروەها هەر جارێک لە ژوورێک دەچیتە دەرەوە (هەر بانگکردنێک)، ژوورەکە بەتاڵ دەکرێتەوە، جگە لە .

A house. On the roof a sign reads "Constants: seen in every room" with define("PI", 3.14);. Inside, the hall (main script) holds the global variables $x = 5; and $y = 10;. Below it, echo $z; fails because $z lives only in its room. Room showX() has a local $z = 1; and an echo $x; that cannot see the global $x (a dashed line with a cross). Room addXY() has global $x, $y;, shown by an arrow from the hall, so it can change the global $y.ماڵێک. لەسەر سەربانەکە تابلۆیەک نووسراوە «نەگۆڕەکان: لە هەموو ژوورێک دیارن» لەگەڵ define("PI", 3.14);. لەناوەوە، هۆڵەکە (دەسنووسە سەرەکییەکە) گۆڕاوە گشتییەکان $x = 5; و $y = 10;ی تێدایە. لە خوارییەوە echo $z; سەرناکەوێت، چونکە $z تەنها لە ژوورەکەی خۆیدایە. ژووری showX() گۆڕاوێکی ناوخۆیی $z = 1; و echo $x;ی تێدایە، کە $xی گشتی نابینێت (هێڵێکی پچڕپچڕ بە خاچێک). ژووری addXY() دێڕی global $x, $y;ی تێدایە، کە بە تیرێک لە هۆڵەکەوە پیشان دراوە، بۆیە دەتوانێت $yی گشتی بگۆڕێت.
Global scope is the house; each function is a room. Only global opens a door, and constants are seen everywhere. ماڵەکەیە؛ هەر ژوورێکە. تەنها global دەرگا دەکاتەوە و لە هەموو شوێنێک دەبینرێن.CêIT
PHP Variable Scopes - Static Variables - Full PHP 8 TutorialProgram With Gio · Englishئینگلیزی · 53K viewsبینین · 2.4K likesلایک · Partبەشی 0:00–3:13 (ofلە 5:41)

See 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.

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

A global variable is not visible inside a functionگۆڕاوێکی گشتی لەناو کردارێک نابینرێتglobal.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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>";
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

$x is 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 $x is visible again: 5.

لە دەرەوەی $x دیسان دەبینرێت: 5.

Quick checkپشکنینی خێراA variable is created outside all functions. Can a function read it without global or $GLOBALS? لە دەرەوەی هەموو دروست کراوە. ئایا دەتوانێت بەبێ global یان $GLOBALS بیخوێنێتەوە؟

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.

کە لە ناو دروست دەکرێت، هەیە. تەنها لەناو ئەو بەکاردێت. کاتێک تەواو دەبێت، دەسڕدرێتەوە.

A local variable is not visible outside its functionگۆڕاوێکی ناوخۆیی لە دەرەوەی کردارەکەی نابینرێتlocal.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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>";
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

$x is created inside the function: it is local.

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

Lineدێڕی 9

Outside the function there is no $x: a warning, and an empty value.

لە دەرەوەی هیچ $xێک نییە: و بەتاڵ.

Quick checkپشکنینی خێراWhat happens? function t() { $a = 3; } t(); echo $a;چی ڕوودەدات؟ function t() { $a = 3; } t(); echo $a;

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ی خوێندکارێکەوە هاتووە:

Two variables called $xدوو گۆڕاو بە ناوی $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>";
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

The global $x holds 10.

$xی گشتی ⁦10⁩ی تێدایە.

Lineدێڕی 5

A new, local $x holds 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 بەکاربهێنن، بە فاریزە لێک جیا دەکرێنەوە.

Adding two global variables inside a functionکۆکردنەوەی دوو گۆڕاوی گشتی لەناو کردارێکglobal-keyword.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 6

global $x, $y; connects the names $x and $y inside 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; is not allowedglobal $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;
?>

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ی یەکەمیش. بیکە بە دوو دێڕ:

First global, then the new valueسەرەتا 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;
?>
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 $x becomes 7, so both echo lines print 7.

$xی گشتی دەبێتە 7، بۆیە هەردوو دێڕی echo ژمارە 7 دەنووسن.

Quick checkپشکنینی خێراInside a function, what is the correct way to set the global $x to 7?لەناو ، ڕێگای دروست بۆ ئەوەی $xی گشتی بکرێتە 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 دەنووسێت).

test2.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
    $x = 5;
    test();

    function test() {
        global $x, $y;
        /*
        'y' works, though we
        haven't defined it yet.
        */
        echo $x;
    }

    echo $x;
?>

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 دروست دەکات، بێ هیچ . ئەگەر دواتر پێ بدات، دەتوانێت بەکاری بهێنێت:

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

Variable 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 بەکاربهێنە. بەم دەوترێت .

A static variable keeps its value between callsگۆڕاوێکی ستاتیک نرخەکەی لە نێوان بانگکردنەکاندا دەپارێزێتstatic.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function myTest() {
    static $x = 0;
    echo $x;
    $x++;
}

myTest();
echo "<br>";
myTest();
echo "<br>";
myTest();
?>
Explain 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 دروست دەبێتەوە:

not-static.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function myTest() {
    $x = 0;          // no "static": a fresh $x every call
    echo $x;
    $x++;
}

myTest();
echo "<br>";
myTest();
echo "<br>";
myTest();
?>
Two rows of three function calls. Without static: each call starts at 0, shows 0 and deletes the variable at the end; output 0 0 0. With static: the first call starts at 0, shows 0 and keeps 1; the second still has 1, shows 1 and keeps 2; the third still has 2, shows 2 and keeps 3. Arrows carry the kept value to the next call; output 0 1 2.دوو ڕیز، هەر ڕیزێک سێ بانگکردنی کردار. بەبێ static: هەر بانگکردنێک لە 0 دەست پێدەکات، 0 نیشان دەدات و لە کۆتاییدا گۆڕاوەکە دەسڕدرێتەوە؛ دەرخستە: 0 0 0. لەگەڵ static: بانگکردنی یەکەم لە 0 دەست پێدەکات، 0 نیشان دەدات و 1 دەهێڵێتەوە؛ دووەم هێشتا 1ی تێدایە، 1 نیشان دەدات و 2 دەهێڵێتەوە؛ سێیەم هێشتا 2ی تێدایە، 2 نیشان دەدات و 3 دەهێڵێتەوە. تیرەکان ژمارە هێڵدراوەکە دەگوازنەوە بۆ بانگکردنی داهاتوو؛ دەرخستە: 0 1 2.
A normal local variable is deleted after every call; a static one survives. ئاسایی دوای هەر بانگکردنێک دەسڕدرێتەوە؛ دەمێنێتەوە.CêIT
The tea-house tallyژمارەکردنی چایخانە

A 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 بەکاردێت کاتێک دەمانەوێت ، کە «پێیان دەوترێت »، نەسڕدرێنەوە:

A static counter and a normal oneژمێرەرێکی ستاتیک و ژمێرەرێکی ئاساییcounter.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function counter() {
    static $n = 0;
    $n++;
    return $n;
}

function counter2() {
    $n = 0;
    $n++;
    return $n;
}

echo counter(), counter(), counter(), " ", counter2(), counter2(), counter2();
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

The static $n remembers 1, 2, 3 …

$nی ستاتیک 1، 2، 3 … هەڵدەگرێت و لەبیری ناچێت.

Lineدێڕی 9

The normal $n starts at 0 every time, so it always returns 1.

$nی ئاسایی هەموو جارێک لە 0 دەست پێدەکات، بۆیە هەمیشە 1 دەگەڕێنێتەوە.

Quick checkپشکنینی خێراWhat does this print? function f() { static $n = 5; $n++; echo $n; } f(); f();ئەمە چی دەنووسێت؟ function f() { static $n = 5; $n++; echo $n; } f(); f();

PHP Variable Scopes - Static Variables - Full PHP 8 TutorialProgram With Gio · Englishئینگلیزی · 53K viewsبینین · 2.4K likesلایک · Partبەشی 3:13–5:41 (ofلە 5:41)

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 هەر لە کۆتایی بانگکردنی پێشوودا هەبوو دەیپارێزێت.

Experiment 1: ++ then --تاقیکردنەوەی 1: ++ پاشان --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>";
?>
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 $x back at 1, so the next call starts at 1 again: 1 2 1, three times.

هەر بانگکردنێک بە $x = 1 کۆتایی دێت، بۆیە بانگکردنی داهاتوو دیسان لە 1ـەوە دەست پێدەکات: ⁦1 2 1⁩، سێ جار.

Experiment 2: ++ twiceتاقیکردنەوەی 2: دوو جار ++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>";
?>
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()

As the student wrote it (cleaned up)وەک خوێندکارەکە نووسیویەتی (ڕێکخراو)index.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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);
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 6-7

global gives the function access to $hey and $test.

global ڕێگە دەدات بگاتە $hey و $test.

Lineدێڕی 8

ECHO works in capitals: keywords are not case-sensitive.

ECHO بە پیتی گەورەش کار دەکات: نین.

Lineدێڕی 9

Single quotes: $hey is printed literally.

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

Lineدێڕی 10

Double quotes: $hey becomes 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. Telling ucwords() 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 echo adds nothing.

خۆی کۆکە دەنووسێت و هیچ ناگەڕێنێتەوە ⁦(NULL)⁩، بۆیە ئەم echoـە هیچ زیاد ناکات.

A cleaner versionوەشانێکی ڕێکترindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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>";
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 10

Capitalise the words first, then add the HTML tags.

سەرەتا وشەکان گەورە بکە، پاشان HTML زیاد بکە.

Lineدێڕی 15

return gives 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:

دوازدە خولەک دواتر هەمان خوێندکار کۆتایی گۆڕی:

index.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function addTwoNums($num1, $num2) {
    echo $num1 + $num2;
}
echo "<p>Summa: "; addTwoNums(5, 4); "</p>";
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 5

"</p>"; has no echo. It is a statement that does nothing, so </p> is never sent. The browser closes the paragraph by itself.

"</p>"; هیچ echoی لەگەڵ نییە. کە هیچ ناکات، بۆیە </p> هەرگیز نانێردرێت. خۆی بڕگەکە دادەخات.

Fixedچاککراوindex.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function addTwoNums($num1, $num2) {
    echo $num1 + $num2;
}
echo "<p>Summa: "; addTwoNums(5, 4); echo "</p>";
?>

Reading real lab programsخوێندنەوەی بەرنامەکانی ڕاستەقینەی تاقیگە

Program 3: a constant, string functions and two $x variablesبەرنامەی 3: نەگۆڕێک، کردارەکانی زنجیرەنووسە و دوو $x

lab2.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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;
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2-3

A name followed by a <br /> tag, kept in one string.

ناوێک و پاشان <br />، لە یەک .

Lineدێڕی 5-6

define() and echo PI; print 3.14.

define() و echo PI; ⁦3.14⁩ دەنووسن.

Lineدێڕی 9-10

strtolower() and strtoupper() 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هەڵە باوەکان

Common mistakes with constants and scopeهەڵە باوەکان لەگەڵ و
MistakeهەڵەWhat happensچی ڕوودەداتFixچاککردن
echo "PI";Prints the text PI.دەقی PI دەنووسێت.echo PI;
echo $PI;Warning: Undefined variable $PIecho 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 $xAdd global $x; inside the function, or pass the value as a parameter.global $x; لەناو زیاد بکە، یان وەک بنێرە.
Using a local variable outside its functionبەکارهێنانی لە دەرەوەی Warning: Undefined variable $xReturn 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

ڕاهێنانەکان

ExerciseڕاهێنانLab task: Example 12 for PHP 8ئەرکی تاقیگە: نموونەی ⁦12⁩ بۆ ⁦PHP 8⁩

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⁩ـدا کاری پێبکە، هەردوو پەیامەکە ڕوون بکەرەوە و چاکی بکە.

The lab codeکۆدی تاقیگەکەexample12.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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 هەیە. سێیەم لاببە و ناوە ڕێکەکە بەکاربهێنە:

example12.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
define("GREETING", "Welcome to our website!");
echo GREETING;
?>
ExerciseڕاهێنانPredict: local or global?پێشبینی بکە: ناوخۆیی یان گشتی؟

What do these two scripts print? They differ in only one line.

ئەم دوو چی دەنووسن؟ تەنها لە یەک دێڕدا جیاوازن.

a.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$x = 1;

function change() {
    $x = 2;
    echo $x;
}

change();
echo $x;
?>
b.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?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ی گشتی بگۆڕێت.

First scriptدەسنووسی یەکەم
21
Second scriptدەسنووسی دووەم
22
ExerciseڕاهێنانPredict: a static countdownپێشبینی بکە: ژمارەکردنی پێچەوانەی ستاتیک

This is a variation of the student experiments: the static variable starts at 5 and goes down. What does it print?

ئەمە وەشانێکی تری تاقیکردنەوەکانی خوێندکارەکانە: لە 5ـەوە دەست پێدەکات و دادەبەزێت. چی دەنووسێت؟

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

What the browser showsئەوەی تۆڕگەڕ پیشانی دەدات
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>
ExerciseڕاهێنانWrite a visitor counterژمێرەرێکی سەردانکەر بنووسە

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وەڵامەکە پیشان بدە
visitors.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function countVisitors() {
    static $visitors = 0;
    $visitors++;
    echo "Visitor number $visitors<br>";
}

countVisitors();
countVisitors();
countVisitors();
?>
ExerciseڕاهێنانA constant in a functionنەگۆڕێک لەناو کردارێک

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

No: constants are automatically global.

نەخێر: خۆکارانە گشتین.

Reviewپێداچوونەوە

Review questionsپرسیارەکانی پێداچوونەوە
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); or const NAME = value;, no $, capitals by convention, case-sensitive, unchangeable, automatically global.
  • define() with true as 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.
  • static keeps 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زاراوە سەرەکییەکان

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

Strings and string functionsزنجیرەنووسەکان و کردارەکانی زنجیرەنووسە

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

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