Chapterبەش 5 · Lectureوانە 5.3

Output with echo and printدەرخستە بە echo و print

How PHP sends output to the page with echo and print: strings, several items, joining with the dot, new lines and <br>, printing HTML and variables, heredoc, and the differences between echo and print.

PHP چۆن بە echo و print دەنێرێت بۆ لاپەڕە: ، چەند بڕگەیەک، بە خاڵ، دێڕی نوێ و ⁦<br>⁩، نووسینی HTML و ، heredoc و جیاوازییەکانی echo و print.

27 min read26 خولەک خوێندنەوە · 27 stepsهەنگاو

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

  • Print text, numbers and HTML with echo and print.
  • Print several items with one echo and join strings with the dot operator.
  • Explain the difference between n in the page source and <br> on the page.
  • Compare echo and print, and use print's return value.
  • Print a whole block of HTML with a heredoc.
  • دەق، ژمارە و HTML بە echo و print بنووسیت.
  • چەند بڕگەیەک بە یەک echo بنووسیت و بە خاڵ پێکەوە ببەستیتەوە.
  • جیاوازیی نێوان n لە سەرچاوەی لاپەڕەدا و ⁦<br>⁩ لەسەر لاپەڕەکە ڕوون بکەیتەوە.
  • echo و print بەراورد بکەیت و گەڕاوەی print بەکار بهێنیت.
  • بلۆکێکی تەواوی HTML بە heredoc بنووسیت.

Sending output to the pageناردنی دەرخستە بۆ لاپەڕە

A PHP page is useful only when it says something. Everything that PHP prints becomes part of the HTML page that the server sends to the browser. This printed text is called the .

لاپەڕەیەکی PHP تەنها کاتێک بەسوودە کە شتێک بڵێت. هەموو ئەوەی PHP دەینووسێت دەبێت بە بەشێک لەو لاپەڕە HTMLـەی بۆ دەنێرێت. بەم دەقە نووسراوە دەگوترێت .

PHP has two basic tools for output: echo and print. In this lecture you learn both: how to print text, numbers and HTML, how to join pieces of text, how to make new lines, and how echo and print differ. All the examples from the slides, the labs, the summary, the Kurdish notes and the “Chp2” echo table are here, corrected and tested with PHP 8.5.

PHP دوو ئامرازی بنەڕەتیی هەیە بۆ : echo و print. لەم وانەیەدا هەردووکیان فێر دەبیت: چۆن دەق، ژمارە و HTML بنووسیت، چۆن پارچەکانی دەق پێکەوە ببەستیتەوە، چۆن دێڕی نوێ دروست بکەیت و echo و print چ جیاوازییەکیان هەیە. هەموو نموونەکانی سلاید، تاقیگە، کورتە، تێبینییە کوردییەکان و خشتەی echoـی «⁦Chp2⁩» لێرەدان، ڕاستکراونەتەوە و بە ⁦PHP 8.5⁩ تاقی کراونەتەوە.

The echo statementڕستەی echo

This is the structure of a basic PHP statement, as shown in Lecture 1:

ئەمە پێکهاتەی بنەڕەتیی PHPـە، وەک لە وانەی 1دا پیشان دراوە:

The basic echo statementڕستەی بنەڕەتیی echohello.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php echo "Hello World!"; ?>
The statement <?php echo "Hello World!"; ?> with labels: <?php is the opening tag where PHP code starts; echo sends output to the page; "Hello World!" is a string inside straight double quotes; the semicolon is the instruction separator that ends the statement; ?> is the closing tag, back to HTML.دێڕی <?php echo "Hello World!"; ?> بە ناونیشانەوە: <?php تاگی کردنەوەیە و کۆدی PHP لێرەوە دەست پێدەکات؛ echo نووسینەکە دەنێرێت بۆ پەڕەکە؛ "Hello World!" دەقێکە لەنێو جووتە نیشانەی گوتەی ڕاستدا؛ نیشانەی ; کۆتایی بە ڕستەکە دەهێنێت؛ ?> تاگی داخستنە و گەڕانەوەیە بۆ HTML.
The parts of a basic PHP statement.بەشەکانی بنەڕەتیی PHP.CêIT
  • <?php and ?> open and close the PHP section (see PHP Syntax).
  • echo prints something: it sends output to the page.
  • "Hello World!" is a : text between .
  • ; is the instruction separator: it marks the end of this instruction.
  • <?php و ?> کەرتی PHP دەکەنەوە و دایدەخەن (بڕوانە داڕستنی PHP).
  • echo شتێک دەنووسێت: دەنێرێت بۆ لاپەڕەکە.
  • "Hello World!" : دەقێک لە نێوان .
  • ; : کۆتایی ئەم ڕێنماییە دیاری دەکات.
What the slide's version with curly quotes printsئەوەی وەشانی سلایدەکە بە نیشانەی چەماوە دەینووسێت
Parse error: syntax error, unexpected identifier "World", expecting "," or ";" in hello.php on line 1

PHP prints the full path of the file in its messages; here we show only the file name.

PHP لە پەیامەکانیدا تەواوی دەنووسێت؛ لێرەدا تەنها ناوی داناوە.

Quick checkپشکنینی خێراWhat is echo in PHP?echo لە PHPـدا چییە؟

The echo statementڕستەی echo

Why is it called “echo”?بۆچی ناوی «echo»ـە؟

The idea of “echo” is not special to PHP; it is much older. The Kurdish notes point this out because in Kurdistan many students think of PHP as soon as they hear the word, since that is where they meet it most. In fact, echo is also a command in the Windows command prompt and in Linux shells, where it prints text in the same way.

چەمکی «echo» تایبەت نییە بە PHP؛ زۆر کۆنترە. تێبینییە کوردییەکان ئاماژەی پێ دەدەن، چونکە لە کوردستاندا زۆر خوێندکار هەر کە ئەم وشەیە دەبیستن بیر لە PHP دەکەنەوە، لەبەر ئەوەی زیاتر لەوێدا تووشی دەبن. لە ڕاستیدا echo فەرمانێکیشە لە ⁦Command Prompt⁩ـی Windows و لە shellـەکانی Linuxـدا، کە بە هەمان شێوە دەق دەنووسێت.

“Echo” is originally a Greek word meaning “sound”. In physics it means a sound that comes back (in Kurdish, «دەنگدانەوە»). In computing, echo is a command that produces output.

«echo» لە بنەڕەتدا وشەیەکی یۆنانییە بە واتای «دەنگ». لە فیزیادا بە واتای دەنگێک دێت کە دەگەڕێتەوە («دەنگدانەوە»). لە echo فەرمانێکە بۆ دروستکردنی .

The echo in an empty roomدەنگدانەوە لە ژوورێکی بەتاڵدا

Why choose the word “echo”? When you speak in an empty room, the room sends your own voice back: you hear the same words you just said. In the computer it is the same: you give data to echo, and it gives the same data back to you, on the screen. For example, echo "Hello World!"; gives back Hello World!.

بۆچی وشەی «echo» هەڵبژێردرا؟ کاتێک لە ژوورێکی بەتاڵدا قسە دەکەیت، ژوورەکە دەنگی خۆتت بۆ دەگەڕێنێتەوە: هەمان ئەو وشانە دەبیستیت کە تۆزێک پێش ئێستا گوتت. لە هەروایە: تۆ دەدەیتە echo و ئەویش هەمان لەسەر پێدەداتەوە. بۆ نموونە echo "Hello World!"; دەقی Hello World! دەداتەوە.

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

A real echo only repeats exactly what you said. PHP's echo can also print results that PHP calculated, such as echo 2 + 3;, which prints 5, not “2 + 3”.

