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.
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دا پیشان دراوە:
<?php echo "Hello World!"; ?>Hello World!
<?phpand?>open and close the PHP section (see PHP Syntax).echoprints 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!": دەقێک لە نێوان .;: کۆتایی ئەم ڕێنماییە دیاری دەکات.
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 لە پەیامەکانیدا تەواوی دەنووسێت؛ لێرەدا تەنها ناوی داناوە.
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 فەرمانێکە بۆ دروستکردنی .
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> نووسیوتە:
<html>
<body>
<p>Hello World</p>
</body>
</html>HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<p>Hello World</p>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> دەنووسێت:
<html>
<body>
<?php
echo "<p>Hello World</p>";
?>
</body>
</html>Hello World
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<html>
<body>
<p>Hello World</p> </body>
</html>The spaces and line breaks of the HTML source changed a little: PHP removed the line break after ?>. The browser does not care.
و دێڕبڕینەکانی HTMLـەکە کەمێک گۆڕان: PHP دێڕبڕینەکەی دوای ?>ی لابرد. گرنگی پێ نادات.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 4
echoprints 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 هەموو ئەوەی لە نێوان دەینووسێت. سەرنجی ئەم دوو قۆناغە بدە:
- 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. - 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.
- PHP لە HTML تێناگات. نازانێت
<p>HTMLـە و وەک ڕێنمایی سەیری ناکات. تەنها وەک دەقێکی سادە دەینووسێت. - ئەم دەقە وەردەگرێت، HTMLـەکە دەناسێتەوە و بەپێی یاساکانی HTML پیشانی دەدات. بۆیە تۆی تەنها «Hello World» لەسەر دەبینیت.
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) بەدوایدا دێت و بە کۆما لێک جیا دەکرێنەوە:
echo item1, item2, item3, ...;Pay attention to these rules:
ئاگات لەم یاسایانە بێت:
- 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.
- Strings must be written inside single quotation marks
' 'or double quotation marks" ". Numbers need no quotes. - The number of items is unlimited.
- Items are separated by commas.
echoputs no space between the items in the output. - If you want a space in the output, add it yourself as a character inside a string, for example the item
" ".
- هەر بڕگەیەک ، یان ژمارەیەکە. ژمارە دەکرێت تەواو () یان کەرتی () بێت. ڕیزێکە لە و دەکرێت ژمارەشی تێدا بێت.
- دەبێت لەنێو تاک
' 'یان جووت" "دا بنووسرێن. ژمارەکان پێویستیان بە نییە. - ژمارەی بڕگەکان بێسنوورە.
- بڕگەکان بە کۆما لێک جیا دەکرێنەوە.
echoلە هیچ لە نێوان بڕگەکاندا دانانێت. - ئەگەر لە دەوێت، خۆت وەک لەناو زیادی بکە، بۆ نموونە بڕگەی
" ".
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 statement echo | Output | Whyبۆچی |
|---|---|---|
echo 123; | 123 | A 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!; | Error | Not valid: the text is not in quotes.دروست نییە: دەقەکە لەنێو نییە. |
echo 'Hello World!'; | Hello World! | Single quotes work too. تاکیش کار دەکات. |
<?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;123 --- Hello World! --- HelloWorld! --- Hello World! --- Hello World! --- 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.\nis 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 وەک ناو بخوێنێتەوە و ڕادەوەستێت:
<?php
echo Hello World!;Parse error: syntax error, unexpected identifier "World", expecting "," or ";" in noquotes.php on line 2
PHP prints the full path of the file in its messages; here we show only the file name.
PHP لە پەیامەکانیدا تەواوی دەنووسێت؛ لێرەدا تەنها ناوی داناوە.
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 زۆر بەسوودە:
<?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\".";HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<a href='contact.php'>Contact us</a><br><a href="about.php">About us</a><br>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 و کورتەکە هەموویان پیشانی دەدەن:
<?php
echo "Hello" . " World!";
echo "<br>";
echo "Hello"."world";Hello World! Helloworld
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Hello World!<br>HelloworldExplain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
Lecture 1: the space is inside the second string,
" World!", so the result isHello 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.
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 ئەو یەک دەنووسێت.
<?php
echo "Erbil", " ", "Duhok";
echo "<br>";
echo "Erbil" . " " . "Duhok";Erbil Duhok Erbil Duhok
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Erbil Duhok<br>Erbil DuhokExplain 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 ئەنجامەکە دەنووسێت:
<?php
echo 2+3;
echo "<br>";
echo "2 + 3 = ", 2 + 3;
echo "<br>";
echo "Total: " . 2 + 3;5 2 + 3 = 5 Total: 5
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
5<br>2 + 3 = 5<br>Total: 5Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
Lecture 1:
2+3is calculated, andechoprints5.وانەی 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 پیشانی دەدات:
<?php print "Hello World!"; ?>Hello World!
print is also a language construct, like echo. But it has two important differences:
printیش وەک echo ە. بەڵام دوو جیاوازیی گرنگی هەیە:
printtakes exactly one . You cannot give it several items with commas.printreturns 1. Because it gives back a value, it can be used inside an , for example on the right side of=.echoreturns nothing and cannot be used like that.
printتەنها یەک وەردەگرێت. ناتوانیت بە کۆما چەند بڕگەیەکی بدەیتێ.printژمارە 1 دەگەڕێنێتەوە. لەبەر ئەوەی دەداتەوە، دەتوانرێت لەناو بەکار بێت، بۆ نموونە لە لای ڕاستی=.echoهیچ ناگەڕێنێتەوە و ناتوانرێت بەو شێوەیە بەکار بێت.
The print statementڕستەی print
print returns 1print ژمارە 1 دەگەڕێنێتەوە
These three small programs come from the practical notes. Each one stores the of print in a variable and prints it:
ئەم سێ بچووکە لە تێبینییە پراکتیکییەکانەوە هاتوون. هەر یەکەیان print لە هەڵدەگرێت و دەینووسێت:
<!DOCTYPE html>
<html>
<body>
<?php
$result = print(null);
echo $result;
?>
</body>
</html>1
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<!DOCTYPE html>
<html>
<body>
1
</body>
</html>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 6
print(null)prints nothing: becomes an empty string. Thenprintreturns1, which is stored in$result.print(null)هیچ نانووسێت: دەبێت بە بەتاڵ. پاشانprintژمارە1دەگەڕێنێتەوە و لە$resultدا هەڵدەگیرێت.- Lineدێڕی 7
echo $result;prints1.echo $result;ژمارە1دەنووسێت.
<!DOCTYPE html>
<html>
<body>
<?php
$result = print("hey");
print $result;
?>
</body>
</html>hey1
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<!DOCTYPE html>
<html>
<body>
hey1
</body>
</html>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 6
print("hey")printsheyand returns1.print("hey")وشەیheyدەنووسێت و1دەگەڕێنێتەوە.- Lineدێڕی 7
print $result;prints that1, right afterhey.print $result;ئەو1ـە دەنووسێت، ڕاستەوخۆ لەدوایhey.
<!DOCTYPE html>
<html>
<body>
<?php
$result = print(null);
print $result;
?>
</body>
</html>1
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<!DOCTYPE html>
<html>
<body>
1
</body>
</html><?php
echo("Hello");
echo ("A"), ("B");
print("C");HelloABC
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
Each item is in its own parentheses, separated by a comma. It still works, because the parentheses belong to the values.
هەر بڕگەیەک لەناو کەوانەی خۆیدایە و بە کۆما جیا کراونەتەوە. هێشتا کار دەکات، چونکە کەوانەکان هی .
The print statementڕستەی print
print inside an expressionprint لەناو دەربڕیندا
<?php
$r = print "Saved. ";
echo $r;
echo "<br>";
$total = (print "x") + 1;
echo " ", $total;Saved. 1 x 2
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Saved. 1<br>x 2Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
printwritesSaved.and its return value1goes into$r.printدەقیSaved.دەنووسێت و ،1، دەچێتە ناو$r.- Lineدێڕی 5
(print "x") + 1:printwritesx, returns1, and1 + 1is2.(print "x") + 1:printپیتیxدەنووسێت،1دەگەڕێنێتەوە و1 + 1دەکاتە2.
echo cannot do this, and print cannot take two items. Both mistakes are parse errors:
echo ناتوانێت ئەمە بکات و print ناتوانێت دوو بڕگە وەربگرێت. هەردوو :
<?php
$r = echo "Saved.";Parse error: syntax error, unexpected token "echo" in echo-expr.php on line 2
PHP prints the full path of the file in its messages; here we show only the file name.
PHP لە پەیامەکانیدا تەواوی دەنووسێت؛ لێرەدا تەنها ناوی داناوە.
<?php
print "Hello", "World";Parse error: syntax error, unexpected token "," in print-two.php on line 2
PHP prints the full path of the file in its messages; here we show only the file name.
PHP لە پەیامەکانیدا تەواوی دەنووسێت؛ لێرەدا تەنها ناوی داناوە.
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، تێبینییە کوردییەکان و سلایدەکان هەموویان ئەم دووانە بەراورد دەکەن. لێرەدا هەمووی لە یەک شوێندا:
| Pointخاڵ | echo | print |
|---|---|---|
| What it isچییە | A language construct | A language construct |
| Return value | Returns 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زۆر کەمێک خاوترە؛ جیاوازییەکە ئەوەندە بچووکە کە هەست ناکرێت |
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» (دێڕی نوێ).
<?php
echo "Hello\n World";
?>Hello World
HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
Hello
WorldThe page source has two lines, but the browser shows “Hello World” on one line, because HTML shows a line break as a space.
سەرچاوەی لاپەڕەکە دوو دێڕە، بەڵام «Hello World» لە یەک دێڕدا پیشان دەدات، چونکە HTML دێڕبڕین وەک پیشان دەدات.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
\nstarts a new line in the output. The space beforeWorldis 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> بنێریت.
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 پشکنراوە:
| echo statement echo | Stage 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! لە دێڕی دواتردا |
<?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!";Hello World! ===== HelloWorld! ===== Hello World! ===== Hello<br>World! ===== Hello <br> World!
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3,6,8,10
Separator lines, only to make the output easy to read.
دێڕی جیاکەرەوە، تەنها بۆ ئەوەی بە ئاسانی بخوێنرێتەوە.
<?php
echo "<p>Hello<br> World!</p>";HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<p>Hello<br> World!</p>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
echostatements 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 دەینووسێت دەکەوێتە یەک دێڕی زۆر درێژەوە، بۆ نموونە کاتێک درێژ دەنووسیت. لاپەڕەیەکی وا کاتێک بە دەردەکەوێت قورسە و هەندێک کێشەیان لەگەڵ دێڕی زۆر درێژدا هەیە. لاپەڕەی ئەم دوو بەراورد بکە:
<?php
echo "<ul>";
echo "<li>Erbil</li>";
echo "<li>Sulaimani</li>";
echo "<li>Duhok</li>";
echo "</ul>";HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<ul><li>Erbil</li><li>Sulaimani</li><li>Duhok</li></ul><?php
echo "<ul>\n";
echo " <li>Erbil</li>\n";
echo " <li>Sulaimani</li>\n";
echo " <li>Duhok</li>\n";
echo "</ul>\n";HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<ul>
<li>Erbil</li>
<li>Sulaimani</li>
<li>Duhok</li>
</ul>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»:
<?php
// Displaying HTML code
echo "<h4>This is a simple heading.</h4>";
echo "<h4 style='color: red;'>This is heading with style.</h4>";
?>HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<h4>This is a simple heading.</h4><h4 style='color: red;'>This is heading with style.</h4>Two level-4 headings; the second one is red.
دوو سەردێڕی ئاستی 4؛ دووەمیان سوورە.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
A comment.
.
- Lineدێڕی 3
Prints a level-4 heading.
سەردێڕێکی ئاستی 4 دەنووسێت.
- Lineدێڕی 4
The
styleattribute is inside single quotes, because the whole string uses double quotes.styleلەناو نیشانەی تاکدایە، چونکە هەموو نیشانەی جووت بەکار دەهێنێت.
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:
<?php
$firstName = "Dilan";
$lastName = "Kamal";
echo "My name is ", $firstName, " ", $lastName;
echo "\n";
echo "My name is ", $firstName, $lastName;
?>My name is Dilan Kamal My name is DilanKamal
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 statement echo | Output | Ruleیاسا |
|---|---|---|
echo $number; | 123 | A 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; | Error | Not valid: no comma separates the variables.دروست نییە: هیچ کۆمایەک جیا ناکاتەوە. |
echo "$word1 $word2 now"; | Hello World! now | Inside double quotes, variables are replaced by their values.لەناو نیشانەی جووتدا بە دەگۆڕدرێن. |
<?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";123 --- HelloWorld! --- Hello World! --- Hello World! now
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 11
Double quotes replace
$word1and$word2with their values. This is called ; see Strings.نیشانەی جووت
$word1و$word2بە دەگۆڕێت. پێی دەگوترێت ؛ بڕوانە زنجیرەنووسەکان.
<?php
$word1 = "Hello";
$word2 = "World!";
echo $word1 $word2;Parse error: syntax error, unexpected variable "$word2", expecting "," or ";" in no-comma.php on line 4
PHP prints the full path of the file in its messages; here we show only the file name.
PHP لە پەیامەکانیدا تەواوی دەنووسێت؛ لێرەدا تەنها ناوی داناوە.
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ـدا باوە.
<?php $city = "Halabja"; ?>
<p>City: <?= $city ?></p>
<p>Same as: <?php echo $city; ?></p>HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<p>City: Halabja</p>
<p>Same as: Halabja</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 وەک کار دەکات: ناوی بە دەگۆڕدرێن و دەتوانیت بە ئازادی " بەکار بهێنیت.
<?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;HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<h2>Welcome to Hana's Bookshop</h2>
<p>We are open every day from 9 to 5.</p>
<p>Ask for our "book of the week".</p>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
<<<ENDstarts the heredoc.<<<ENDheredoc دەست پێدەکات.- Lineدێڕی 4
$shopis 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 دروست دەبێت. وەک کار دەکات: هیچ شتێکی ناوی ناگۆڕدرێت.
<?php
$shop = "Hana's Bookshop";
echo <<<'END'
<p>In a nowdoc, $shop is printed exactly as written.</p>
END;HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<p>In a nowdoc, $shop is printed exactly as written.</p>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». کۆدەکەی، دوای ڕاستکردنەوەی نیشانە چەماوەکان، ئەمەیە:
<?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; ?>HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
This uses the "document"no extra whitespace!<br> <?XML
<channel>
<title>What's For Dinner</title>
<link>http://menu.example.com/ </link>
<description>Choose what to eat
tonight.</description>
</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
<<<ENDandEND;.ئەمە تاکە heredocـەکەیە: یەک دێڕی دەق لە نێوان
<<<ENDوEND;دا.- Lineدێڕی 14-15
printa<br>, then the string from line 2.print<br>دەنووسێت، پاشان دێڕی 2.
<?php
$channel = <<<XML
<channel>
<title>What's For Dinner</title>
<link>http://menu.example.com/</link>
</channel>
XML;
echo "<pre>", htmlspecialchars($channel), "</pre>";HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<pre><channel>
<title>What's For Dinner</title>
<link>http://menu.example.com/</link>
</channel></pre>Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2-7
A heredoc that holds a small XML document.
XMLis just the name chosen for the marker.heredocێک کە بەڵگەنامەیەکی بچووکی XMLـی تێدایە.
XMLتەنها ئەو ناوەیە کە بۆ نیشانەکە هەڵبژێردراوە.- Lineدێڕی 8
htmlspecialchars()changes<into<and>into>, so the browser shows the tags instead of reading them.<pre>keeps the line breaks.htmlspecialchars()نیشانەی<دەگۆڕێت بۆ<و>بۆ>، بۆیە پیشان دەدات لە جیاتی ئەوەی بیانخوێنێتەوە.<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" دەگەڕێنێتەوە. هەندێک ڕیز بیرۆکەگەلێک بەکار دەهێنن کە لە وانەکانی داهاتوودا دەخوێنرێن (، ، )؛ ستوونی کۆتایی پێت دەڵێت لە کوێ.
| # | Exampleنموونە | Output in PHP 8 لە PHP 8دا | Whyبۆچی |
|---|---|---|---|
| 1 | echo $var1.$var2; | Var1ValueVar2Value | The 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. هیچ زیاد ناکات. (تێبینییەکان بە پیشان دەدەن.) |
| 3 | echo $var1.func1(); | Var1ValueFunc1Value | The value returned by the function is joined to $var1. See Functions.ئەو دەیگەڕێنێتەوە بە $var1ـەوە دەلکێت. بڕوانە کردارەکان. |
| 4 | echo $var1." ".$var2; | Var1Value Var2Value | A string with one space is joined between them. کە یەک تێدایە لە نێوانیاندا دەلکێت. |
| 5 | echo "$var1"; | Var1Value | Double quotes replace a variable with its value.نیشانەی جووت بە دەگۆڕێت. |
| 6 | echo '$var1'; | $var1 | Single quotes print the text exactly as written.نیشانەی تاک دەقەکە ڕێک وەک نووسراوە دەنووسێت. |
| 7 | define("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 بوو.) بڕوانە نەگۆڕەکان. |
| 8 | define("cons", 2);
echo cons; | 2 | The same name in the same case: the value 2 is printed.هەمان ناو بە هەمان شێوەی پیت: 2 دەنووسرێت. |
| 9 | echo "<pre>Hello\tWorld</pre>"; | Hello, a tab, WorldHello، تابێک، World | In double quotes \t is a tab; <pre> makes the browser show it.لەناو نیشانەی جووتدا \t تابێکە؛ <pre> وا دەکات پیشانی بدات. |
| 10 | echo '<pre>Hello\tWorld</pre>'; | Hello\tWorld | In single quotes \t is not special: a backslash and a t are printed.لەناو نیشانەی تاکدا \t تایبەت نییە: لارەهێڵێکی پێچەوانە و پیتی t دەنووسرێن. |
| 11 | echo 'I\'ll do it\nH'; | I'll do it\nH | In single quotes only \' (and \\) are special, so \n stays as it is.لەناو نیشانەی تاکدا تەنها \' (و \\) تایبەتن، بۆیە \n وەک خۆی دەمێنێتەوە. |
| 12 | $var = 4;
print($var = 5); | 5 | An assignment has a value: $var = 5 is 5, and print prints it. خۆی هەیە: $var = 5 دەکاتە 5 و print دەینووسێت. |
| 13 | echo true; | 1 | true is printed as 1. See Data types.true وەک 1 دەنووسرێت. بڕوانە جۆرەکانی دراوە. |
| 14 | echo !false; | 1 | !false is true, which is printed as 1.!false دەکاتە true، کە وەک 1 دەنووسرێت. |
| 15 | echo false; | (nothing)(هیچ) | false is printed as an empty string.false وەک بەتاڵ دەنووسرێت. |
| 16 | echo null; | (nothing)(هیچ) | null is printed as an empty string.null وەک بەتاڵ دەنووسرێت. |
<?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";1: Var1ValueVar2Value 2: Var1ValueVar2Value 3: Var1ValueFunc1Value 4: Var1Value Var2Value 5: Var1Value 6: $var1 8: 2 9: <pre>Hello World</pre> 10: <pre>Hello\tWorld</pre> 11: I'll do it\nH 12: 5 13: 1 14: 1 15: [] 16: []
Row 9 contains a real tab character between Hello and World.
ڕیزی 9 ڕاستەقینەی تابی لە نێوان Hello و Worldدا تێدایە.
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
$var1keeps 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 لە نێوان کەوانەی چوارگۆشەدا دەنووسرێن، تا ببینیت کە بەتاڵە.
<?php
define("Cons", 2);
echo cons;Fatal error: Uncaught Error: Undefined constant "cons" in row7.php:3
Stack trace:
#0 {main}
thrown in row7.php on line 3PHP prints the full path of the file in its messages; here we show only the file name.
PHP لە پەیامەکانیدا تەواوی دەنووسێت؛ لێرەدا تەنها ناوی داناوە.
Common mistakesهەڵە باوەکان
| Mistake | Resultئەنجام | 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 error | Use echo, or join: print "a" . "b";echo بەکار بهێنە، یان پێکەوەیان ببەستە: print "a" . "b"; |
$r = echo "x"; | Parse error | echo 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 هەمان شێوە کار دەکات) و نموونەکان تاقی بکەرەوە. لەگەڵ ڕاهێنانەکانی خوارەوەش هەمان شت بکە.
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وەڵامەکە پیشان بدە
<?php
echo "Hello world!";Hello world!
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 بە بنووسێت.
<?php
// Concatenating two strings
echo "Hello"."world";
?>Show answerوەڵامەکە پیشان بدە
Helloworld
<?php
// Concatenating two strings
echo "Hello"." world";
?>Hello world
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3
The space is now at the start of the second string.
ئێستا لە سەرەتای دووەمدایە.
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وەڵامەکە پیشان بدە
<?php
// Displaying HTML code
echo "<h4>This is a simple heading.</h4>";
echo "<h4 style='color: red;'>This is heading with style.</h4>";
?>HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<h4>This is a simple heading.</h4><h4 style='color: red;'>This is heading with style.</h4>Predict the output of each line before you run it. Write it down, then check.
هەر دێڕێک پێش کارپێکردنی پێشبینی بکە. بینووسە، پاشان بیپشکنە.
<?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وەڵامەکە پیشان بدە
<?php
echo "A", "B", 1;
echo "\n";
echo "Sum: " . 3 + 4;
echo "\n";
$r = print "OK ";
echo $r, "\n";
echo 'Total: $sum\n';AB1 Sum: 7 OK 1 Total: $sum\n
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
Three items, no spaces added:
AB1.سێ بڕگە، هیچ زیاد ناکرێت:
AB1.- Lineدێڕی 3
\nmakes a new line in the output.\nلە دێڕێکی نوێ دروست دەکات.- Lineدێڕی 4
+before.:"Sum: " . 7.+پێش.:"Sum: " . 7.- Lineدێڕی 6-7
printwritesOK, thenechowrites the returned1.printدەقیOKدەنووسێت، پاشانechoئەو1ـە دەنووسێت کە گەڕاوەتەوە.- Lineدێڕی 8
Single quotes: the text is printed as written.
نیشانەی تاک: دەقەکە وەک نووسراوە دەنووسرێت.
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 لەناویدا بەکار بهێنە.
<?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وەڵامەکە پیشان بدە
<?php
$name = "Rezan";
echo <<<HTML
<div class="card">
<h3>Student: $name</h3>
<p>Welcome to the Web Programming course.</p>
</div>
HTML;HTML sent to the browserئەو HTMLـەی دەنێردرێت بۆ تۆڕگەڕ
<div class="card">
<h3>Student: Rezan</h3>
<p>Welcome to the Web Programming course.</p>
</div>Summaryپوختە
echoandprintsend output to the page. Both are language constructs, not functions; parentheses are optional.echotakes any number of items separated by commas and returns nothing.printtakes one argument and returns 1, so it can be used in an expression.- Strings need straight single or double quotes.
echoadds no spaces: put spaces inside strings. - The dot
.joins strings into one string.+is addition, not concatenation. - PHP output is HTML.
\nbreaks 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.trueprints 1;falseandnullprint nothing.
echoوprintدەنێرن بۆ لاپەڕەکە. هەردووکیان ، نەک ؛ کەوانە ئارەزوومەندانەیە.echoهەر ژمارەیەک بڕگە وەردەگرێت کە بە کۆما جیا کراونەتەوە و هیچ ناگەڕێنێتەوە.printیەک وەردەگرێت و 1 دەگەڕێنێتەوە، بۆیە دەکرێت لە بەکار بێت.- پێویستیان بە تاک یان جووتی ڕاست هەیە.
echoزیاد ناکات: بخەرە ناو . - خاڵ
.دەکات بە یەک .+کۆکردنەوەیە، نەک . - PHP HTMLـە.
\nلە سەرچاوەی لاپەڕەدا دێڕ دەشکێنێت؛<br>لەسەر لاپەڕەکە دێڕ دەشکێنێت. <?= ... ?>ڕێگەیەکی کورتە بۆ نووسینی یەک ؛ heredoc بلۆکێکی تەواوی دەق دەنووسێت.trueژمارە 1 دەنووسێت؛falseوnullهیچ نانووسن.
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زاراوە سەرەکییەکان
Variablesگۆڕاوەکان
Related lecturesوانە پەیوەندیدارەکان
References and further readingسەرچاوەکان و خوێندنەوەی زیاتر
This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.