دەنگدانەوەی ڕاستەقینە تەنها هەمان ئەوەی دەیڵێیت دووبارە دەکاتەوە. echoـی PHP دەتوانێت ئەنجامی ژمێرکاری PHPیش بنووسێت، وەک echo 2 + 3; کە 5 دەنووسێت، نەک «⁦2 + 3⁩».

The echo statementڕستەی echo

The “Hello World” programبەرنامەی «سڵاو جیهان»

It is a common tradition in every programming language that your first program is the “Hello World” program: a program that only shows the words Hello World. In HTML you already wrote it with a <p> tag:

لە هەموو نەریتێکی باوە کە یەکەم «سڵاو جیهان» ⁦(Hello World Program)⁩ بێت: کە تەنها وشەکانی ⁦Hello World⁩ پیشان دەدات. لە HTMLـدا پێشتر بە <p> نووسیوتە:

hello.htmlHTMLRuns in the browserلە ناو تۆڕگەڕ کار دەکات
<html>
  <body>
    <p>Hello World</p>
  </body>
</html>

Your first PHP script does the same job. It contains both HTML and PHP, and the PHP part prints the <p> element:

یەکەم PHPـت هەمان کار دەکات. هەم HTML و هەم PHPـی تێدایە و بەشی PHPـەکە <p> دەنووسێت:

hello.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<html>
  <body>
    <?php
      echo "<p>Hello World</p>";
    ?>
  </body>
</html>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 4

echo prints the text <p>Hello World</p>, including the tag.

echo دەقی <p>Hello World</p> دەنووسێت، لەگەڵ .

The echo statement outputs everything between the quotation marks. Notice the two stages:

echo هەموو ئەوەی لە نێوان دەینووسێت. سەرنجی ئەم دوو قۆناغە بدە:

  1. PHP does not understand HTML. It does not know that <p> is an HTML tag and does not treat it as an instruction. It simply outputs it as plain text.
  2. The browser receives this text, recognises the HTML tag, and displays it by the rules of HTML. So you, the user, see only “Hello World” on the screen.
  1. PHP لە HTML تێناگات. نازانێت <p> HTMLـە و وەک ڕێنمایی سەیری ناکات. تەنها وەک دەقێکی سادە دەینووسێت.
  2. ئەم دەقە وەردەگرێت، HTMLـەکە دەناسێتەوە و بەپێی یاساکانی HTML پیشانی دەدات. بۆیە تۆی تەنها «⁦Hello World⁩» لەسەر دەبینیت.
Quick checkپشکنینی خێراPHP runs echo "<p>Hello World</p>";. Who turns <p> into a paragraph on the screen?PHP echo "<p>Hello World</p>"; جێبەجێ دەکات. کێ <p> دەکات بە بڕگەیەک لەسەر ؟

Printing several items with one echoنووسینی چەند بڕگەیەک بە یەک echo

The Kurdish notes call echo an output statement («ڕستەی دەرخستە»): it puts output on the screen. It is made of the keyword echo followed by any number of items, separated by commas:

تێبینییە کوردییەکان بە echo دەڵێن : دەخاتە سەر . لە echo پێکدێت کە هەر ژمارەیەک بڕگە ⁦(item)⁩ بەدوایدا دێت و بە کۆما لێک جیا دەکرێنەوە:

The form of an echo statementشێوەی ڕستەی echoTEXT
echo item1, item2, item3, ...;

Pay attention to these rules:

ئاگات لەم یاسایانە بێت:

  1. Each item is a , a or a number. A number can be whole () or fractional (). A string is a row of characters, and it may contain digits too.
  2. Strings must be written inside single quotation marks ' ' or double quotation marks " ". Numbers need no quotes.
  3. The number of items is unlimited.
  4. Items are separated by commas. echo puts no space between the items in the output.
  5. If you want a space in the output, add it yourself as a character inside a string, for example the item " ".
  1. هەر بڕگەیەک ، یان ژمارەیەکە. ژمارە دەکرێت تەواو () یان کەرتی () بێت. ڕیزێکە لە و دەکرێت ژمارەشی تێدا بێت.
  2. دەبێت لەنێو تاک ' ' یان جووت " "دا بنووسرێن. ژمارەکان پێویستیان بە نییە.
  3. ژمارەی بڕگەکان بێسنوورە.
  4. بڕگەکان بە کۆما لێک جیا دەکرێنەوە. echo لە هیچ لە نێوان بڕگەکاندا دانانێت.
  5. ئەگەر لە دەوێت، خۆت وەک لەناو زیادی بکە، بۆ نموونە بڕگەی " ".

Printing several items with one echoنووسینی چەند بڕگەیەک بە یەک echo

Examples from the bookنموونەکانی کتێبەکە

The Kurdish notes include a list of examples from the book PHP for Dummies (Table 3-2, “echo Statements”). Here it is again, with the output checked in PHP 8.5:

تێبینییە کوردییەکان خشتەیەکی نموونە لە کتێبی ⁦PHP for Dummies⁩ـەوە دەهێنن («⁦Table 3-2⁩», ⁦echo Statements⁩). لێرەدا دیسان دەیخەینە ڕوو و بە ⁦PHP 8.5⁩ پشکنراوە:

echo statements and their output (from PHP for Dummies, Table 3-2, verified) echo و (لە ⁦PHP for Dummies⁩، ⁦Table 3-2⁩، پشکنراو)
echo statement echoOutputWhyبۆچی
echo 123;123A number needs no quotes.ژمارە پێویستی بە نییە.
echo "Hello World!";Hello World!One string item.یەک بڕگەی .
echo "Hello","World!";HelloWorld!Two items; echo adds no space between them.دوو بڕگە؛ echo هیچ لە نێوانیاندا دانانێت.
echo "Hello"," ","World!";Hello World!The middle item is a string with one space.بڕگەی ناوەڕاست کە یەک تێدایە.
echo Hello World!;ErrorNot valid: the text is not in quotes.دروست نییە: دەقەکە لەنێو نییە.
echo 'Hello World!';Hello World!Single quotes work too. تاکیش کار دەکات.
The rows of the list, run togetherڕیزەکانی خشتەکە، پێکەوە کار پێکراوitems.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
echo 123;
echo "\n---\n";
echo "Hello World!";
echo "\n---\n";
echo "Hello","World!";
echo "\n---\n";
echo "Hello"," ","World!";
echo "\n---\n";
echo 'Hello World!';
echo "\n---\n";
echo "Hello", " ", 2026, " ", 3.5;
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2,4,6,8,10

These lines only print a separator --- between the examples, so you can see each output on its own line. \n is a new line (explained below).

ئەم دێڕانە تەنها جیاکەرەوەی --- لە نێوان نموونەکاندا دەنووسن تا هەر لە دێڕی خۆیدا ببینیت. \n دێڕێکی نوێیە (لە خوارەوە ڕوون کراوەتەوە).

Lineدێڕی 11

Items can be strings and numbers mixed: a string, a space, an integer, a space, a float.

بڕگەکان دەکرێت تێکەڵەیەک بن لە و ژمارە: ، ، ، ، .

And the row marked “not valid”. Without quotes, PHP tries to read Hello and World as names and stops:

و ئەو ڕیزەی کە نیشانەی «دروست نییە»ی لەسەرە. بەبێ ، PHP هەوڵ دەدات Hello و World وەک ناو بخوێنێتەوە و ڕادەوەستێت:

noquotes.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
echo Hello World!;
Quick checkپشکنینی خێراWhat does echo "Erbil", "Duhok"; print?echo "Erbil", "Duhok"; چی دەنووسێت؟

Printing several items with one echoنووسینی چەند بڕگەیەک بە یەک echo

Quotes: straight, not curlyنیشانەی گوتە: ڕاست، نەک چەماوە

Code needs straight quotation marks: " and '. Word processors, slide programs and phones often change them into curly quotes “ ” ‘ ’, which PHP does not accept. Many examples in the course materials were printed with curly quotes, so they fail if you copy them. Always type code in a code editor.

کۆد پێویستی بە ڕاست هەیە: " و '. نووسین، سلاید و مۆبایل زۆرجار دەیانگۆڕن بۆ نیشانەی چەماوەی “ ” ‘ ’، کە PHP وەریان ناگرێت. زۆرێک لە نموونەکانی کەرەستەکانی کۆرسەکە بە نیشانەی چەماوە چاپ کراون، بۆیە ئەگەر کۆپییان بکەیت سەرناکەون. هەمیشە کۆد لە دەستکاریکەرێکی کۆددا بنووسە.

To print a quotation mark inside a string, use the other kind of quote around it, or put a backslash before it, like \". This is very useful for HTML attributes:

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

quotes.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
echo "<a href='contact.php'>Contact us</a>";
echo "<br>";
echo '<a href="about.php">About us</a>';
echo "<br>";
echo "She said \"Hello\".";
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

Double quotes outside, single quotes for the HTML attribute inside.

نیشانەی جووت لە دەرەوە، نیشانەی تاک بۆ HTML لە ناوەوە.

Lineدێڕی 4

Single quotes outside, double quotes inside.

نیشانەی تاک لە دەرەوە، نیشانەی جووت لە ناوەوە.

Lineدێڕی 6

\" is an escaped double quote: it is printed as ".

\" نیشانەیەکی جووتی escape کراوە: وەک " دەنووسرێت.

Joining strings with the dotپێکەوەلکاندنی زنجیرەنووسە بە خاڵ

The dot . is the operator: it joins two strings into one string. Lecture 1, Lab 1 and the summary all show it:

خاڵ . ـە: دوو دەکات بە یەک . وانەی 1، تاقیگەی 1 و کورتەکە هەموویان پیشانی دەدەن:

join.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
echo "Hello" . " World!";
echo "<br>";
echo "Hello"."world";
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

Lecture 1: the space is inside the second string, " World!", so the result is Hello World!.

وانەی 1: لەناو دووەمدایە، " World!"، بۆیە ئەنجامەکە Hello World!ـە.

Lineدێڕی 3

<br> makes a line break in the browser, so the two results appear on two lines.

<br> لە دێڕ دەشکێنێت، بۆیە دوو ئەنجامەکە لە دوو دێڕدا دەردەکەون.

Lineدێڕی 4

Lab 1, Exercise 3: no space anywhere, so the words are glued: Helloworld.

تاقیگەی 1، ڕاهێنانی 3: هیچ نییە، بۆیە وشەکان پێکەوە نووساون: Helloworld.

Quick checkپشکنینی خێراWhat does echo "Hello"."world"; print?echo "Hello"."world"; چی دەنووسێت؟

Joining strings with the dotپێکەوەلکاندنی زنجیرەنووسە بە خاڵ

Comma or dot?کۆما یان خاڵ؟

Both of these lines print the same text, but they work differently. With commas, echo prints three separate items. With dots, PHP first builds one string and echo prints that one string.

هەردوو ئەم دێڕانە هەمان دەق دەنووسن، بەڵام جیاواز کار دەکەن. بە کۆما، echo سێ بڕگەی جیا دەنووسێت. بە خاڵ، PHP سەرەتا یەک دروست دەکات و echo ئەو یەک دەنووسێت.

comma-dot.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
echo "Erbil", " ", "Duhok";
echo "<br>";
echo "Erbil" . " " . "Duhok";
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

Three items separated by commas.

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

Lineدێڕی 4

Three strings joined with dots into one string.

سێ کە بە خاڵ کراون بە یەک .

Joining strings with the dotپێکەوەلکاندنی زنجیرەنووسە بە خاڵ

echo with arithmeticecho لەگەڵ ژمێرکاری

echo can also print the result of a calculation. PHP first calculates the , then echo prints the result:

echo دەتوانێت ئەنجامی ژمێرکارییەکیش بنووسێت. PHP سەرەتا دەژمێرێت، پاشان echo ئەنجامەکە دەنووسێت:

sum.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
echo 2+3;
echo "<br>";
echo "2 + 3 = ", 2 + 3;
echo "<br>";
echo "Total: " . 2 + 3;
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

Lecture 1: 2+3 is calculated, and echo prints 5.

وانەی 1: 2+3 دەژمێردرێت و echo ژمارەی 5 دەنووسێت.

Lineدێڕی 4

Two items: a string and the result of 2 + 3.

دوو بڕگە: و ئەنجامی 2 + 3.

Lineدێڕی 6

Careful: + is done before . (since PHP 8), so this is "Total: " . 5.

ئاگاداربە: + پێش . ئەنجام دەدرێت (لە ⁦PHP 8⁩ـەوە)، بۆیە ئەمە دەبێتە "Total: " . 5.

The print statementڕستەی print

print also sends output to the page. Lecture 1 shows it as another way to write Hello World:

printیش دەنێرێت بۆ لاپەڕەکە. وانەی 1 وەک ڕێگەیەکی تری نووسینی ⁦Hello World⁩ پیشانی دەدات:

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

print is also a language construct, like echo. But it has two important differences:

printیش وەک echo ە. بەڵام دوو جیاوازیی گرنگی هەیە:

  • print takes exactly one . You cannot give it several items with commas.
  • print returns 1. Because it gives back a value, it can be used inside an , for example on the right side of =. echo returns nothing and cannot be used like that.
  • print تەنها یەک وەردەگرێت. ناتوانیت بە کۆما چەند بڕگەیەکی بدەیتێ.
  • print ژمارە 1 دەگەڕێنێتەوە. لەبەر ئەوەی دەداتەوە، دەتوانرێت لەناو بەکار بێت، بۆ نموونە لە لای ڕاستی =. echo هیچ ناگەڕێنێتەوە و ناتوانرێت بەو شێوەیە بەکار بێت.

echo or print?echo یان print؟

The summary, a supplement image in Lecture 2, the Kurdish notes and the slides all compare the two. Here is everything in one place:

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

echo and print comparedبەراوردی echo و print
Pointخاڵechoprint
What it isچییەA language constructA language construct
Return valueReturns nothingهیچ ناگەڕێنێتەوەReturns 11 دەگەڕێنێتەوە
Number of argumentsژمارەی One or more, separated by commasیەک یان زیاتر، بە کۆما لێک جیا کراونەتەوەExactly oneتەنها یەک
Inside an expressionلەناو Not possibleناکرێتPossible (because it returns 1)دەکرێت (چونکە 1 دەگەڕێنێتەوە)
ParenthesesکەوانەOptionalئارەزوومەندانەOptionalئارەزوومەندانە
SpeedخێراییVery slightly fasterزۆر کەمێک خێراترەVery slightly slower; the difference is too small to noticeزۆر کەمێک خاوترە؛ جیاوازییەکە ئەوەندە بچووکە کە هەست ناکرێت
Quick checkپشکنینی خێراWhat does $x = print "Hi"; store in $x?$x = print "Hi"; چی لە $xدا هەڵدەگرێت؟

Quick checkپشکنینی خێراWhich line is a parse error?کام دێڕ ؟

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

Explains the PHP opening and closing tags (and why to leave out ?> in pure PHP files), and the kinds of PHP comments.

کردنەوە و داخستنی ڕوون دەکاتەوە، بۆچی لەو تەنها تێدایە ?> نانووسین و جۆرەکانی لە .

Watch 3:50 to 5:18: the 'Print vs Echo' chapter.

لە ⁦3:50⁩ تا ⁦5:18⁩ سەیر بکە: بەشی Print vs Echo، واتە جیاوازیی و .

New lines: n and <br>دێڕی نوێ: n و ⁦<br>⁩

Some characters have a special meaning inside a double-quoted string. They start with a backslash and are called . A very common one is \n, which means new line. The letter n comes from the word new: “new line”.

هەندێک لەناو واتای تایبەتیان هەیە. بە لارەهێڵی پێچەوانە دەست پێدەکەن و پێیان دەگوترێت . یەکێک لە باوترینیان \nـە، کە واتای دێڕی نوێیە. پیتی n لە وشەی newـەوە هاتووە: «⁦new line⁩» (دێڕی نوێ).

newline.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
	echo "Hello\n World";
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

\n starts a new line in the output. The space before World is printed too.

\n لە دێڕێکی نوێ دەست پێدەکات. پێش Worldیش دەنووسرێت.

New lines: n and <br>دێڕی نوێ: n و ⁦<br>⁩

Two stages: what PHP sends and what the browser showsدوو قۆناغ: ئەوەی PHP دەینێرێت و ئەوەی تۆڕگەڕ پیشانی دەدات

This is the most important idea of this section. \n tells PHP to start a new line in the output (the HTML source of the page), but it does not make a new line on the web page. To break a line on the page you must send the HTML tag <br>.

ئەمە گرنگترین بیرۆکەی ئەم بەشەیە. \n بە PHP دەڵێت دێڕێکی نوێ لە دەست پێبکات (HTMLـی لاپەڕەکە)، بەڵام لەسەر دێڕی نوێ دروست ناکات. بۆ شکاندنی دێڕ لەسەر لاپەڕەکە دەبێت HTMLـی <br> بنێریت.

Three rows, each with PHP code, what PHP sends, and what the browser shows. echo "HellonWorld!" sends two lines but the browser shows Hello World! on one line. echo "Hello<br>World!" sends one line but the browser shows Hello and World! on two lines. echo "Hello <br>nWorld!" sends two readable lines and the browser shows two lines.سێ ڕیز، هەر یەکەیان کۆدی PHP، ئەوەی PHP دەینێرێت و ئەوەی تۆڕگەڕ پیشانی دەدات. echo "HellonWorld!" دوو دێڕ دەنێرێت بەڵام تۆڕگەڕ Hello World! لە یەک دێڕدا پیشان دەدات. echo "Hello<br>World!" یەک دێڕ دەنێرێت بەڵام تۆڕگەڕ Hello و World! لە دوو دێڕدا پیشان دەدات. echo "Hello <br>nWorld!" دوو دێڕی ڕوون دەنێرێت و تۆڕگەڕ دوو دێڕ پیشان دەدات.
Stage 1 is the text that PHP sends; stage 2 is how the browser displays it.قۆناغی یەکەم ئەو دەقەیە کە PHP دەینێرێت؛ قۆناغی دووەم ئەوەیە کە چۆن پیشانی دەدات.CêIT

The Kurdish notes include the book's Table 3-3, “Stages of Web Page Delivery”. Here it is, checked with PHP 8.5:

تێبینییە کوردییەکان «⁦Table 3-3⁩»ی کتێبەکە دەهێنن، «قۆناغەکانی گەیاندنی ». لێرەدا بە ⁦PHP 8.5⁩ پشکنراوە:

Stages of web page delivery (from PHP for Dummies, Table 3-3, verified)قۆناغەکانی گەیاندنی (لە ⁦PHP for Dummies⁩، ⁦Table 3-3⁩، پشکنراو)
echo statement echoStage 1: PHP output (page source)قۆناغی ١: PHP (سەرچاوەی لاپەڕە)Stage 2: web page displayقۆناغی ٢: پیشاندان لە
echo "Hello World!";Hello World!Hello World!
echo "Hello"; echo "World!";HelloWorld!HelloWorld!
echo "Hello\nWorld!";Hello World!Hello World!
echo "Hello<br>World!";Hello<br>World!Hello, then World! on the next lineHello، پاشان ⁦World!⁩ لە دێڕی دواتردا
echo "Hello <br>\nWorld!";Hello <br> World!Hello, then World! on the next lineHello، پاشان ⁦World!⁩ لە دێڕی دواتردا
All five rows, run togetherهەر پێنج ڕیزەکە، پێکەوە کار پێکراوstages.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
echo "Hello World!";
echo "\n=====\n";
echo "Hello";
echo "World!";
echo "\n=====\n";
echo "Hello\nWorld!";
echo "\n=====\n";
echo "Hello<br>World!";
echo "\n=====\n";
echo "Hello <br>\nWorld!";
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3,6,8,10

Separator lines, only to make the output easy to read.

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

br.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
echo "<p>Hello<br> World!</p>";
Quick checkپشکنینی خێراYou print "Hello\nWorld". What does the visitor see in the browser?"Hello\nWorld" دەنووسیت. سەردانیکەر لە چی دەبینێت؟

New lines: n and <br>دێڕی نوێ: n و ⁦<br>⁩

Where do the spaces come from?بۆشاییەکان لە کوێوە دێن؟

  • Row 1 has a space inside the string, so the space is printed.
  • Row 2 has two echo statements with no space in either string, so no space appears: HelloWorld!.
  • Row 3 has no space in the code, but the page shows one. The browser turned the line break into a space, because in HTML a line break is displayed as a single space.
  • ڕیزی 1 لەناو هەیە، بۆیە دەنووسرێت.
  • ڕیزی 2 دوو echoـی هەیە و لە هیچکامیاندا نییە، بۆیە هیچ دەرناکەوێت: HelloWorld!.
  • ڕیزی 3 لە کۆدەکەدا نییە، بەڵام لاپەڕەکە یەکێک پیشان دەدات. دێڕبڕینەکەی کرد بە ، چونکە لە HTMLـدا دێڕبڕین وەک یەک پیشان دەدرێت.

New lines: n and <br>دێڕی نوێ: n و ⁦<br>⁩

Use n generouslyn بە دەستی کراوە بەکار بهێنە

The book gives this advice: use \n generously. Without it, all the HTML your PHP prints ends up on one very long line, for example when you print a long form. Such a page is hard to debug when it looks wrong, and some browsers have trouble with very long lines. Compare the page source of these two scripts:

کتێبەکە ئەم ئامۆژگارییە دەدات: \n بە دەستی کراوە بەکار بهێنە. بەبێ ئەو، هەموو ئەو HTMLـەی PHP دەینووسێت دەکەوێتە یەک دێڕی زۆر درێژەوە، بۆ نموونە کاتێک درێژ دەنووسیت. لاپەڕەیەکی وا کاتێک بە دەردەکەوێت قورسە و هەندێک کێشەیان لەگەڵ دێڕی زۆر درێژدا هەیە. لاپەڕەی ئەم دوو بەراورد بکە:

list-oneline.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
echo "<ul>";
echo "<li>Erbil</li>";
echo "<li>Sulaimani</li>";
echo "<li>Duhok</li>";
echo "</ul>";
list-lines.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
echo "<ul>\n";
echo "  <li>Erbil</li>\n";
echo "  <li>Sulaimani</li>\n";
echo "  <li>Duhok</li>\n";
echo "</ul>\n";

Printing HTML tagsنووسینی تاگەکانی HTML

Because the browser reads PHP's output as HTML, you can print HTML tags with echo. This is Example 1 of Lab 2, “Embedding HTML tag with PHP code”:

لەبەر ئەوەی PHP وەک HTML دەخوێنێتەوە، دەتوانیت بە echo HTML بنووسیت. ئەمە نموونەی 1ی تاقیگەی 2یە، «⁦Embedding HTML tag with PHP code⁩»:

headings.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
// Displaying HTML code
echo "<h4>This is a simple heading.</h4>";
echo "<h4 style='color: red;'>This is heading with style.</h4>";
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

A comment.

.

Lineدێڕی 3

Prints a level-4 heading.

سەردێڕێکی ئاستی 4 دەنووسێت.

Lineدێڕی 4

The style attribute is inside single quotes, because the whole string uses double quotes.

style لەناو نیشانەی تاکدایە، چونکە هەموو نیشانەی جووت بەکار دەهێنێت.

Quick checkپشکنینی خێراA student's first PHP page prints echo 'My name is <h1>Aram</h1> and I am 20 years old';. What does the browser show?یەکەم لاپەڕەی PHPـی خوێندکارێک ئەمە دەنووسێت: echo 'My name is <h1>Aram</h1> and I am 20 years old';. چی پیشان دەدات؟

PHP Programming Language Tutorial - Full CoursefreeCodeCamp.org · Englishئینگلیزی · 4.2M viewsبینین · 72K likesلایک · Partبەشی 20:29–27:30 (ofلە 4:36:39)

Watch PHP print HTML tags with echo, so the browser shows headings and lines that your PHP code made.

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

Watch 20:29 to 27:30, the 'Writing HTML' chapter of a full course.

لە ⁦20:29⁩ تا ⁦27:30⁩ سەیر بکە، بەشی Writing HTML لە کۆرسێکی تەواو.

Printing variablesنووسینی گۆڕاوەکان

A is a named place that holds a value; its name starts with $. You will learn variables properly in Variables. Here we only print them. A variable can be one of the items of echo:

شوێنێکی ناونراوە کە هەڵدەگرێت؛ ناوەکەی بە $ دەست پێدەکات. بە تەواوی لە وانەی گۆڕاوەکان فێر دەبیت. لێرەدا تەنها دەیاننووسین. دەتوانێت یەکێک بێت لە بڕگەکانی echo:

name.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
	$firstName = "Dilan";
	$lastName = "Kamal";
	echo "My name is ", $firstName, " ", $lastName;
	echo "\n";
	echo "My name is ", $firstName, $lastName;
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2-3

Two variables that hold the first name and the last name.

دوو کە ناوی یەکەم و ناوی کۆتایی هەڵدەگرن.

Lineدێڕی 4

Four items: a string, a variable, a string with one space, a variable.

چوار بڕگە: ، ، کە یەک تێدایە، .

Lineدێڕی 5

A new line in the output.

دێڕێکی نوێ لە .

Lineدێڕی 6

Without the " " item, the names are glued together.

بەبێ بڕگەی " "، ناوەکان بە یەکەوە دەنووسێن.

Statements with more than one variable must follow some rules. The Kurdish notes show them with the book's Table 4-1, “echo Statements with Variables”. It assumes $number = 123, $word1 = "Hello" and $word2 = "World!":

ئەگەر زیاتر لە یەک تێدا بێت، دەبێت ڕەچاوی هەندێک یاسا بکات. تێبینییە کوردییەکان بە «⁦Table 4-1⁩»ی کتێبەکە پیشانیان دەدەن، «⁦echo Statements with Variables⁩». وای دادەنێت کە $number = 123 و $word1 = "Hello" و $word2 = "World!":

echo statements with variables (from PHP for Dummies, Table 4-1, verified) echo لەگەڵ (لە ⁦PHP for Dummies⁩، ⁦Table 4-1⁩، پشکنراو)
echo statement echoOutputRuleیاسا
echo $number;123A variable alone.تەنها .
echo $word1,$word2;HelloWorld!Commas separate the items; no space is added.کۆما بڕگەکان جیا دەکاتەوە؛ هیچ زیاد ناکرێت.
echo $word1," ",$word2;Hello World!Add the space as an item of its own. وەک بڕگەیەکی جیا دابنێ.
echo $word1 $word2;ErrorNot valid: no comma separates the variables.دروست نییە: هیچ کۆمایەک جیا ناکاتەوە.
echo "$word1 $word2 now";Hello World! nowInside double quotes, variables are replaced by their values.لەناو نیشانەی جووتدا بە دەگۆڕدرێن.
variables-echo.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$number = 123;
$word1 = "Hello";
$word2 = "World!";
echo $number;
echo "\n---\n";
echo $word1,$word2;
echo "\n---\n";
echo $word1," ",$word2;
echo "\n---\n";
echo "$word1 $word2 now";
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 11

Double quotes replace $word1 and $word2 with their values. This is called ; see Strings.

نیشانەی جووت $word1 و $word2 بە دەگۆڕێت. پێی دەگوترێت ؛ بڕوانە زنجیرەنووسەکان.

The row marked “not valid”ئەو ڕیزەی نیشانەی «دروست نییە»ی لەسەرەno-comma.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$word1 = "Hello";
$word2 = "World!";
echo $word1 $word2;

Printing variablesنووسینی گۆڕاوەکان

The short echo tag <?=تاگی کورتی ⁦echo: <?⁩=

When you only need to print one value inside HTML, there is a shortcut: <?= value ?> means exactly <?php echo value; ?>. It always works in PHP 8 and is popular in HTML templates.

کاتێک تەنها پێویستت بە نووسینی یەک لەناو HTMLـدا هەیە، ڕێگەیەکی کورت هەیە: <?= value ?> ڕێک واتای <?php echo value; ?>ـە. لە ⁦PHP 8⁩دا هەمیشە کار دەکات و لە HTMLـدا باوە.

city.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php $city = "Halabja"; ?>
<p>City: <?= $city ?></p>
<p>Same as: <?php echo $city; ?></p>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

<?= $city ?> prints the value of $city.

<?= $city ?> $city دەنووسێت.

Lineدێڕی 3

The long form does the same.

شێوە درێژەکە هەمان کار دەکات.

Printing a whole block with heredocنووسینی بلۆکێکی تەواو بە heredoc

Printing a long piece of HTML with many echo lines is tiring. A lets you write a whole block of text, over many lines, as one string. It starts with <<< and a name you choose (often END or HTML), and it ends with the same name on its own line, followed by ;. A heredoc works like a double-quoted string: variables inside it are replaced by their values, and you can use " freely.

نووسینی پارچەیەکی درێژی HTML بە چەندین دێڕی echo ماندووکەرە. ڕێگەت پێدەدات بلۆکێکی تەواوی دەق، لە چەندین دێڕدا، وەک یەک بنووسیت. بە <<< و ناوێک کە خۆت هەڵیدەبژێریت دەست پێدەکات (زۆرجار END یان HTML) و بە هەمان ناو لە دێڕێکی جیادا کۆتایی دێت، کە ;ی بەدوادا دێت. heredoc وەک کار دەکات: ناوی بە دەگۆڕدرێن و دەتوانیت بە ئازادی " بەکار بهێنیت.

shop.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$shop = "Hana's Bookshop";
echo <<<END
<h2>Welcome to $shop</h2>
<p>We are open every day from 9 to 5.</p>
<p>Ask for our "book of the week".</p>
END;
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

<<<END starts the heredoc.

<<<END heredoc دەست پێدەکات.

Lineدێڕی 4

$shop is replaced by its value.

$shop بە دەگۆڕدرێت.

Lineدێڕی 6

Double quotes need no backslash inside a heredoc.

نیشانەی جووت لەناو heredocـدا پێویستی بە لارەهێڵی پێچەوانە نییە.

Lineدێڕی 7

END; on its own line ends it. Since PHP 7.3 this line may be indented.

END; لە دێڕێکی جیادا کۆتایی پێدەهێنێت. لە ⁦PHP 7.3⁩ـەوە ئەم دێڕە دەکرێت لە پێشەوە بێت.

If you write the name in single quotes, <<<'END', you get a nowdoc. It works like a single-quoted string: nothing inside is replaced.

ئەگەر ناوەکە لەنێو نیشانەی تاکدا بنووسیت، <<<'END'، nowdoc دروست دەبێت. وەک کار دەکات: هیچ شتێکی ناوی ناگۆڕدرێت.

nowdoc.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$shop = "Hana's Bookshop";
echo <<<'END'
<p>In a nowdoc, $shop is printed exactly as written.</p>
END;
Quick checkپشکنینی خێرا$city = "Erbil"; Which one prints Erbil and not $city?$city = "Erbil"; کامیان Erbil دەنووسێت، نەک $city؟

PHP String Data Type - Heredoc & Nowdoc Syntax - Full PHP 8 TutorialProgram With Gio · Englishئینگلیزی · 82K viewsبینین · 3K likesلایک

Learn single- and double-quoted strings, then how heredoc and nowdoc let you write a long text over many lines.

فێری و دەبیت، پاشان دەبینیت چۆن و nowdoc ڕێگەت پێدەدەن دەقێکی درێژ لە چەند دێڕێکدا بنووسیت.

The first part reviews quotes; heredoc and nowdoc come in the second half.

بەشی یەکەم دووبارە باسی گوتەکان دەکاتەوە؛ و nowdoc لە نیوەی دووەمدا دێن.

Printing a whole block with heredocنووسینی بلۆکێکی تەواو بە heredoc

The slide example, correctedنموونەکەی سلایدەکە، ڕاستکراوە

The course deck shows a picture captioned “Printing a whole document with heredoc”. Its code, with the curly quotes made straight, is:

سلایدەکانی کۆرسەکە وێنەیەک پیشان دەدەن بە ناونیشانی «⁦Printing a whole document with heredoc⁩». کۆدەکەی، دوای ڕاستکردنەوەی نیشانە چەماوەکان، ئەمەیە:

dinner.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
 $channel =" <?XML
<channel>
<title>What's For Dinner</title>
<link>http://menu.example.com/ </link>
<description>Choose what to eat
tonight.</description>
</channel>";

echo <<<END
This uses the "document"no extra whitespace!
END;

 print "<br>";
 print $channel; ?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2-8

This is not a heredoc. It is an ordinary double-quoted string that happens to cover several lines.

ئەمە heredoc نییە. ئاساییە کە بەڕێکەوت چەند دێڕێک دەگرێتەوە.

Lineدێڕی 10-12

This is the only heredoc: one line of text between <<<END and END;.

ئەمە تاکە heredocـەکەیە: یەک دێڕی دەق لە نێوان <<<END و END;دا.

Lineدێڕی 14-15

print a <br>, then the string from line 2.

print <br> دەنووسێت، پاشان دێڕی 2.

xml-as-text.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$channel = <<<XML
<channel>
  <title>What's For Dinner</title>
  <link>http://menu.example.com/</link>
</channel>
XML;
echo "<pre>", htmlspecialchars($channel), "</pre>";
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2-7

A heredoc that holds a small XML document. XML is just the name chosen for the marker.

heredocێک کە بەڵگەنامەیەکی بچووکی XMLـی تێدایە. XML تەنها ئەو ناوەیە کە بۆ نیشانەکە هەڵبژێردراوە.

Lineدێڕی 8

htmlspecialchars() changes < into &lt; and > into &gt;, so the browser shows the tags instead of reading them. <pre> keeps the line breaks.

htmlspecialchars() نیشانەی < دەگۆڕێت بۆ &lt; و > بۆ &gt;، بۆیە پیشان دەدات لە جیاتی ئەوەی بیانخوێنێتەوە. <pre> دێڕبڕینەکان دەپارێزێت.

The echo table from the course notesخشتەی echo لە تێبینییەکانی کۆرسەکەوە

The course notes “PHP (Ch2) – Variables” contain an Echo Table: short examples and their output. There are two versions of the notes. The newer version changed row 7 and added rows 12–16. Here are all 16 rows, checked one by one with PHP 8.5. The table assumes $var1 = "Var1Value", $var2 = "Var2Value", and a function func1() that returns "Func1Value". Some rows use ideas that are taught in later lectures (variables, constants, strings); the last column tells you where.

تێبینییەکانی کۆرسەکە «⁦PHP (Ch2) – Variables⁩» خشتەی Echoیان تێدایە: نموونەی کورت و . دوو وەشانی تێبینییەکان هەن. وەشانە نوێیەکە ڕیزی 7ی گۆڕی و ڕیزەکانی ⁦12–16⁩ی خستە سەری. لێرەدا هەر ⁦16⁩ ڕیزەکە هەن، کە یەک بە یەک بە ⁦PHP 8.5⁩ پشکنراون. خشتەکە وای دادەنێت کە $var1 = "Var1Value" و $var2 = "Var2Value" و func1() هەیە کە "Func1Value" دەگەڕێنێتەوە. هەندێک ڕیز بیرۆکەگەلێک بەکار دەهێنن کە لە وانەکانی داهاتوودا دەخوێنرێن (، ، )؛ ستوونی کۆتایی پێت دەڵێت لە کوێ.

The Echo Table, verified with PHP 8.5خشتەی Echo، پشکنراو بە ⁦PHP 8.5⁩
#ExampleنموونەOutput in PHP 8 لە ⁦PHP 8⁩داWhyبۆچی
1echo $var1.$var2;Var1ValueVar2ValueThe dot joins the two strings, with no space.خاڵ دوو پێکەوە دەبەستێت، بەبێ .
2$var1 .= $var2; echo $var1;Var1ValueVar2Value.= adds $var2 to the end of $var1. It adds no space. (The notes wrongly show a space.).= $var2 دەخاتە کۆتایی $var1. هیچ زیاد ناکات. (تێبینییەکان بە پیشان دەدەن.)
3echo $var1.func1();Var1ValueFunc1ValueThe value returned by the function is joined to $var1. See Functions.ئەو دەیگەڕێنێتەوە بە $var1ـەوە دەلکێت. بڕوانە کردارەکان.
4echo $var1." ".$var2;Var1Value Var2ValueA string with one space is joined between them. کە یەک تێدایە لە نێوانیاندا دەلکێت.
5echo "$var1";Var1ValueDouble quotes replace a variable with its value.نیشانەی جووت بە دەگۆڕێت.
6echo '$var1';$var1Single quotes print the text exactly as written.نیشانەی تاک دەقەکە ڕێک وەک نووسراوە دەنووسێت.
7define("Cons", 2); echo cons;Error: Undefined constant "cons": ⁦Undefined constant "cons⁩"Constant names are case-sensitive: cons is not Cons. (The old notes say it prints “cons”; that was PHP 7 behaviour.) See Constants.ناوی هەستیارن: cons هەمان Cons نییە. (تێبینییە کۆنەکان دەڵێن «cons» دەنووسێت؛ ئەمە ڕەفتاری ⁦PHP 7⁩ بوو.) بڕوانە نەگۆڕەکان.
8define("cons", 2); echo cons;2The same name in the same case: the value 2 is printed.هەمان ناو بە هەمان شێوەی پیت: 2 دەنووسرێت.
9echo "<pre>Hello\tWorld</pre>";Hello, a tab, WorldHello، تابێک، WorldIn double quotes \t is a tab; <pre> makes the browser show it.لەناو نیشانەی جووتدا \t تابێکە؛ <pre> وا دەکات پیشانی بدات.
10echo '<pre>Hello\tWorld</pre>';Hello\tWorldIn single quotes \t is not special: a backslash and a t are printed.لەناو نیشانەی تاکدا \t تایبەت نییە: لارەهێڵێکی پێچەوانە و پیتی t دەنووسرێن.
11echo 'I\'ll do it\nH';I'll do it\nHIn single quotes only \' (and \\) are special, so \n stays as it is.لەناو نیشانەی تاکدا تەنها \' (و \\) تایبەتن، بۆیە \n وەک خۆی دەمێنێتەوە.
12$var = 4; print($var = 5);5An assignment has a value: $var = 5 is 5, and print prints it. خۆی هەیە: $var = 5 دەکاتە 5 و print دەینووسێت.
13echo true;1true is printed as 1. See Data types.true وەک 1 دەنووسرێت. بڕوانە جۆرەکانی دراوە.
14echo !false;1!false is true, which is printed as 1.!false دەکاتە true، کە وەک 1 دەنووسرێت.
15echo false;(nothing)(هیچ)false is printed as an empty string.false وەک بەتاڵ دەنووسرێت.
16echo null;(nothing)(هیچ)null is printed as an empty string.null وەک بەتاڵ دەنووسرێت.
Rows 1–6 and 8–16 in one scriptڕیزەکانی ⁦1–6⁩ و ⁦8–16⁩ لە یەک دەسنووسداecho-table.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
function func1() {
    return "Func1Value";
}
$var1 = "Var1Value";
$var2 = "Var2Value";

echo "1: ", $var1.$var2, "\n";
$copy = $var1;
$copy .= $var2;
echo "2: ", $copy, "\n";
echo "3: ", $var1.func1(), "\n";
echo "4: ", $var1." ".$var2, "\n";
echo "5: ", "$var1", "\n";
echo "6: ", '$var1', "\n";
define("cons", 2);
echo "8: ", cons, "\n";
echo "9: ", "<pre>Hello\tWorld</pre>", "\n";
echo "10: ", '<pre>Hello\tWorld</pre>', "\n";
echo "11: ", 'I\'ll do it\nH', "\n";
$var = 4;
echo "12: "; print($var = 5); echo "\n";
echo "13: ", true, "\n";
echo "14: ", !false, "\n";
echo "15: [", false, "]\n";
echo "16: [", null, "]\n";
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2-6

The function and the two variables that the table assumes.

ئەو و دوو خشتەکە وایان دادەنێت.

Lineدێڕی 9-11

Row 2 works on a copy, so $var1 keeps its value for the next rows.

ڕیزی 2 لەسەر کۆپییەک کار دەکات، بۆیە $var1 خۆی بۆ ڕیزەکانی دواتر دەپارێزێت.

Lineدێڕی 17-18

Row 8. Row 7 is left out here because it stops the script with an error (see below).

ڕیزی 8. ڕیزی 7 لێرەدا دانەنراوە چونکە بە ڕادەگرێت (بڕوانە خوارەوە).

Lineدێڕی 27-28

Rows 15 and 16 print between brackets, so you can see that the output is empty.

ڕیزەکانی ⁦15⁩ و ⁦16⁩ لە نێوان کەوانەی چوارگۆشەدا دەنووسرێن، تا ببینیت کە بەتاڵە.

Row 7 in PHP 8ڕیزی 7 لە ⁦PHP 8⁩داrow7.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
define("Cons", 2);
echo cons;
Quick checkپشکنینی خێراWhat is printed by echo false;?echo false; چی دەنووسێت؟

Common mistakesهەڵە باوەکان

Common output mistakes باوەکانی
MistakeResultئەنجامFixچارەسەر
echo “Hello”;Parse error or “Undefined constant” error یان «⁦Undefined constant⁩»Straight quotes: echo "Hello";نیشانەی ڕاست: echo "Hello";
echo Hello;Error: the text is read as a constant name: دەقەکە وەک ناوی دەخوێنرێتەوەPut text in quotesدەق بخەرە ناو
echo "Hello"."World";HelloWorld (no space)HelloWorld (بێ )Put the space inside a string: "Hello" . " World" بخەرە ناو : "Hello" . " World"
echo "Line 1\nLine 2";One line on the pageیەک دێڕ لە لاپەڕەکەداUse <br> for a line break on the pageبۆ دێڕبڕین لە لاپەڕەکەدا <br> بەکار بهێنە
echo 'Hi\n';Prints Hi\n literallyHi\n وەک خۆی دەنووسێتUse double quotes for \nبۆ \n نیشانەی جووت بەکار بهێنە
print "a", "b";Parse errorUse echo, or join: print "a" . "b";echo بەکار بهێنە، یان پێکەوەیان ببەستە: print "a" . "b";
$r = echo "x";Parse errorecho returns nothing; use print if you need a valueecho هیچ ناگەڕێنێتەوە؛ ئەگەر دەوێت print بەکار بهێنە
echo "<a href="x.php">";Parse error: the inner " ends the string: "ی ناوەوە کۆتایی پێدەهێنێتUse single quotes inside: echo "<a href='x.php'>";لە ناوەوە نیشانەی تاک بەکار بهێنە: echo "<a href='x.php'>";

Practiceڕاهێنان

Lab 2 begins with this instruction: open a new PHP project in your editor (the lab uses NetBeans; VS Code with XAMPP works the same) and try the examples. Do the same with the exercises below.

تاقیگەی 2 بەم ڕێنماییە دەست پێدەکات: پرۆژەیەکی نوێی PHP لە دەستکاریکەرەکەتدا بکەرەوە (تاقیگەکە NetBeans بەکار دەهێنێت؛ ⁦VS Code⁩ لەگەڵ XAMPP هەمان شێوە کار دەکات) و نموونەکان تاقی بکەرەوە. لەگەڵ ڕاهێنانەکانی خوارەوەش هەمان شت بکە.

ExerciseڕاهێنانYour first pageیەکەم لاپەڕەت

Your first page. In the htdocs folder of XAMPP, save a file called index.php that prints Hello world! with echo. Open http://localhost/index.php in the browser.

یەکەم لاپەڕەت. لە htdocsی XAMPPـدا بە ناوی index.php پاشەکەوت بکە کە بە echo دەقی Hello world! بنووسێت. http://localhost/index.php لە بکەرەوە.

Hintئاماژە

If you see the PHP code instead of the text, check that the file ends in .php and that you opened it through http://localhost, not by double-clicking the file.

ئەگەر لە جیاتی دەقەکە کۆدی PHP دەبینیت، بپشکنە کە بە ⁦.php⁩ کۆتایی دێت و لە ڕێگەی ⁦http://localhost⁩ـەوە کردووتەتەوە، نەک بە دوو لەسەر .

Show answerوەڵامەکە پیشان بدە
index.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
echo "Hello world!";
ExerciseڕاهێنانString concatenationپێکەوەلکاندنی زنجیرەنووسە

Lab 1, Exercise 3 (string concatenation). String concatenation is used to connect two strings together. Type the code below with straight quotes and run it. What does it print? Then change it so that it prints Hello world with a space.

تاقیگەی 1، ڕاهێنانی 3 ( ). بۆ پێکەوە بەستنی دوو بەکار دێت. کۆدەکەی خوارەوە بە نیشانەی ڕاست بنووسە و کاری پێبکە. چی دەنووسێت؟ پاشان بیگۆڕە تا Hello world بە بنووسێت.

concat.phpPHP
<?php
// Concatenating two strings
echo "Hello"."world";
?>
Show answerوەڵامەکە پیشان بدە
Output of the lab codeدەرخستەی کۆدی تاقیگەکە
Helloworld
concat.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
// Concatenating two strings
echo "Hello"." world";
?>
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 3

The space is now at the start of the second string.

ئێستا لە سەرەتای دووەمدایە.

ExerciseڕاهێنانHeadings from PHPسەردێڕ لە PHPـەوە

Lab 2, Example 1. Print two level-4 headings with echo: “This is a simple heading.” and “This is heading with style.”, the second one in red.

تاقیگەی 2، نموونەی 1. بە echo دوو سەردێڕی ئاستی 4 بنووسە: «⁦This is a simple heading⁩.» و «⁦This is heading with style⁩.»، دووەمیان بە ڕەنگی سوور.

Show answerوەڵامەکە پیشان بدە
headings.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
// Displaying HTML code
echo "<h4>This is a simple heading.</h4>";
echo "<h4 style='color: red;'>This is heading with style.</h4>";
?>
ExerciseڕاهێنانPredict the outputدەرخستەکە پێشبینی بکە

Predict the output of each line before you run it. Write it down, then check.

هەر دێڕێک پێش کارپێکردنی پێشبینی بکە. بینووسە، پاشان بیپشکنە.

predict.phpPHP
<?php
echo "A", "B", 1;
echo "\n";
echo "Sum: " . 3 + 4;
echo "\n";
$r = print "OK ";
echo $r, "\n";
echo 'Total: $sum\n';
Show answerوەڵامەکە پیشان بدە
predict.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
echo "A", "B", 1;
echo "\n";
echo "Sum: " . 3 + 4;
echo "\n";
$r = print "OK ";
echo $r, "\n";
echo 'Total: $sum\n';
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

Three items, no spaces added: AB1.

سێ بڕگە، هیچ زیاد ناکرێت: AB1.

Lineدێڕی 3

\n makes a new line in the output.

\n لە دێڕێکی نوێ دروست دەکات.

Lineدێڕی 4

+ before .: "Sum: " . 7.

+ پێش .: "Sum: " . 7.

Lineدێڕی 6-7

print writes OK, then echo writes the returned 1.

print دەقی OK دەنووسێت، پاشان echo ئەو 1ـە دەنووسێت کە گەڕاوەتەوە.

Lineدێڕی 8

Single quotes: the text is printed as written.

نیشانەی تاک: دەقەکە وەک نووسراوە دەنووسرێت.

ExerciseڕاهێنانFrom echo to heredocلە echo ـەوە بۆ heredoc

Rewrite this script so that it prints the same HTML with one heredoc instead of four echo statements, and use the variable $name inside it.

ئەم دووبارە بنووسەرەوە بە شێوەیەک کە هەمان HTML بە یەک heredoc بنووسێت لە جیاتی چوار echo و $name لەناویدا بەکار بهێنە.

card.phpPHP
<?php
$name = "Rezan";
echo "<div class=\"card\">\n";
echo "  <h3>Student: $name</h3>\n";
echo "  <p>Welcome to the Web Programming course.</p>\n";
echo "</div>\n";
Show answerوەڵامەکە پیشان بدە
card.phpPHPRuns on the serverلەسەر ڕاژەکاری وێب کار دەکات
<?php
$name = "Rezan";
echo <<<HTML
<div class="card">
  <h3>Student: $name</h3>
  <p>Welcome to the Web Programming course.</p>
</div>

HTML;

Summaryپوختە

  • echo and print send output to the page. Both are language constructs, not functions; parentheses are optional.
  • echo takes any number of items separated by commas and returns nothing. print takes one argument and returns 1, so it can be used in an expression.
  • Strings need straight single or double quotes. echo adds no spaces: put spaces inside strings.
  • The dot . joins strings into one string. + is addition, not concatenation.
  • PHP output is HTML. \n breaks lines in the page source; <br> breaks lines on the page.
  • <?= ... ?> is a short way to echo one value; a heredoc prints a whole block of text.
  • true prints 1; false and null print nothing.
  • echo و print دەنێرن بۆ لاپەڕەکە. هەردووکیان ، نەک ؛ کەوانە ئارەزوومەندانەیە.
  • echo هەر ژمارەیەک بڕگە وەردەگرێت کە بە کۆما جیا کراونەتەوە و هیچ ناگەڕێنێتەوە. print یەک وەردەگرێت و 1 دەگەڕێنێتەوە، بۆیە دەکرێت لە بەکار بێت.
  • پێویستیان بە تاک یان جووتی ڕاست هەیە. echo زیاد ناکات: بخەرە ناو .
  • خاڵ . دەکات بە یەک . + کۆکردنەوەیە، نەک .
  • ⁦PHP HTML⁩ـە. \n لە سەرچاوەی لاپەڕەدا دێڕ دەشکێنێت؛ <br> لەسەر لاپەڕەکە دێڕ دەشکێنێت.
  • <?= ... ?> ڕێگەیەکی کورتە بۆ نووسینی یەک ؛ heredoc بلۆکێکی تەواوی دەق دەنووسێت.
  • true ژمارە 1 دەنووسێت؛ false و null هیچ نانووسن.

Review questions

پرسیارەکانی پێداچوونەوە

Review questionsپرسیارەکانی پێداچوونەوە
Give three differences between echo and print.سێ جیاوازیی نێوان echo و print بڵێ.

echo can print several items separated by commas, print only one; print returns 1, echo returns nothing; so print can be used inside an expression and echo cannot. (echo is also very slightly faster.)

echo دەتوانێت چەند بڕگەیەک بنووسێت کە بە کۆما جیا کراونەتەوە، print تەنها یەک؛ print ژمارە 1 دەگەڕێنێتەوە، echo هیچ؛ بۆیە print دەکرێت لەناو بەکار بێت و echo ناکرێت. (echo هەروەها زۆر کەمێک خێراترە.)

Why does echo "Hello\nWorld"; show “Hello World” on one line in the browser?بۆچی echo "Hello\nWorld"; لە تۆڕگەڕدا «⁦Hello World⁩» لە یەک دێڕدا پیشان دەدات؟

Because \n puts a line break in the HTML source, and the browser displays a line break in HTML as a single space. To break the line on the page, print <br>.

چونکە \n دێڕبڕینێک دەخاتە ناو سەرچاوەی HTMLـەکەوە و دێڕبڕین لە HTMLـدا وەک یەک پیشان دەدات. بۆ شکاندنی دێڕ لەسەر لاپەڕەکە <br> بنووسە.

What is the difference between echo "a", "b"; and echo "a" . "b";?جیاوازیی نێوان echo "a", "b"; و echo "a" . "b"; چییە؟

Both print ab. The first gives echo two items; the second first joins the strings into one string with the dot operator and then prints it.

هەردووکیان ab دەنووسن. یەکەمیان دوو بڕگە دەداتە echo؛ دووەمیان سەرەتا بە خاڵ دەکات بە یەک و پاشان دەینووسێت.

Why is it called echo?بۆچی ناوی echoـە؟

From the Greek word for sound: like an empty room that sends your voice back, echo gives back on the screen the data you give it.

لە وشەی یۆنانیی «دەنگ»ـەوە: وەک ژوورێکی بەتاڵ کە دەنگت بۆ دەگەڕێنێتەوە، echo ئەو پێی دەدەیت لەسەر دەتداتەوە.

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

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

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

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

Variablesگۆڕاوەکان

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

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