136

PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

  • Upload
    others

  • View
    14

  • Download
    0

Embed Size (px)

Citation preview

Page 1: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of
Page 2: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

PHP GUIDE BOOKFirst Edition

Moe Myint Shein

PHP Guide Book Moe Myint Shein

2 Youth Dreams 2

Page 3: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

အမာစကား

မဂၤလာပါခငဗာ။ က ေနာ Part (I) ပါဆျပးေတာ႔ ျပးခ႔တ႔ လက တစအပ ထတခ႔ပါတယ။ ၂၀၁၀ ၾသဂတ ၁၃ မာ Part(II) ထတမယလ႔ေျပာခ႔ပါတယ။ အလမးဆ ႏစအပျဖစျပး ရႈပေနမယထငပါတယ။ တကယေရးျပးတ႔အခါမာ First editionလ႔ ထတတာက ပျပး သငေတာတယလ႔ ယဆမပါတယ။ဘာရယမဟတပါဘး အေရ႕က တစအပန႔ အမညကြေအာငပါ။ Second edition လာလမ႔မယလ႔ေတာ႔ မထငပါဘး။ ဒလပ နာမညေပးတာပါ။ပထမစာအပမာ စာမကႏာ ၇၀ ၀နးက ငန႔ ထတေပးခ႔ျပးေတာ႔ ဒ First Edition မာ က ေနာ ေနာကထပ စာမကႏာ ၇၀ နးပါး ေလာကထပေပါငးထညလကပါတယ။ဒစာအပမာေတာ႔ တတႏငသမ ပထမစာအပမာ အေလာတၾကး လပလကလ႔ Chapter ေတြေသခာမစထားျဖစတာေတြကအစ ပစေတြျပနျပငထားျပး ၾကးစားေရး ထားပါတယ။ စာေတြေတာ႔ မေျပာငး သြားပါဘး။ အဆငေျပေအာငအေရ႕အေနာက ျပနစစဥထားတာေတာ႔ရပါတယ။

ဒစာအပက ေလ႔လာျပးတ႔အခါ သင PHP မတတသြားပါဘး။ Web Developer လညးမျဖစပါ။ ဒါေပမယ႔ မဖတခငက န႔ ယဥရငေတာ႔ သငအမားၾကး ကြာျခားသြားမာပါ။ PHP န႔ပတသတျပးအေျခခေကာငးေတြရျပး ဆကလကေလ႔လာဖ႔ လမးပြင သြားမယလ႔ ယၾကညပါတယ။

ဒစာအပန႔ေလ႔လာဖ႔ Programming အေျခခရက အလြနေကာငးပါသည။ မရပါက ျဖညးျဖညးခငး ဖတပါ..။ codeတငးက တတႏငသမ run ပါ လ႔အၾကျပပါရေစ။၀နခခငတာေတြကေတာ႔ code ေတြက w3schools.com ကေန referenceလပျပးသးထားပါတယ။ အမားစက ဘာသာျပနတာျဖစပါတယ။ နားလညမယလ႔ေတာ႔ ေမာလငမပါတယ။ တစစတစရာ အဆငမေျပတာ ရခ႔ေသာ က ေနာညလ႔သာျဖစေၾကာငး။ ေရးေနစဥမာ အားေပးခ႔သမားအားလးက ေက းဇးတငပါတယ။ အားလးေသာ ေဒါငးလပဆြၾကသ ဖတရႈအားေပး ေလ႔လာၾကသ ညအစက ေမာငႏမ မား အားလးကလညးေက းဇးတငပါတယ။ စကားမစပ က ေနာ စာေရးဆရာမဟတပါ။ ကညခငတ႔ စတသကသကန႔ေရးတာ ျဖစပါတယ။ ေနာကလညး စာေတြအမားၾကး ေရးဖ႔ အစစဥမရပါခငဗာ။ တတႏငသေလာကေတာ႔ ကညသြားမာပါ။ အားလးကေက းဇးတငပါတယ။ အၾကျပခကမားက အးေမး [email protected] သ႔ေပးပ႔ႏငပါတယ။ဘာေတြ အသစရေနမလဆတာကလညး ကေနာ႔ ဘေလာ႔ www.moemyintshein.com မာလာေရာက လညပတႏင ေၾကာငးပါ။

ေလ႔လာၾကသအားလးက ေလးစားလ က

မးျမငရန

PHP Guide Book Moe Myint Shein

3 Youth Dreams 3

Page 4: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

Contents at a Glance

Chapter 1 Introducing PHP 5

Chpater 2 The Building Blocks of PHP 7

Chapter 3 Flow Control Functoins in PHP 14

Chapter 4 Working With Functions and Forms 25

Chapter 5 PHP Date, Include, File Handling & Some PHP Advaced 32

Chapter 6 PHP and AJAX 54

Chapter 7 PHP MyAdmin Basic 74

Chapter 8 PHP and Database 83

Chapter 9 XML and PHP 97

Chatper 10 Exception Handling and Filter in PHP 124

Conclusion 136

PHP Guide Book Moe Myint Shein

4 Youth Dreams 4

Page 5: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

Chapter 1 Introducing PHP

What is PHP?PHP ဆတာ dynamic web pages ေတြအတြကရညရႊယျပးေပၚေပါကလာခ႔တ႔ scripting language ျဖစပါတယ။PHP က web scripts ေတြေရးဖ႔ဖနတးခ႔တာသာျဖစပါတယ။ stand alone application ေတြေရးဖ႔မဟတပါဘး (ဒါေပမယ႔ အခအခနမာေတာ႔ application ေတြကလညး PHP န႔ေရးလ႔ရေနပါျပ။) PHP က မလပထမဆး ဖနတးခ႔သကေတာ႔ RasmusLerdorf ပျဖစပါတယ။ စစေပၚခငး 1994 မာ PHP ရ႕အရညက Personal Home Page လ႔ Rasmus Lerdorfကေခၚတြငခ႔ပါတယ .။ ေနာကပငးမာေတာ႔ အခသၾကတ႔အတငး PHP: Hypertext Preprocessor ျဖစပါတယ။ PHPဟာယေန႔အခါမာေတာ႔ အသးအမားဆး programming language တစခအျဖစရပတညေနပါျပ။ အခအခါမာ PHP က THEPHP GROUP ကေန maintain လပေပးေနပါတယ။ ဒါ႔အျပင PHP ဟာ PHP License ရ႕ေအာကမာ ထြကေပၚတ႔ freesoftware ျဖစပါတယ။ PHP ဟာ general-purpose scripting language တစချဖစေပမယ႔ အထးသျဖငေတာ႔ webdevelopment အတြကအဓကတြငတြငက ယက ယအသးျပၾကပါတယ။ သ႔က HTML coding ေတြအထမာ ထညသြငးအသးျပႏငျပး web server ေပၚမာ run တာျဖစပါတယ။ web servers အမားစမာ အသးျပႏငျပးေတာ႔ Operating System ေတြန႔ Platform ေတြအားလးနးပါးမာ အခေၾကးေငြေပးစရာမလပ ရယ အသးျပႏငပါတယ။ အခအခနမာ PHP က websites ေပါငး သနး ၂၅ ေက ာ န႔ web server ၁ သနးေက ာေပၚမာ installလပကာအသးျပေနၾကျပး ဒအေရအတြကဟာ ေန႔စဥန႔အမတးလ႔လာေနပါတယ။ သ႔ရ႕ syntax ဟာ Perl န႔ Cတန႔ဆငတပါတယ။ operating system အေတာမားမားေပၚမာ Apache (web server) န႔ အတတ တြျပး အသးျပၾကပါတယ။ Windows ေပၚမာလညး Microsoft ရ႕ IIS န႔လညးယဥတြအသးျပႏငပါတယ။ official current releaseကေတာ႔ PHP 5.3.2 (March 4, 2010) ျဖစပါတယ။ အခဒစာေရးေနစဥ latest preview release ကေတာ႔5.3.3 rc3 (July15, 2010) ျဖစပါတယ။PHP 4 ကေန PHP 5 ကေျပာငးလာတ႔အခါမာ object-oriented programming languageျဖစလာခ႔ပါတယ။ အခအခါမာေတာ႔ ကမာၻေပၚမာရတ႔ ဘာသာစကားအားလးနးပါး ရ႕ အကၡရာ ေတြက ကငတြယႏငျပလ႔ဆႏငပါတယ။ PHP ဟာ သ႔ရ႕ ယဥျပငဘက language ေတြထက ပေကာငးတယ ပျမနတယ သငယဖ႔ပမ လြယကတယ လ႔ အၾကမးအားျဖင ေျပာႏငပါတယ။ database အေတာမားမားန႔ယဥတြအသးျပႏငျခငး ေပါ႔ပါးျခငး အကန႔ အသန႔ မရဘးလ႔ ေျပာႏငေလာကေအာင ခ႕ထြငအသးခႏငျခငး open source ျဖစေသာေၾကာင ကနက စရတမရျခငး စတ႔ေကာငးကး မားစြာ ရေန ပါတယ။ ကၽႊနေတာ PHP စာအပကအခေရးေပမယ႔ JSP, ROR န႔ ASP.net တ႔ကလညး ေလ႔လာေနတာပျဖစပါတယ။ တစခစတငးမာေတာ႔ အားသာခက အားနညးခကကယစရၾကတာမးျဖစပါတယ။ ဘယအရာကမ ရာႏႈနးျပညေကာငးးမြနတယလ႔မဆႏငပါဘး..။

PHP အလပလပပ

PHP သည server-side language ျဖစပါတယ။ ဆလတာက ကၽႊနေတာတ႔ေရးလကတ႔ code ေတြဟာ server ေပၚမာ runတာျဖစပါတယ။ server-side language ေတြက web page ေတြဆကေန ပ႔ေပးလကတ႔ အခကအလကေတြကရယပါတယ။ host ရ႕ server ဆက ေရာကလာမယ။ ေရာကလာတ႔ information ေပၚမာမတညျပးေတာ႔ server ေပၚမာ run ပါတယ။ ျပးမ browser ဆက လအပတ႔အခကအလကကျပနပ႔ေပးပါတယ။ ဥပမာ ကၽႊနေတာတ႔က web page ေပၚမာ clickတစခကႏပလကရင ကၽႊနေတာတ႔ အဒ click ႏပလကျခငးေၾကာင ရလာတ႔ result ကမျမငရခငမာ server ဆက ကၽႊနေတာတ႔ရ႕ Input ေပးလကတ႔ အခကအလကေတြက ပ႔တာ ရယ server ကေန ျပနပ႔တာေတြ လပေနတာျဖစပါတယ။ဒါက server-side ရ႕သေဘာပါ။

client-side language ကေတာ႔ ကၽႊနေတာတ႔ user ေတြအသးျပတ႔ client ေပၚမာပ တကရက run တာပါ။ JavaScript ရ႕ client ကေတာ႔ web browser ေပါ႔။ ဒါေၾကာင သ႔အေနန႔ web server ဆက information ပ႔တာ

PHP Guide Book Moe Myint Shein

5 Youth Dreams 5

Page 6: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

ျပနထတတာေတြ လပဖ႔မလပါဘး။ သေဘာတရားကေတာ႔ browser က code ကဖတတယ ျပးရင interprets လပတယ။ ျပးရင ျပနထတျပတယ။ ျမနျမနဆနဆန ျပးပါတယ။ သ႔ေနရာန႔ သေတာ႔ client-side ေရာ server-side ေရာ လအပပါတယ။ ဥပမာ form တစခ ဆပါစ႔ဗာ။ ကၽႊနေတာ တ႔ sign up လပတ႔အခါျဖစျဖစေပါ႔ ကၽႊနေတာတ႔ text box ထမာထညလကတ႔ data ေတြကမန မမန က client side language တစခန႔စစမယ။valid ျဖစမျဖစကေပါ႔။ ျပးမ submit ကျဖစျဖစႏပလကမ server ဆကပ႔မာေပါ႔။ server ဆကမနကနတ႔ information ေတြပေရာကရေစတာေပါ႔ဗာ။ web server ေပၚမာ အခကအလကေတြက save လပျပး သမးထားဖ႔လအပတာမး ၾကေတာ႔ server side န႔ပလပလ႔ရတာေပါ႔။

Fig 1.1 PHP ကအသးျပထားေသာ server ထမ user က web page က request လပပPHP ကေလ႔လာေတာမယဆရင Apache web server န႔ My SQL Database တ႔ပါတြျပးပါေနတာျဖစပါတယ။အခကၽႊနေတာတ႔ PHP ကစတငေလ႔လာဖ႔အတြက လအပတ႔ software ကသြငးရပါမယ။

localhost မာသးဖ႔အတြက လအပတ႔software ေတြအားလးကေပါငးစျပးထညထားတ႔package software ေတြရပါတယ။အဒsoftware ေတြကသးျပးweb server န႔database ကကယ႔စကထမာပရရမာျဖစပါတယ။အဒsoftware ေတြထမာwindows အတြကထငရားတာေတြကေတာ႔WAMP န႔XAMPP တ႔ပျဖစၾကပါတယ။သ႔ထမာapache, mysql, php စတ႔လအပတာေတြအားလးပါ၀ငတာျဖစပါတယ။ကၽႊနေတာတ႔အခXAMPP ကပေရြးျပးေတာ႔ေလ႔လာၾကပါမယ။၁။http://www.apachefriends.org/en/xampp.html ကသြားျပးေတာ႔ကယ႔ရ႕windows version န႔ကကညတ႔xampp installer ကေဒါငးလပဆြပါ။အဆငဆငသြငးသြားလကရင ရသြားမာပါ။ သာမန software သြငးသလပါပခငဗာ။ သြငးျပးသြားရငေတာ႔ C ေအာကက xampp ေအာကက htdocs ေအာကမာ php ဆတ႔ folder ေလးေဆာကသငပါတယ။ အဒ ေအာကမာပက ေနာတ႔ ေရးသမက save သငပါတယ။ PHP စာအပတစအပျဖစတ႔အတြက လပတတသငတယလ႔လညး ယဆတ႔အတြက PHP,Apache န႔MySQL တ႔က သးျခားစ Install လပတ႔ပစက Part (II) စာအပမာ ေရးေပးပါမယ။ အခက ေနာတ႔ အလပျဖစဖ႔အတြကန႔ လြယလြယကက ျမနျမနဆနဆနေလ႔လာႏငဖ႔ XAMPP န႔ပသြားဖ႔ က ေနာ ေရြးလကတာပါ။ေနာကထပ ဒစာအပမာပါတ႔ သငခနးစာ မားကေလ႔လာႏငဖ႔အတြက Web Browser (firefox, IE) တစခလပါမယ။ texteditor တစခလပါမယ။ PHP editor တစခဆပေကာငးပါတယ။ ကၽႊနေတာကေတာ႔ Dream weaver CS 4 က အသးျပပါတယ။ ဒါေပမယ႔လညး Notepad ကပအသးျပမယဆရငလညး အဆငေျပႏငပါေသးတယ။

PHP Guide Book Moe Myint Shein

6 Youth Dreams 6

Page 7: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

Chapter 2.The Building Blocks of PHP

Basic PHP Syntax

PHP ကေရးတ႔အခါ <?php န႔စျပး ?> န႔အဆးသတေလ႔ရပါတယ။ PHP block တစခက document ရ႕ဘယေနရာမဆ ထညလ႔ရပါတယ။ အတေကာကအေနန႔ <? န႔စျပး ?> န႔ဆးရငေတာ႔ရပါတယ။ ဒါေပမယ႔ အရညေရးတာကပအားေပးပါတယ။

<?php?>

PHP file တစခမာ ပမနအားျဖင HTML ဖငတစခလပ HTML tag ေတြပါ၀ငပါတယ။ PHP code ေတြလညးပါ၀ငပါတယ။ ေအာကက PHP script ကၾကညမယဆရင ဒါဟာ browser က “Hello World” ဆတ႔ စာသားေလးက ထတျပေပးတ႔ codeေလးပျဖစပါတယ။

<html><body><?phpecho "Hello World";?></body></html>

ဒ code ေလးေတြက run မယဆရင Dreamweaver ကဖြင PHP ကေရြးျပးေတာ႔ အလအေလာကနဂရေနတ႔ codeေတြကျဖတႏငပါတယ။ Notepad ကပသးရငလညးရပါတယ။ အဒေနာက code ေတြက copy paste လပပါ။ ျပးရင saveလပပါ.. save လပရမယ႔ေနရာကေတာ႔ C ေအာကက xampp ေအာကက htdocs ေအာကက php ရ႕ေအာကမာျဖစပါတယ။ အဆငေျပသလနာမညေပးႏငပါတယ။ ကၽႊနေတာကေတာ႔ 1.php, 2.php အစရသျဖငပေပးထားပါတယ။ အဒါက browserမာၾကညမယဆရင http://localhost/php/1.php, http://localhost/php/2.php အစရသျဖငေခၚျပး ၾကညလ႔ ရပါျပ။ PHPcode ေတြရ႕အဆးသတတငးမာ semicolon န႔အဆးသတရပါမယ။ semicolon က စာေၾကာငးတစေၾကာငးန႔ ေနာကတစေၾကာငးကျခားနားေပးတ႔ separator သေဘာပါပ။ PHP ကသးျပးေတာ႔ text စာသားေတြက outputထတတ႔အခါ အသးျပႏငတ႔အေျခခ statements ႏစမးရပါတယ။ echo န႔ print ပါ။ အထကပါဥပမာမာေတာ႔ echoကသးျပး Hello World ဆတ႔ စာသားက output ထတထားပါတယ။ မတသားရမာက တကယလ႔ဖငက save လပတ႔အခါ .php န႔ မသမးပန႔ .html န႔သမးမယဆရင အထက PHP code ေတြဟာအလပလပမာမဟတပါဘး။

Comments in PHP

PHP မာ single line comment အတြက // ကအသးျပပါတယ။ စာေၾကာငးေရမားတယဆရငေတာ႔ /* န႔စျပး */န႔ဆးပါတယ။

PHP Guide Book Moe Myint Shein

7 Youth Dreams 7

Page 8: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

<html><body><?php//This is a comment/*This isa commentblock*/?></body></html>

PHP Variables

variable ေတြက number ေတြ၊ strings ေတြ arrays ေတြ function result ေတြစတ႔ တနဖးေတြက သမးဖ႔အတြကအသးျပပါတယ။ ဒလ variable တစခကေၾကျငာထားခ႔မယဆရင သ႔က ကၽႊနေတာတ႔ရ႕ php script ထမာ ထပခါထပခါျပနေခၚသးႏငတာျဖစပါတယ။

PHP ရ႕ variable ေတြအကနလးက $ သေကၤတန႔ စရပါတယ။

$var_name = value;

တကယလ႔ $ သေကၤတန႔မစဘးဆရင အလပလပမာမဟတပါဘး။ အခ string variable တစခန႔ number variableတစခက ေရးၾကညပါမယ။

<?php$txt = "Hello World!";$number = 10;?>PHP မာ variable တစခက ဘာ data type ျဖစတယဆတာ ၾကတငေၾကျငာစရာ မလပါဘး။ အထကက ဥပမာေလးမာ ဒ variable ဟာ ဘာ data type ျဖစတယဆတာမးက ေနာတ႔ မေၾကျငာခ႔ပါဘး။ သ႔ကက ေနာတ႔ ေပးထားခ႔တ႔ တနဖးေပၚမာမတညျပး မနကနတ႔ data type ကPHP က အလအေလာက ေျပာငးေပးမာျဖစပါတယ။ Java လမး Strongtype language မာဆရငေတာ႔ အသးမျပခငမာ သ႔ရ႕ data type ကၾကတငေၾကျငာေပးရတာျဖစပါတယ။ variableကအမညေပးတ႔အခါ သ႔ရ႕ ဥပေဒေတြရပါတယ။ - variable name တစခက letter တစခခ ဒါမမဟတ underscore “_” န႔စတငရပါမယ။- variable name တစခမာ alpha-numeric characters န႔ underscores (ဆလတာက a-z, A-Z, 0-9, န႔ _ )တ႔ပပါ၀ငရပါမယ။

PHP Guide Book Moe Myint Shein

8 Youth Dreams 8

Page 9: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

- variable name တစခမာ space ေတြမပါ၀ငရပါဘး။ variable name စာလး ( word ) တစခထကပတယဆရင underscore (_) န႔ ($my_string) ဒါမမဟတ capital letter န႔ ($myString) ျခားနားထားသငပါတယ။

Strings in PHP

String variable တစခက text (character stings)ေတြက သမးဆညး ဖ႔ အလပလပဖ႔ အတြက အသးျပပါတယ။ stringေတြန႔ ပတသတျပး အသးမားတ႔ function ေတြန႔ operators ေတြကနနေလ႔လာၾကညပါမယ။ String တစခက ဖနတးျပးတ႔အခါ သ႔က တြကခကႏငပါတယ။ string တစခက function တစခထမာ တကရကအသးျပႏငသလ variableတစခအျဖစန႔လညး သမးဆညးႏငပါတယ။ေအာကက ဥပမာမာ Hello World ဆတ႔ String က $txt ဆတ႔ string variable ထကတနဖး assignလပထားတာျဖစပါတယ။

<?php$txt = "Hello World";echo $txt;?>

သ႔ရ႕ output ကေတာ႔ Hello World ျဖစပါတယ။ အခ string ေတြက function န႔ operators တစခ႕ကသးျပး manipulateလပတ႔ပစကၾကညပါမယ။

The Concatenation OperatorPHP မာ string န႔ပတသတတ႔ operator တစခပရပါတယ။ concatenation operator ဆတ႔ (.) က string value ႏစခက အတတကြျဖစသြားေအာင လပဖ႔အတြက အသးျပပါတယ။ dot (.) operator ကအသးျပျပး string ႏစခက ေပါငးတ႔ပစက ေအာကမာၾကညပါ။

<?php$txt1="Hello World";$txt2="12345";echo $txt1 . " " . $txt2;?>

သ႔ရ႕ output ကေတာ႔Hello World 12345 ပျဖစပါတယ။ က ေနာတ႔ရ႕ code ကၾကညမယဆရင concatenation operator(.) က ႏစၾကမအသးျပထားတာကေတြ႔မာျဖစပါတယ။ တတယ string ျဖစတ႔ “ ”(empty space)ကထညခငလ႔ပျဖစပါတယ။

Using the strlen() function

strlen() function က string ရ႕အရည length ကသရလတ႔အခါမာ အသးျပပါတယ။ “Hello World!” ဆတ႔ string ရ႕ length ကရာမယဆရင

PHP Guide Book Moe Myint Shein

9 Youth Dreams 9

Page 10: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

<?phpecho strlen("Hello World!");?>သ႔ရ႕ output ကေတာ႔ 12 ပျဖစပါတယ။ string ရ႕ length က looping ေတြန႔ တျခား functionအေတာမားမားမာအသးျပဖ႔လအပပါတယ။

Using the strpos() function

strpos() function က string တစခအထမာရတ႔ character တစခ ဒါမမဟတ ေနာက string တစခက ရာဖ႔အတြက အသးျပပါတယ။

<?phpecho strpos("Hello World!", "World");?>

သ႔ရ႕ output ကေတာ႔ 6 ပျဖစပါတယ။World ရ႕ေနရာ တနညးအားျဖင W ရ႕ေနရာက 6 ျဖစေနပါတယ။ ဘာလ႔ 7မျဖစရတာလဆေတာ႔ string တစခရ႕ ပထမ position ဟာ 1 မဟတပါဘး 0 ျဖစလ႔ပါပ။ PHP မာ string အတြက functionေပါငးေျမာကမားစြာရပါတယ။ အခေတာ႔ အေျခချဖစလ႔ ဒေလာကထပထညလကပါတယ။

PHP Operators

ေအာကမာ PHP မာသးတ႔ operator ေတြပျဖစပါတယ။Arithmetic Operators

Arithmetic Operators ေတြအေနန႔ ဥပမာတစခေလာက run ၾကညပါမယ။

<?php$no1=2;

PHP Guide Book Moe Myint Shein

10 Youth Dreams 10

Page 11: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

$no2=3;$no3= $no1+$no2;echo $no3;?>ဒ code က run ရင output 5 ရပါမယ။

အေပါငး၊ အႏႈတ၊ အေျမာက၊ ကေတာ႔ ရငးပါတယ။ Division ဆတ႔ အစားမာေတာ႔15 က 5 န႔စားရင သးရပါမယ။ 5 က 2န႔စားရင 2.5 ရလဒရပါမယ။

<?php$no1=5;$no2=2;$no3= $no1/$no2;echo $no3;?>

Modulus ဆတာကေတာ႔ အၾကြငးက ရာတာျဖစပါတယ။5%2 ဆရင 1 ရပါမယ .. 10%2 ဆရင 0 ရပါမယ။

<?php$no1=5;$no2=2;$no3= $no1%$no2;echo $no3;?>

Assignment Operators

Assignment Operators ေတြကေတာ႔ အတေရးတယလ႔ေျပာရမာပါ..။ PHP မာသးတ႔ ပစေတြေပါ႔။ x=x+y ဆရင xန႔ y န႔ေပါငးပါ ရတ႔တနဖးက x ထထညပါလ႔ဆလပါတယ။ အဒါက PHP မာ assignment operatorသးျပးေတာ႔ x+=y လ႔ေရးပါတယ။ဥပမာ တစပဒေလာက run ၾကညပါ။

PHP Guide Book Moe Myint Shein

11 Youth Dreams 11

Page 12: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

<?php$x=5;$y=2;$z= $x+=$y;echo $z;?>Output က 7 ရပါမယ။

Comparison Operators

Comoparison Operators ေတြကေတာ႔ တနဖးျခငးႏႈငးယဥပါမယ … အမနန႔ အမားဆျပး ရလဒႏစခ ျပနထတေပးပါတယ။== ဆရင ညသလားလ႔စစပါတယ။ ဥပမာ 5 က 8 န႔ညသလားဆရင 5==8 လ႔စစပါမယ result ကေတာ႔ falseထြကပါမယ။

$x=5;$y=2;if ($x==$y)echo “true”;else echo “false”;?>အထကပါ code က run မယဆ 5 န႔ 2 န႔ မညတ႔အတြက false ရပါမယ။

ေအာကကအတငး variable x ေရာ y ေရာ3 ဆျပး တေနတ႔အတြက run တ႔အခါ output true ရပါမယ။$x=3;$y=3;if ($x==$y)echo "true";else echo "false";?>

PHP Guide Book Moe Myint Shein

12 Youth Dreams 12

Page 13: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

Logical Operators

Logical Operators ေတြအတြကလညး အထကပါ ဇယားကၾကညရင နားလညမာပါ.. ဒ Operator ေတြက အေရ႕မာ တျဖညးျဖညးန႔အသးျပရငး ေသခာနားလညလာပါလမ႔မယ။

PHP Guide Book Moe Myint Shein

13 Youth Dreams 13

Page 14: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

Chapter 3.Flow Control Functions in PHP

PHP If… Else Statements

PHP မာ if, elseif န႔ else statements ေတြ က မတညတ႔ condition ေတြေပၚမာ မတညတ႔ လပေဆာငခကေတြက ေဆာငရြကဖ႔အတြက အသးျပပါတယ။

Conditional Statements

ကေနာတ႔ coding ေရးတ႔အခါမာ မၾကာခဏ ဆသလ မတညတ႔ ဆးျဖတခကေတြအတြက မတညတ႔ လပေဆာငမႈေတြက ေဆာငရြကလၾကပါတယ။ အဒလလပေဆာငႏငဖ႔အတြက conditional statements ေတြကအသးျပၾကရပါတယ။

The if…else statementအေျခအေနတစခက မနတယဆရင ဒ code ကဆက run ပါ.. တကယလ႔ မားတယဆရင ဒဘကက code ကဆက runပါဆတာမးအတြက ဒ if…else statement ကအသးျပပါတယ။ သ႔ရ႕ syntax ကေတာ႔ ေအာကေဖာျပပါပစမးျဖစပါတယ။if (condition)code to be executed if condition is true;

elsecode to be executed if condition is false;

ဒဟာက ဥပမာတစခန႔ၾကညမယဆရင<html><body><?php$d=date("D");if ($d=="Fri")echo "Have a nice weekend!";elseecho "Have a nice day!";?></body></html>

ဒ code က run ၾကညမယဆရင တကယလ႔ ဒေန႔က ေသာၾကာေန႔ျဖစရင Have a nice weekend လ႔ေပၚမာျဖစျပးေတာ႔ တျခားေန႔ဆရငေတာ႔ Have a nice day လ႔ေပၚမာျဖစပါတယ။ ကေနာအခ run တာေတာ႔ Have a nice weekendလ႔ေပၚပါတယ ဒေန႔က ေသာၾကာေန႔ျဖစေနလ႔ပါ.. အဒါန႔ Computer ရ႕ System Date က Tuesday လ႔ေျပာငးျပး ကေနာျပန run ပါတယ။ Have a nice day လ႔ေပၚပါတယခငဗာ… :) ။

PHP Guide Book Moe Myint Shein

14 Youth Dreams 14

Page 15: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

တကယလ႔မား condition တစခက မနတယ ဒါမမဟတ မားတယဆတ႔အခါမာ တစေၾကာငးထကပတ႔ code ေတြက runေစခငတယဆရင ေတာ႔ curly braces ဆတ႔ { } ထမာထညထားရမာျဖစပါတယ။ဥပမာ…

<html><body><?php$d=date("D");if ($d=="Fri"){echo "Hello! <br/>";echo "Have a nice weekend!";echo " See you on Monday!";}?></body></html>ဒ code ေလးရ႕ output ကေတာ႔Hello!Have a nice weekend! See you on Monday!ျဖစပါတယခငဗာ။Computer System Date က ေသာၾကာျဖစမေနဘးဆရငေတာ႔ေပၚမာမဟတဘးေနာ..။

The elseif statementတစခထကပတ႔ condition ေတြအမားၾကးထက တစခခကမနေနရင code က run ေစခငတယဆရင elseif statementကအသးျပပါတယ။သ႔ရ႕ syntax ကေတာ႔

if (condition)code to be executed if condition is true;

elseif (condition)code to be executed if condition is true;

elsecode to be executed if condition is false;

ဒလပစ ျဖစျပးေတာ႔ ဥပမာျပရရင…

<html><body>

PHP Guide Book Moe Myint Shein

15 Youth Dreams 15

Page 16: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

<?php$d=date("D");if ($d=="Fri")echo "Have a nice weekend!";elseif ($d=="Sun")echo "Have a nice Sunday!";elseecho "Have a nice day!";?></body></html>

တကယလ႔ဒေန႔က ေသာၾကာျဖစရင Have a nice weekend! လ႔ျပပါမယ။ တနဂၤေႏြျဖစေနရင Have a nice Sunday! လ႔ output ျပမာျဖစ ျပးေတာ႔ တျခားေန႔တစရကရကဆရငေတာ႔ Have a nice day! လ႔ပျပမာျဖစပါတယ။

The Switch Statement

အေျခအေနတစခကမတညထားမယ အဒအေျခအေနေပၚမာမတညျပး မနကနတ႔အခါ ဘယ code က run ရမယ … ဘယ case န႔ကကညတယဆတာက စစတာျဖစပါတယ။ switch case statement ကသးတ႔အခါမာ if…elseif…else statementေတြအရညၾကးသးရတာေတြကေနလညး သကသာေစပါတယ။သ႔ရ႕ Syntax ကေအာကေဖာျပပါအတငးျဖစပါတယ။

switch (expression){case label1:code to be executed if expression = label1;break;

case label2:code to be executed if expression = label2;break;

default:code to be executedif expression is differentfrom both label1 and label2;

}

ဥပမာက run ၾကညမသာ သေဘာေပါကႏငမာျဖစပါတယ။

<html><body><?php

PHP Guide Book Moe Myint Shein

16 Youth Dreams 16

Page 17: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

$x=4;switch ($x){case 1:echo "Number 1";break;case 2:echo "Number 2";break;case 3:echo "Number 3";break;default:echo "No number between 1 and 3";}?></body></html>

ဒ code က run လကရင No number between 1 and 3 လ႔ရမာျဖစပါတယ။ $x=4လ႔တနဖးသကမတေပးထားခ႔တာေၾကာငပါ..။ တကယလ႔ $x=2 လ႔ assign လပခ႔ရင Number 2 လ႔ outputျပမာျဖစပါတယ။ သ႔ရ႕ အလပလပပကေတာ႔ single expression တစခ အမားအားျဖငေတာ႔ variable တစလးက တစၾကမ evaluate လပတယ .. code ေတြထမာပါတ႔ case တစခစထက တနဖးန႔ expression ရ႕ တနဖးန႔က ႏႈငးယဥတယ။ ကကညတယဆလ႔ရရင အဒ case န႔ဆကႏြယတ႔ code က execute (run) တယ။ execute လပျပးတ႔အခါ ေနာက caseတစခက ဆကျပး မ run ေတာ႔ေလေအာင break ကသးျပးထြကတယ။ တကယလ႔ case ေတြအားလးက မမနဘးဆရင default statement က execute လပသြားမာပျဖစပါတယ။

PHP Arrays

array တစခက variable name တစခေလးထညးမာပ တစခသ႔မဟတ တစခထကပတ႔ တနဖးေတြက သမးဆညးေပးပါတယ။ကေနာတ႔အေနန႔ တခါတရမာ ဆငတတ႔ variable ေတြအမားၾကးက ဖနတးလတ႔အခါမးရလာပါတယ။ အဒအခါမာ variable ေတြအမားၾကး မဖနတးေတာ႔ပန႔ array တစခထမာပ elements ေတြအေနန႔ သမးလကႏငပါတယ။ arrayထမာရတ႔ element တစခစမာ သ႔ရ႕ ကယပင အမတ (ID) ရမာျဖစတ႔အတြက လြယကစြာေခၚသးႏငပါတယ။

array အမးအစား သးမးရပါတယ။-Numeric array – ID key ကနပါတန႔ေပးတ႔ array မး-Associative array- ID key ေတြတစခစတငးက တနဖးတစခန႔ ဆကစပေနတ႔ array မး

PHP Guide Book Moe Myint Shein

17 Youth Dreams 17

Page 18: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

-Multidimensional array- တစခ သ႔မဟတ တစခထကပတ႔ array ေတြပါ၀ငေနတ႔ array မးနားလညသြားေအာင တစခခငးစက ဥပမာန႔ၾကညၾကပါမယ။

Numeric array

numeric array တစခက နညးလမးအမးမးန႔ ဖနတးႏငပါတယ။“

$names = array("MgMg","MaMa","KoKo");

ဒ စာေၾကာငးမာဆရင ID key ကအလအေလ ာကသတမတေပးမာျဖစပါတယ။ေအာကက code မာေတာ႔ က ေနာတ႔က manual သတမတေပးတာျဖစပါတယ။

$names[0] = "MgMg";$names[1] = "MaMa";$names[2] = "KoKo";

အဒ ႏစမးစာလးရ႕ အကးသကေရာကမႈကေတာ႔ အတတပါပ။ PHP coding ထမာ ထညသးၾကညမယဆရင…

<?php>$names[0] = "MgMg";$names[1] = "MaMa";$names[2] = "KoKo";echo $names[1] . " and " . $names[2] . " are " . $names[0] . "'s neighbours";?>output အေနန႔ MaMa and KoKo are MgMg's neighbours ဆျပး ရလာမာျဖစပါတယ။

Associative Arrays

associative array မာ ID key တစခစဟာ တနဖးတစခစန႔ဆကႏြယပါတယ။ တကတ႔ တနဖး တစခ႕န႔ပတသတတ႔ ေဒတာေတြက သမးဆညးတ႔အခါ numerical array ကအျမတမးအေကာငးဆးနညးလမးမဟတပါဘး…။ associativearrays ကသးတ႔အခါမာ တနဖးေတြက key အေနန႔ပါ သးႏငျပးေတာ႔ array မာ တနဖးေတြက assign လပႏငပါတယ။ ရငးသြားေအာင ဥပမာကၾကညပါမယ။ဒဥပမာမာက ေနာတ႔ဟာ လတစေယာကစက သတ႔ရ႕အသကက array ကသးျပး assign (တနဖးသတမတ ) ပါတယ။

$ages = array ("MgMg"=>18,"MaMa"=>20,"KoKo"=>19);

ေအာကကပစန႔လညးေရးႏငပါတယ.. အဓပၸါယကေတာ႔အတတပါပ ေရးတ႔ပစကြာသြားတာပါ။ သကလညး arrayကဖနတးတာပါပ။

$ages['MgMg'] = "18";

PHP Guide Book Moe Myint Shein

18 Youth Dreams 18

Page 19: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

$ages['MaMa'] = "20";$ages['KoKo'] = "19";

ဒ code ေတြက PHP ထမာ ထညေရးတ႔အခါ…

<?php>$ages['MgMg'] = "18";$ages['MaMa'] = "20";$ages['KoKo'] = "19";echo "KoKo is" . $ages['KoKo'] . " years old.";?>

သ႔ရ႕ output ကေတာ႔ KoKo is19 years old. ပျဖစပါတယ။

Multidimensional Arrays

multidimensional array တစခထမာ အဓက array ၾကးရမယ .. အဒ အဓက array ၾကးထက element တစခစကလ arrayျဖစႏငတယ။ အဒ sub array တစခစထက elelment ေတြက လညး array ထပျဖစႏငတယ .. ထပဆငထပဆငေပါ႔…။

<?php$families = array("Griffin"=>array("Peter","Lois","Megan"),"Quagmire"=>array("Glenn"),"Brown"=>array("Cleveland","Loretta","Junior")

);echo "Is " . $families['Griffin'][2] ." a part of the Griffin family?";

PHP Guide Book Moe Myint Shein

19 Youth Dreams 19

Page 20: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

?>အထကပါ php code ကၾကညရင $families ဆတ႔ variable ထမာ array တစခေဆာကပါတယ။ Griffin, Quagmire,Brown ဆတ႔သးခပါ၀ငပါတယ။ Griffin ရ႕အထမာ Peter, Lois, Megan ဆတ႔ array သးခ ထပဆငေဆာကထားသလ Quagmire ထမာ Glenn Brown ထမာCleveland, Loretta, Junior ဆျပးသးခထပပါေနတာေတြ႕ရပါတယ။ ဒဟာက runလကရင Output က Is Megan a part of the Griffin family?ရမာျဖစပါတယ။ Griffin array ရ႕ နပါတႏစအခနးက Megan ျဖစပါတယ။ Peter က နပါတ ၀ ၊ Lois က နပါတ ၁ျဖစပါတယ။

PHP Looping

code အပငးေလးတစခက အၾကမမားစြာ run ဖ႔အလရတ႔အခါ looping ကသးရပါတယ။ PHP မာ ေအာကေဖာျပပါ loopingstatements မားရပါတယ။while - အေျခအေနတစခမနေနပါက ေဖာျပပါ code က run ပါမယ … မနေနသေရႊ႕ဆက run ေနမာျဖစပါတယ။do…while - တစၾကမေတာ႔ ေဖာျပပါ code က ေသခာေပါက run ပါမယ ..။ အဒေနာကမာမ အေျခအေနက မနေနသေရႊ႕ထပခါ ထပခါ ဆက run ပါမယ။for - သကမတထားတ႔အၾကမအေရအတြကအတငး ေဖာျပပါ code က ထပခါထပခါ run ေနပါမယ။foreach – array တစခထမာရတ႔ element တစခစအတြက ေဖာျပပါcode က run ပါမယ။တစခခငးဆကအေသးစတၾကညၾကရေအာင…။

The while Statementအေျခအေနတစခမနေနပါက ေဖာျပပါ code က run ပါမယ … မနေနသေရႊ႕ဆက run ေနမာျဖစပါတယ။Syntaxwhile (condition)code to be executed;

Exampleေဖာျပပါ code ေလးက variable i အေနန႔ ၅ ထက ငယေန ဒါမမဟတ ညေနသေရႊ႕ ဆကျပးေတာ႔ တစၾကမျပးတစၾကမ looping ပတ ေနမာျဖစပါတယ။ တစၾကမ loop ပတ ျပးတငးမာ i ရ႕တနဖးက ၁ တးတးလာပါမယ။

<html><body><?php$i=1;while ($i<=5){echo "The number is " . $i . "<br/>";$i++;}?>

PHP Guide Book Moe Myint Shein

20 Youth Dreams 20

Page 21: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

</body></html>

output အေနန႔-

The number is 1The number is 2The number is 3The number is 4The number is 5

ဆျပးရလာမာပါ…။ အရငဆး variable i ေလးက 1 assign လပထားေပးပါတယ..။ အဒေနာက i က ငါးထကငယလား ညလားလ႔စစပါတယ .. i ရ႕တနဖးက ၁ ျဖစေနတ႔အတြက ငယပါတယ ဒါေၾကာင { } ထက code ေတြက အလပလပပါတယ။ The number is 1 ဆတ႔တစေၾကာငး output ထြကပါတယ … <br/> ေၾကာင ေအာကတစေၾကာငးဆငးပါတယ.. i ရ႕တနဖးက တစတးတ႔အတြက i တနဖးက 2 ျဖစသြားပါတယ .. } ကေရာကတ႔အခါ while မာတစခါထပသြားစစပါတယ .. i က ငါးထက ငယေနေသးတ႔ အတြက ထပအလပလပပါတယ.. The number is 2ဆျပးထြကပါတယ ..။ ဒလန႔ i ရ႕တနဖး ၆ ျဖစသြားတ႔အထလပပါတယ … ၆ ျဖစသြားတ႔အခါ while မာထပစစေတာ႔ ငါးထကမငယသလ မညေတာ႔တ႔အတြက.. { } ထက code ကအလပမလပေတာ႔ပါဘး..နားလညမယလ႔ေမာလငပါတယ…။

The do…while Statement

တစၾကမေတာ႔ ေဖာျပပါ code က ေသခာေပါက run ပါမယ..။ အဒေနာကမာမ အေျခအေနက မားေနရင ဆက မ runေတာ႔ပါဘး.. မနေနသေရႊ႕ေတာ႔ ထပခါ ထပခါ ဆက run ပါမယ။Syntax

do{code to be executed;}while (condition);

Example

ေဖာျပပါ ဥပမာမာ အရငဥးဆး a ကတစတးပါတယ အဒေနာကမာ a ရ႕တနဖးက ၅ ထကငယေနသေရႊ႕ ဆကျပး တစတးသြားမာ ျဖစပါတယ။

<html>

PHP Guide Book Moe Myint Shein

21 Youth Dreams 21

Page 22: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

<body><?php$a=0;do{$a++;echo "The number is " . $a . "<br/>";}while ($a<5);?></body></html>

အေသးစတအလပလပပကေတာ႔ variable a ထက သညထညပါတယ အဒေနာက do {} ထက codeေတြကအလပလပပါတယ .. a က တစတးတ႔အတြက a က တစျဖစသြားပါတယ… echo ေၾကာင The number is 1လ႔ထြကမာျဖစပါတယ..။ <br/> ေၾကာင ေအာကတစလငးဆငးပါတယ ..။ a ကငါးထကငယေနေသးတ႔အတြက doဆကျပနသြားျပး do { } ကထပ အလပလပပါတယ ..။a ကငါးထကၾကးသြားတ႔အခါၾကမ looping ကေနထြကသြားပါတယ ..သ႕ရ႕ output က…

The number is 1The number is 2The number is 3The number is 4The number is 5ပျဖစပါတယ။

The for Statement

ရးရးေျပာရရငေတာ႔ code အပငးေလးတစခက ဘယႏစၾကမ run ေစခငတယဆတာမးကက ေနာတ႔ သထားတ႔အခါမာ forstatement ကသးပါတယ။ သ႔ရ႕ syntax ကေတာ႔for (init; cond; incr){code to be executed;}init ဆတာက initial value ကေျပာတာပါ.. မလအစတနဖးေပါ႔… cond ကေတာ႔ condition ေပါ႔… အဒ conditionမနေနသမ အလပလပပါမယ…မားသြားတာန႔ loop ထကေနထြကပါမယ။ incr ကေတာ႔ မလတနဖး init က တးေပးတ႔ increment ေပါ႔… ။ေအာကကေတာ႔ Hellow World! ဆတ႔ စာသားက ဆယၾကမ ျပေပးမယ႔ ဥပမာေလးပါ…

<html><body>

PHP Guide Book Moe Myint Shein

22 Youth Dreams 22

Page 23: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

<?phpfor ( $i=1; $i<=10; $i++)

{echo "Hello World!<br />";}?></body></html>

variable i ရ႕မလတနဖးက 1 လ႔ေပးျပးစလကပါတယ i က 10 ထက ငယလား ညေနလား စစပါတယ.. မနတ႔အတြက HelloWorld! တစၾကမျပပါတယ ေအာကတစေၾကာငးဆငးပါတယ.. i ရ႕တနဖးက တစတးပါတယ ..။ i က 2 ျဖစသြားပါတယ.. 10ထကငယလား ညလား ထပျပးစစပါတယ .. ငယေနေသးတ႔အတြက ေနာကထပ Hello World! တစေၾကာငးျပပါမယ ..ဒါေပမယ႔ program မျပးေသးတ႔အတြက output ေတြကမျပဘးေနာ.. အကနျပးသြားမ တခါတညးျပမာ…။ ဟတပါျပ…အဒလန႔ i ကတစတးလက 10 ထကငယလား ညလား စစလကန႔ ေနာကဆး i ရ႕တနဖး 11 ျဖစသြားတ႔အခါမာမ.. Loopingကေနထြကသြားပါတယ..။ output အေနန႔ Hello World! ဆတ႔ စာသား ဆယေၾကာငးရရပါတယ။

The foreach Statementforeach statement ကေတာ႔ array ေတြ က looping ပတဖ႔အတြကအသးျပပါတယ။ သ႔ရ႕ Syntax (ပစ) ကေတာ႔

foreach (array as value){code to be executed;}ျဖစပါတယ။ ေအာကက ဥပမာကၾကညရငနားလညမာပါ။

<html><body><?php$arr=array("one", "two", "three");foreach ($arr as $value){

echo "Value: " . $value. "<br />";}?></body></html>

output အေနန႔-

PHP Guide Book Moe Myint Shein

23 Youth Dreams 23

Page 24: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

Value: oneValue: twoValue: three

ဆျပးရပါမယ။looping တစခစတငးအတြက array element ရ႕တနဖးက $value ထက assign လပတယ။ array ရ႕ pointerကတစေနရာျပးတစေနရာေရႊ႕သြားတယ။ ေနာကတစၾကမထပပတတ႔အခါ ေနာကထပ elementတစခေပၚမာအလပလပပါတယ။

PHP Guide Book Moe Myint Shein

24 Youth Dreams 24

Page 25: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

Chapter 4.Working With Functions & Forms

PHP Functions

အမနတကယ PHP ရ႕စြမးအားက function ေတြပါပ..။ PHP မာ built-in နဂတညးကပါတ႔ functions ေပါငး 700ေက ာရပါတယ။ဒ ကတရရယမာေတာ႔ function တစခကဘယလဖနတးတယဆတာကေရးသားသြားပါမယ။

Creating a PHP Function

function ဆတာ code အစအေ၀းတစခပါပ.. က ေနာတ႔လအပတ႔အခနမာသ႕က ေခၚျပးသးရမာျဖစပါတယ။function န႔ပတသတျပးမတသားရမာေတြကေတာ႔ -- function ေတြအားလးက “function ()” ဆတ႔စကားလးန႔ စတငရပါမယ။- function ကအမညေပးတ႔ေနရာမာ သ႔ရ႕နာမညကၾကညလကတာန႔ ဒ functionဟာဘာလပတ႔ဟာလညးဆတာက သသာထငရားေအာငအမညေပးသငပါတယ။ နာမညက letter ဒါမမဟတ underscoreန႔စတငသငပါတယ နပါတန႔မဟတပါဘး။- “{ ”က function ရ႕အစမာထညရပါမယ။“{ ” ရ႕အေနာကမာေတာ႔ function ရ႕ code ေတြကေရးရပါမယ ..။- function ကပတတ႔အခါ “}” န႔ပတရပါမယ။

အရငးဆးပစန႔ function ရ႕ပစေလးကေရးၾကညပါမယ။Example

<html><body><?phpfunction writeMyName(){

echo "Moe Myint Shein";}writeMyName();?></body></html>

writeMyName() ဆတ႔ function ေလးကအရငေဆာကလကတယ .. သ႔ရ႕ code ကတစေၾကာငးပရပါတယ။ နာမညက screen မာျပေပးမာပါ။ ဒအတငးပထားလ႔ ရရင မေပၚပါဘး.. function ကပတတယ။ ျပးမ writeMyName();ဆျပးေခၚလကေတာ႔မ အလပလပတာျဖစပါတယ။ပျပးနားလညသြားေအာငေနာကထပ ဥပမာတစခထပေရးပါမယ။

<html><body>

PHP Guide Book Moe Myint Shein

25 Youth Dreams 25

Page 26: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

<?phpfunction writeMyName(){

echo "Moe Myint Shein";}echo "Hello World! <br />";echo "My name is ";writeMyName();echo ". <br /> That's right, ";writeMyName();echo " is my name.";?></body></html>

သ႔ရ႕ output ကေတာ႔

Hello World!My name is Moe Myint Shein.That's right, Moe Myint Shein is my name.

အခေလာကဆ function ရ႕အလပလပပကနားလညမယလ႔ေမာလငပါတယ။

PHP Functions- Adding parameters

အခက ေနာတ႔ေရးလကတ႔ writeMyName() ဆတ႔ function ေလးကေတာ႔ အရးရငးဆးပစေလးပါ…။ သက stringေလးတစခကပ echo လပေပးတာေလးပါ။ function တစခကပမသက၀ငလႈပရားလာေအာင parametersေတြကထညေပးႏငပါတယ။ parameter ဆတာvariable လသေဘာတရားပါပ။ writeMyName() ဆတ႔ function nameကၾကညမယဆရင parentheses လ႔ေခၚတ႔ () ကသတထားမမာပါ။ paramenter ေတြက အဒ parenthesesရ႕အထမာထညေပးရမာျဖစပါတယ။Example 1အခဥပမာမာ တညတ႔မးရးနာမညေတြန႔ မတညတ႔ first name န႔ကေရးမာျဖစပါတယ။

<html><body><?phpfunction writeMyName($fname){

echo $fname. "Moe. <br />";}echo "My name is ";

PHP Guide Book Moe Myint Shein

26 Youth Dreams 26

Page 27: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

writeMyName("Shwe");echo "My name is ";writeMyName("Ngwe");echo "My name is ";writeMyName("Sein");?></body></html>

အခလ output ထြကလာပါမယ။

My name is ShweMoe.My name is NgweMoe.My name is SeinMoe

အလပလပပကေတာ႔ writeMyName () ဆတ႔ function ထမာ $fname ဆတ႔ parameter တစလးထညေပးထားပါတယ။function ထမာပါ၀ငတ႔ code ကလညးတစေၾကာငးတညးပါပ.. $fname ဆတ႔parameter ရ႕တနဖးရယ သ႔ရ႕ေနာကမာ Moe ဆတ႔ မးရးနာမညရယက echo န႔ရကခငးျပး ေအာကတစေၾကာငးဆငးေစထားတာျဖစပါတယ။ က ေနာတ႔က writeMyName() ဆတ႔ function က Shwe ဆတ႔ parameter ေလးကထညေစျပးေတာ႔ writeMyName(“Shwe”);ဆျပးလမးေခၚလကတ႔အခါ $fname ရ႕တနဖးက Shwe ျဖစသြားျပးေတာ႔ ShweMoe ဆတ႔ နာမညကျဖစသြားေစပါတယ။ ကနတာေတြလညးအလားတပါပ။အခဆကျပးေတာ႔ parameter ႏစလးေပးတ႔ ဥပမာကၾကညၾကပါမယ။

<html><body><?phpfunction writeMyName($fname, $punctuation){

echo $fname. "Moe". $punctuation . "<br />";}echo "My name is ";writeMyName("Shwe", ".");echo "My name is ";writeMyName("Ngwe","!");echo "My name is ";writeMyName("Sein","...");?></body></html>run လကတ႔အခါ အခအတငးထြကလာပါတယ။

PHP Guide Book Moe Myint Shein

27 Youth Dreams 27

Page 28: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

My name is ShweMoe.My name is NgweMoe!My name is SeinMoe...ပထမဥပမာအတငး အပါအ၀င $punctuation ဆတ႔ parameter ေနရာမာပါ “.” , “!”, န႔ “…” တ႔က functionကေခၚလကတ႔အခါတငး တလညစ ေနရာ၀ငယသြားတာျဖစပါတယ။

PHP Functions – Return Values

function ေတြက တနဖးေတြက return ျပနဖ႔အတြကလညး အသးျပႏငပါတယ။

<html><body><?phpfunction add($x,$y){

$total= $x + $y;return $total;

}echo "1+16=" . add(1,16);?></body></html>output - 1+16=17 ျဖစပါတယ။add () function ထမာ $x န႔ $y parameter ႏစလးပါပါတယ။ အဒ parameter ႏစလးက ရလာမယ႔ တနဖးေတြက ေပါငးျပး $total ထကထညတယ .. အဒေနာက $total က return ျပနပါတယ.. add() functionကေခၚလကရင $total ရ႕တနဖးကျပေပးမာျဖစပါတယ။ add(1,16); ဆျပးေတာ႔ function ကေခၚလကတ႔အခါမာ…function အလပလပပါျပ.. 1 န႔ 16 န႔ကေပါငးျပးေတာ႔ $total ထက 17 ေရာကသြားတယ.. အဒတနဖးက returnျပနထားတာျဖစတ႔အတြက output မာ 1+16=17 ဆျပးရလာတာျဖစပါတယ။

PHP Forms and User Inputuser ကေရးသြငးလကတ႔အခကအလက တနညးအားျဖင Form ေတြက အခကအလကေတြက ရယဖ႔အတြက PHP မာ $_GET န႔ $_POST ဆတ႔ variable ေတြက အသးျပပါတယ။ HTML form ေတြ PHP form ေတြန႔အလပလပတ႔အခါ အေရးအၾကးဆးသတထားရမာကေတာ႔ HTML Page တစခထမာ form element ေတြအကနလးဟာ PHP code ထမာ အလအေလ ာက အသးျပႏငမာျဖစပါတယ။ဥပမာ-

<html><body><form action = "welcome.php" method="post">Name: <input type="text" name="name"/>Age:<input type="text" name ="age"/><input type="submit"/></form>

PHP Guide Book Moe Myint Shein

28 Youth Dreams 28

Page 29: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

</body></html>

ဒ code ေလးက form1.html လ႔ save လကပါမယ။ဒဥပမာေလးမာ input field ႏစခန႔ button ေလးတစခပါပါတယ။ userက form ထမာျဖညျပးေတာ႔ submit button ကႏပလကရင form ထက အခကအလကေတြက welcome.php ဆတ႔ fileေလးဆကပ႔ေပးတာျဖစပါတယ။ အခေတာ႔ welcome.php မေရးရေသးတ႔အတြက button ကႏပလကရင object notfound လ႔ေပၚပါလမ႔မယ။ welcome.php ဖငေလးကပါေရးလကပါ မယ။

<html><body>Welcome <?php echo $_POST ["name"]; ?>.<br />You are <?php echo $_POST ["age"]; ?> years old.</body></html>

ဒါေလးက welcome.php လ႔အမညေပးျပး ခနက form1.html န႔ေနရာအတတ folder တစခထညးမာ save လကမယ ..ျပးရင form1.html က http://localhost/php/form1.html လ႔ေခၚျပး run မယ အမညန႔ အသကက ထညျပး submitbutton ႏပလကရင ဒလပစမး output ရလာပါလမ႔မယ။

Welcome Moe Myint SheinYou are 20 years old.

PHP $_GET

PHP $_GET variable က get method က သးျပးေတာ႔ form တစခက တနဖးေတြက ရယဖ႔အတြက အသးျပပါတယ။$_GET variable က HTTP GET method ကေနပ႔လကတ႔ variable names ေတြန႔ တနဖးေတြရ႕ array တစချဖစပါတယ။GET method ကအသးျပျပး အခကအလကေတြက ပ႔တ႔အခါမာ (browser ရ႕ address bar မာျပလ႔ရရင)လတငးျမငရပါတယ။ ျပးေတာ႔ အမားဆး 100 characters ပပ႔လ႔ရတယဆတ႔ ကန႔သန႔ခကရပါတယ။Example<form action ="welcome1.php" method ="get">Name:<input type="text" name = "name1"/>Age: <input type= "text" name = "age1"/><input type="submit"/></form>

ဒ code ေလးက form2.php လ႔ save လကပါတယ။(က ေနာကေတာ႔ စာအပရ႕အစပငးမာေျပာခ႔သလပ php န႔ html fileေတြအားလးက C: ေအာကက xampp ေအာကက htdocs ေအာကက php ရ႕ေအာကထမာပ save တာျဖစပါတယ။ ဒါမ http://localhost/php/form2.php လ႔ browser address bar မာ ေခၚျပး run ရတာျဖစပါတယ ) welcome1.php ဆတ႔ fileကေအာကပါအတငးေရးျပးေတာ႔ save လကပါတယWelcome <?php echo $_GET["name1"]; ?>.<br />

PHP Guide Book Moe Myint Shein

29 Youth Dreams 29

Page 30: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

You are <?php echo $_GET["age1"]; ?> years old!

အခအခနမာ form2.php က run လကတ႔ပါတယ။ text box ႏစခကျဖညျပးေတာ႔ submit button ႏပလကတ႔အခါ address bar မာ http://localhost/php/welcome1.php?name1=Moe+Myint+Shein&age1=20 ဆျပးေတာ႔ ေပၚသြားတာေတြ႔ရပါတယ .. welcome1.php ဖငထမာ name1 န႔ age1 ရ႕တနဖးေတြက $GET variable န႔ catchလပထားတ႔အတြက browser မာ အခလျပပါတယ။

Welcome moemyintshein.You are 20 years old!

$_GET variable ကအသးျပတ႔အခါ variable name ေတြန႔ တနဖးေတြက address bar မာျပတာကခရမာျဖစတ႔အတြက password ေတြန႔ လသမခေစလတ႔ အခကအလကေတြက ပ႔ေပးတ႔အခါမာ မသးသငပါဘး… ဒါေပမယ႔ တခ႕ေနရာေတြမာေတာ႔ အခလ variable ေတြက URL မာျမငရတ႔အတြက အကးရပါတယ။ အရမးၾကးတ႔ variable valuesေတြကလညးမကငတြယႏငပါဘး character အလး 100 ထကပလ႔မရလ႔ပါ။THE $ REQUEST Variable$_REQUEST variable က $_GET, $_POST န႔ $_COOKIE တ႔အလပလပသလလပေပးပါတယ။ Get method န႔ျဖစျဖစ POST method န႔ျဖစျဖစ ပ႔လကတ႔data ေတြရ႕ result ကရရေစဖ႔အတြက အသးျပႏငပါတယ။အကယလ႔ ခနက welcome1.php ဖငေလးမာ $_GET ေနရာမာ $_REQUEST လ႔ေအာကပါအတငးေျပာငးေရးရငလညး အတတပအလပလပတာျဖစပါတယ။

Welcome <?php echo $_REQUEST["name1"]; ?>.<br />You are <?php echo $_REQUEST["age1"]; ?> years old!

PHP $_POST

$_POST variable ကေတာ႔ HTTP POST method ကသးျပး variable name န႔ values ေတြရ႕ array တစခပျဖစပါတယ။ method= “post” ဆျပးေတာ႔ form တစခက တနဖးေတြက ရယဖ႔အတြက သးပါတယ။ POST method ကအသးျပရင ပ႔လကတ႔ အခကအလကေတြက browser address bar မာမျမငရတ႔အျပင အကန႔အသန႔မရလညး ပ႔ႏငပါတယ။ကေနာကေတာ႔ form3.php လ႔ save ပါတယ။

Example<form action="welcome2.php" method="post">Enter your name: <input type="text" name="name" />Enter your age: <input type="text" name="age" /><input type="submit" /></form>

welcome2.php ရ႕ code မာ POST ကသးျပးလကခပါမယ။

Welcome <?php echo $_POST["name"]; ?>.<br />You are <?php echo $_POST["age"]; ?> years old!

PHP Guide Book Moe Myint Shein

30 Youth Dreams 30

Page 31: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

form3.php က run ျပးေတာ႔ အမည အသကတ႔ကျဖညျပး submit လပတ႔အခါ GET method န႔မတပ address bar မာhttp://localhost/php/welcome2.php ဒလေလးပျပေပးပါတယ။ output ကေတာ႔ အထကက GET methodတနးကလပျဖစမာပါ။$_REQUEST Variable က $POST ေနရာမာထညျပးသးရငလညး တတပရမာျဖစပါတယ..။ တခါတရမာ GET methodကအသးျပျပးေတာ႔ တခါတရမာ POST method ကသးတာကပသငေလာပါတယ။

PHP Guide Book Moe Myint Shein

31 Youth Dreams 31

Page 32: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

Chapter 5 .PHP Date, Include & File Handling

The PHP Date() Function

အခနန႔ ေန႔ရကေတြက ပစခဖ႔အတြက ဒ function ကအသးျပပါတယ။ သ႔ရ႕ Syntax ကေတာ႔date(format, timestamp) ျဖစပါတယ။ format ဆတ႔ parameter ကေတာ႔ မျဖစမေနပါ၀ငဖ႔လအပပါတယ။ အခနရ႕ format က သကမတေပးတာျဖစပါတယ။ timestamp ကေတာ႔ optional ပါ… default အေနနကေတာ႔ လကရ ေန႔ရကန႔ အခနက timestamp အေနန႔ သးသြားမာျဖစပါတယ။ timestamp ဆတာကေတာ႔ 1970 ဇနန၀ါရ တစရက GMT 00:00:00ကေနစတ႔ စကန႔အေရအတြက ျဖစပါတယ။ Unix Timestamp လ႔လညး ေခၚၾကပါတယ။

date() function ရ႕ ပထမ parameter ျဖစတ႔ format က date/time တစခက ဘယလ format ေပးရမလဆတာ သတမတေပး ပါတယ။ ေန႔ရကန႔ အခနပစေတြက ကယစားျပေပးဖ႔အတြက letter ေတြကအသးျပပါတယ။ အသးျပႏငတ႔ ပစေတြကေတာ႔- d - လတစလရ႕ရက ေတြျဖစပါတယ။ (01-31)- m - လကရလျဖစပါတယ။ (01-12)- Y – digit ေလးလးန႔ေရးတ႔ လကရႏစပျဖစပါတယ။“/”, “.”, “-“ အစရတ႔ character ေတြကလညး letter ေတြၾကားထမာထညသြငး အသးျပႏငပါတယ။<?phpecho date("Y/m/d");echo "<br />";echo date("Y.m.d");echo "<br />";echo date ("Y-m-d");?>အထကပါ code က run လကမယဆရင ေအာကပါပစအတငး output ရလာမာပျဖစပါတယ။2009/09/132009.09.132009-09-13date() function ထမာ format ဆတ႔ parameter တစခထပါ၀ငတ႔ ပစျဖစပါတယ။ timestamp အေနန႔ အခေရာကေနတ႔အခနန႔ေန႔က အလ အေလ ာက ေခၚသးသြားတာျဖစပါတယ။

PHP Date- Adding a Timestamp

date() function ထက ဒတယ parameter ျဖစတ႔ timestamp ကပါထညျပးသးၾကညပါမယ။ အခထပျပးၾကညၾကမယ႔ ဥပမာေလးမာ mktime () ဆတ႔ function ေလးကသးျပးေတာ႔ မနကဖနအတြက timestamp တစခကဖနတးပါမယ။ mktime() function အေနန႔ specified date တစခအတြက Unix timestamp က return ျပနေပးပါတယ။ သ႕ရ႕ Syntaxကေတာ႔mktime(hour, minute, second, month, day, year, is_dat) ျဖစပါတယ။ အနာဂတက မနကဖနျဖစတ႔အတြက တစရကပထပေပါငး ေပးရပါမယ။ mktime() function ရ႕ day ဆတ႔ argument မာ တစရကထပေပါငးေပးလကရန႔ မနကဖန အခနန႔ ေန႔ကေဖာျပမာ ျဖစပါတယ။

PHP Guide Book Moe Myint Shein

32 Youth Dreams 32

Page 33: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

<?php$tomorrow = mktime(0,0,0,date("m"),date("d")+1, date("Y"));echo "Tomorrow is ". date("Y/m/d", $tomorrow);?>သ႕ရ႕ output ကေတာ႔Tomorrow is 2009/09/14 ပျဖစပါတယ။ ဒေန႔ လကရအခနက စကတငဘာ 13 ျဖစတ႔အတြက ျဖစပါတယ။

PHP Include File

Server Side Includes ဆတ႔ SSI က စာမကႏာေပါငးမားစြာေပၚမာ ထပခါထပခါ အသးျပမယ႔ functions ေတြ header ေတြ footer ေတြ elements ေတြက ဖနတးဖ႔အတြက အသးျပပါတယ။

Server Side Includes

include() ဒါမမဟတ require() ဆတ႔ function ကသးျပးေတာ႔ PHP file တစခထက ဆာဗာက မ run ခငမာ file ရ႕ content က ထညေပးႏငပါတယ။ ဒ function ႏစခ ရ႕ အလပလပပက ေနရာအေတာမားမားမာအတတျဖစပါတယ။ ကြသြားတာကေတာ႔ include () function အေနန႕က warning က ထတေပးမယ .. ဒါေပမယ႔ script ကေတာ႔ ဆကျပး execute လပ သြားမာျဖစျပးေတာ႔ require () function ကေတာ႔ error ထတေပးမယ ျပးရင script ကလညး ဆကျပး execute မလပေတာ႔ဘ ရပလကမာျဖစပါတယ။ ဒ function ႏစခက စာမကႏာေတြအမားၾကးေပၚမာ ထပခါထပခါ အသးျပႏငတ႔ function ေတြ header ေတြ footer ေတြ န႔ elements ေတြက ဖနတးဖ႔ အသး ျပၾကတာျဖစပါတယ။ ဒလလပတာဟာ developer အတြက အခနကနသကသာေစပါတယ။ ဆလတာက က ေနာတ႔ရ႕ web pageေတြအားလးမာပါ၀ငေစခငတ႔ header ဒါမမဟတ menu file ေလးတစခက ဖနတးႏငပါတယ။ header က updateလပဖ႔လအပတ႔အခါ ဒ include file တစခထကပ update လပဖ႔လမာျဖစသလ site ထက စာမကႏာအသစတစခထပေပါငးတ႔အခါမးမာလညး web page ေတြအားလးရ႕ link ေတြက update လပရမ႔အစား menufile ေလးကပ ေျပာငးလျပငဆငေပးရပျဖစပါတယ။

Exampleကေနာတ႔မာ header.php ဆတ႔ header file တစခရတယလ႔ ယဆၾကညပါမယ။ ဒ header file က pageတစခမာေပၚဖ႔အတြက include() function ကေအာကပါအတငးအသျပႏငပါတယ။

<html><body><?php include("header.php"); ?><h1> Welcome to my home page</h1><p> Some text</p></body></html>Example 2

PHP Guide Book Moe Myint Shein

33 Youth Dreams 33

Page 34: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

include file ေတြဟာအမားအားျဖင .php န႔ပအဆးသတတာျဖစပါတယ။ အခတစခါ web pageေတြအားလးမာအသးျပမယ႔ menu file ေလးတစခ ရတယလ႔ ယဆပါမယ ေအာကက menu.php ကေလ႔လာၾကညပါမယ။<html><body><a href ="http://www.moemyintshein.com/default.php"> Home </a> |<a href= "http://www.moemyintshein.com/about.php"> About Me</a> |<a href="http://www.moemyintshein.com/contact.php"> Contact Me</a></body></html>default.php , about.php န႔ contact.php ဆတ႔ file သးခစလးမာ menu.php ဆတ႔ file ေလးပါ၀ငသငပါတယ။ ဥပမာ ဒါကေတာ႔ default.php ထက code ပျဖစပါတယ။

<html><body><?php include("menu.php");?><h1> Welcome to my home page</h1><p> Some text</p></body></html>ကေနာတ႔ menu.php က ပrun တ႔အခါ output အေနန႔

Home | About Us | Contact Us

ဆျပးေတာ႔ ရလာမာျဖစပါတယ။ default.php က run တ႔အခါမာေတာ႔

Home | About Us | Contact UsWelcome to my home pageSome text

ယခလပေပၚမာျဖစပါတယ။ default.php န႕ menu.php ကေတာ႔ directory(folder) တစခထမာပရရပါမယ။အကယ၍ က ေနာတ႔က about.php က ေအာကပါအတငးေရးမယဆရင<html><body><?php include("menu.php");?><h1> Welcome to my about me page.</h1><p> Some text</p></body></html>

PHP Guide Book Moe Myint Shein

34 Youth Dreams 34

Page 35: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

ေအာကပါအတငး Output ရမာျဖစပါတယ။

Home | About Us | Contact UsWelcome to my about me page.Some text

သ႔ရ႕ source က ၾကညမယဆရငလညး<html><body><html><body><a href="http://www.w3schools.com/default.php">Home</a> |<a href="http://www.w3schools.com/about.php">About Us</a> |<a href="http://www.w3schools.com/contact.php">Contact Us</a><h1> Welcome to my about mepage.</h1><p> Some text</p></body></html>

အခလပေတြ႕ရမာျဖစပါတယ။ က ေနာတ႔အေနန႔ contact.php မာလညး အခလပ include.php ကျပနေခၚျပး ေရးႏငတာျဖစပါတယ။ include file ကအသးျပျခငးအားျဖင menu link ေတြရအစအစဥက ေျပာငးလခငတ႔အခါ အမညေျပာငးခငတ႔အခါ ဒါမမဟတ site ထက ေနာကထပ web page ထပထညခငတ႔အခါေတြမးမာ page တစခစ fileတစခစမာျပငဖ႔မလဘန႔ menu.php ထမာရတ႔ code ကပ ျပငလကရပျဖစပါတယ။

The require() Functionrequire () function က include() function အလပလပသလပ ပစတအလပလပပါတယ။ errorကကငတြယတ႔ေနရာမာပကြာျခားမႈ ရတာျဖစပါတယ။ include() function ကသးရင warning ေပးရသာေပးျပး scriptကဆက run မာျဖစေပမယ႔ require() function ကသး ထားရငေတာ႔ error message ျပျပးတာန႔ ဆကျပးေတာ႔လညး execute လပမာမဟတေတာ႔ပါဘး။include() function န႔ ဖငတစခကေခၚသးထားျပး အဒဖငကရာမေတြ႔ရငေသာလညးေကာငး error တစခချဖစတယဆရင ေအာကပါအတငး error message တတမာျဖစပါတယ။

PHP CODE

<html><body><?phpinclude("wrongFile.php");

PHP Guide Book Moe Myint Shein

35 Youth Dreams 35

Page 36: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

echo "Hello World!";?></body></html>

ERROR MESSAGE

Warning: include(wrongFile.php) [function.include]: failed to open stream: No such file or directory inC:\xampp\htdocs\php\10.php on line 5

Warning: include() [function.include]: Failed opening 'wrongFile.php' for inclusion(include_path='.;C:\xampp\php\pear\') in C:\xampp\htdocs\php\10.php on line 5

Hello World!

error message ဘယလပျပျပ echo ေၾကာင ရလာတ႔ Hello World ကေတာ႔ execute လပတာပျဖစပါတယ။ ဒါဟာ include ကသးတ႔အတြက warning ေပးရပရျပး execute လပတာကမရပလကတ႔အတြကျဖစပါတယ။ အခဒ ဥပမာကပ require() function န႔ေျပာငးျပး ေရးၾကညပါမယ။PHP CODE

<html><body>

<?phprequire("wrongFile.php");echo "Hello World!";?>

</body></html>

Error Message

Warning: require(wrongFile.php) [function.require]: failed to open stream: No such file or directory inC:\xampp\htdocs\php\10.php on line 5

Fatal error: require() [function.require]: Failed opening required 'wrongFile.php'(include_path='.;C:\xampp\php\pear\') in C:\xampp\htdocs\php\10.php on line 5

PHP Guide Book Moe Myint Shein

36 Youth Dreams 36

Page 37: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

echo statement ကေတာင execute မလပပါဘး require ကသးထားတ႔ျဖစပါတယ ။ error ျပျပးတာန႔ executeလပတာကပါရပလကတာ ျဖစပါတယ။ က ေနာတ႔ တကယ ေရးတ႔အခါမာေတာ႔ include() ကသးမယ႔အစား require()ကသးဖ႔ recommend လပပါတယ။ အကယ၍ file ေတြ missing ျဖစေနတာမး နာမညမားေနတာမးျဖစတ႔အခါမာ scriptက ဆကလကျပး execute မလပေစသငတ႔အတြကျဖစပါတယ။

PHP File Handling

Opening a File

PHP မာ fopen() function ကfile ေတြကဖြငဖ႔အတြကအသးျပၾကပါတယ။ function ရ႕ ပထမ parameterမာဖြငမယ႔ဖငရ႕ အမညပါ၀င မာျဖစျပး.. ဒတယ parameter မာေတာ႔ဘယလပစ မးန႔ဖငကဖြငခငးမယဆတာသတမတေပးရမာျဖစပါတယ။

<html><body><?php$file=fopen("welcome.txt","r");?></body></html>

file က ဘယလပစမးန႔ ဖြငခငးမယဆတ႔ ဒတယ parameter မာေရြးရတ႔ mode ေပါငးမားစြာရပါတယ။ အခ ကေနာသးလကတ႔ r ကေတာ႔ read only ကဆလတာပါ။ေအာကက ဥပမာ မာ အကယ၍ welcome.txt ဆတ႔ file က သ႔ရ႕ directory မာရမေနဘးဆရင fopen() functionအလပမလပပန႔ Unable to Open File ! ဆတ႔ error message က ျပေပးမာျဖစပါတယ။

<html><body><?php$file=fopen("welcome.txt","r") or exit("Unable to open file!");?></body></html>

Closing a File

fcolse() function ကေတာ႔ ဖြငထားတ႔ ဖငက ပတဖ႔အတြကသးတာပေပါ႔..။

<?php

PHP Guide Book Moe Myint Shein

37 Youth Dreams 37

Page 38: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

$file = fopen("welcome2.txt","r");//some code to be executedfclose($file);?>

Checking End-of-Filefeof() function ကသးျပးေတာ႔ ဖငရ႕အဆး (End of File) ေရာကျပလားဆတာက စစႏငပါတယ။ ဒ function က ဘယေလာက data length ရတယဆတာကမသတ႔looping ေတြအတြက အသး၀ငပါတယ။if (feof($file)) echo “End of file”;

Reading a File Line by Linefgets() function ကသးျပးေတာ႔ ဖငတစခ က တစေၾကာငးျခငးက read လပဖ႔သးပါတယ။ ေအာကက ဥပမာမာ တစေၾကာငးျပးတစေၾကာငး ဖငရ႕အဆးကမေရာကမခငးread လပသြားတာျဖစပါတယ။အကယ၍ fopen() function က file ကမဖြငႏငဘးဆရင false(0) က return ျပနမာျဖစပါတယ။

<?php$file = fopen("welcome.txt", "r") or exit("Unable to open file!");//Output a line of the file until the end is reachedwhile(!feof($file)){echo fgets($file). "<br />";}

fclose($file);?>

ဒဟာက run လကတ႔အခါ… welcome.txt ထကစာေတြ တစေၾကာငးျပးတစေၾကာငးးက read သြားျပးေတာ႔ echoန႔ျပထားတာျဖစတ႔အတြက browser မာ welcome.txt ဖငထက စာေတြအကနလးေပၚမာျဖစပါတယ။

Reading a File Character by Character

fgetc() function ကေတာ႔ ဖငတစခက character တစလးခငးစက read လပဖ႔သးပါတယ။ ေအာကက ဥပမာေလးက ဖငရ႕ အဆးက ေရာကသြားတ႔အထ character တစလးခငးစက read လပသြားတာပျဖစပါတယ။

<?php$file=fopen("welcome.txt","r") or exit("Unable to open file!");while (!feof($file)){echo fgetc($file);}

fclose($file);?>

PHP Guide Book Moe Myint Shein

38 Youth Dreams 38

Page 39: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

PHP File Upload

အခ က ေနာတ႔ PHP ကသးျပးေတာ႔ server ေပၚက file ေတြ upload တငတာကၾကညပါမယ။user ေတြအေနန႔ ဖငေတြက upload တငႏငဖ႔အတြက form က သးမယဆရင အဆငေျပပါတယ။ ေအာကက HTML codeက ဖငေတြ upload တငတ႔ေနရာမာသးႏငပါတယ။

<html><body><form action="upload_file.php" method="post"enctype="multipart/form-data"><label for="file">Filename:</label><input type="file" name="file" id="file"/><br/><input type="submit" name="submit" value="Submit" /></form>သ႔က run လကမယဆရင browser မာေအာကကအတငး browse လပဖ႔ရယ submit လပဖ႔ရယ အတြက upload တငတ႔ ပစေလးကရပါျပ။..

ဒ form မာက ေနာတ႔ submit button ကႏပလကမယဆရင upload_file.php ဆတာေလးက run မာျဖစပါတယ။ upload_file.php ဖငေလးကေအာကပါအတငးေရးျပးသမးလကပါမယ။

<?phpif ($_FILES["file"]["error"] > 0){echo "Error: " . $_FILES["file"]["error"] . "<br />";}

else{echo "Upload: " . $_FILES["file"]["name"] . "<br />";echo "Type: " . $_FILES["file"]["type"] . "<br />";echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";

PHP Guide Book Moe Myint Shein

39 Youth Dreams 39

Page 40: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

echo "Stored in: " . $_FILES["file"]["tmp_name"];}

?>

PHP $_FILES array ကသးျပးေတာ႔ remote server ဆက client computer ကေန ဖငေတြတငတာပျဖစပါတယ။ ပထမဆး parameter ကေတာ႔ ေဖာင ရ႕ input name ျဖစပါတယ .. ဒတယ index အေနန႕ကေတာ႔ name, type, size, tmp_name,error စသညျဖငတစခချဖစႏငပါတယ။- $_FILES[“file”][“name]- upload တငလကတ႔ ဖငရ႕အမည၊- $_FILES[“file”][“type”]- upload တငလကတ႔ ဖငရ႕အမးအစား၊- $_FILES[“file”][“size”]- upload တငလကတ႔ ဖငရ႕ size (bytes န႔ျပေပးပါတယ )၊- $_FILES[“file”][“tmp_name”]- server မာသမးမယ႔ ဖငရ႕ ယာယ ေကာပရ႕နာမည၊- $_FILES[“file”][“error”]- ဖင upload လပရကေနျဖစလာႏငတ႔error code၊ စသညျဖငျဖစပါတယ။အခဒပစက ဖငက php န႔အရးရငးဆး upload တငတ႔ပစပျဖစပါတယ။ security အတြက user ေတြက ဖင uploadတငခြငေပးတ႔အခါမာ ကန႔သန႔ခကေတြ ထားရသငပါတယ။

Restrictions on Upload

အခေအာက က script ထမာ ဖင upload လပတ႔ေနရာမာ ကန႔သတခကအခ႕ထညေပးမာျဖစပါတယ။ user အေနန႔ gifဒါမမဟတ jpeg ဖငေတြကပ upload ခြငေပးပါမယ။ ဖငအရြယအစားကလညး 20 kb ထကမၾကးေစရဘးလ႔ ကန႔သတပါမယ။

<?phpif ((($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")|| ($_FILES["file"]["type"] == "image/pjpeg"))&& ($_FILES["file"]["size"] < 20000)){if ($_FILES["file"]["error"] > 0){echo "Error: " . $_FILES["file"]["error"] . "<br />";}

else{echo "Upload: " . $_FILES["file"]["name"] . "<br />";echo "Type: " . $_FILES["file"]["type"] . "<br />";echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";echo "Stored in: " . $_FILES["file"]["tmp_name"];}

}else{

PHP Guide Book Moe Myint Shein

40 Youth Dreams 40

Page 41: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

echo "Invalid file";}

?>

Saving the uploaded Fileအေပၚကဥပမာက server မာရတ႔ PHP temp folder ထမာပ upload တငလကတ႔ ဖငရ႕ ယာယ ေကာပကပ ဖနတးေပးတာျဖစပါတယ။ php script ဆးသြားတာန႔ အဒေကာပကလညး ေပာကသြားမာျဖစပါတယ။ upload လပလကတ႔ ဖငက store လပဖ႔အတြက location တစခက ကးယေပးဖ႔လအပပါတယ။

<?phpif ((($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")|| ($_FILES["file"]["type"] == "image/pjpeg"))&& ($_FILES["file"]["size"] < 20000)){if ($_FILES["file"]["error"] > 0){echo "Return Code: " . $_FILES["file"]["error"] . "<br />";}

else{echo "Upload: " . $_FILES["file"]["name"] . "<br />";echo "Type: " . $_FILES["file"]["type"] . "<br />";echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

if (file_exists("upload/" . $_FILES["file"]["name"])){echo $_FILES["file"]["name"] . " already exists. ";}

else{move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]);echo "Stored in: " . "upload/" . $_FILES["file"]["name"];}

}}

else{

PHP Guide Book Moe Myint Shein

41 Youth Dreams 41

Page 42: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

echo "Invalid file";}

?>

upload_file.php ဖငေလးက အထကပါ code အတငး ေရးျပးသမးလကတ႔အခါ upload.php က run လ႔ရပါျပ။ phpdirectory (C:\xampp\htdocs\php)ေအာကမာ upload ဆတ႔ folder ေလးက ေဆာကေပးလကပါ.. အဒေနာက uploadတငမယဆရငေတာ႔ upload ဆတ႔ folder ေအာကထက ကယတငလကတ႔ file ေလးေရာကေနမာျဖစပါတယ။ အခက ကေနာတ႔က ကယ႔ရ႕ local စကထမာပစမးေနတာျဖစပါတယ။ web hosting ေပၚမာစမးရငလညး သေဘာတရားကေတာ႔ အတတပျဖစပါတယ။

PHP Cookies

cookie က user တစေယာကရ႕ identity အတြက အသးျပပါတယ။ cookie ဆတာ user ရ႕ ကြနပတာေပၚမာ ဆာဗာကေန ခနထားခ႔တ႔ ဖငေလးတစခပျဖစပါတယ။ browser တစခကသးျပး တညတ႔ ကြနပတာတစလးကေန web page တစခက request လပလကတ႔အခါတငးမာ cookie ကပ႔ေပးပါတယ။ PHP ကသးျပးေတာ႔ cookie ဖနတးပ ရယပေတြကေလ႔လာၾကပါမယ။

How to Create a Cookie?

cookie တစခက ဖနတးဖ႔အတြက setcookie() function ကသးပါတယ။ setcookie() function က <html> tagရ႕အေရ႕မာ ေရးရမာျဖစပါတယ။setcookie (name,value,expire,path,domain); ဒါကေတာ႔ cookie က set လပတ႔ syntax ျဖစပါတယ။Exampleေအာကက ဥပမာေလးမာ က ေနာတ႔ user ဆတ႔ cookie ေလးတစခက ဖနတးပါတယ။ Alex Porter ဆတ႔ တနဖးက ေပးပါတယ။ တစနာရၾကာတ႔အခါ expire ျဖစဖ႔က သကမတေပးပါတယ။<?phpsetcookie("user", "Alex Porter", time()+3600);?><html>....cookie ရ႕ expire time က ေနာကထပနညးတစမးန႔အခလသကမတႏငပါတယ။ စကန႔ကသးတာထကပလြယပါတယ။<?php$expire=to,e()+60*60*24*30;setcookie("user", "Alex Porter", $expire);?><html>.....ဒ ဥပမာမာဆရင expire time က တစလအျဖစသတမတထားခ႔တာျဖစပါတယ။ (60 sec * 60 min * 24 hours * 30days).

PHP Guide Book Moe Myint Shein

42 Youth Dreams 42

Page 43: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

Cookie တနဖးတစခက Retrieve လပျခငး<?php//Print a cookieecho $_COOKIE["user"];// A way to view all cookiesprint_r($_COOKIE);?>ဒဥပမာမာ user ဆတ႔ cookie ေလးရ႕တနဖးက ရယျပးေတာ႔ ျပနျပေပးပါတယ။ေအာကက ဥပမာမာေတာ႔ isset() function ကသးျပး cookie တစခဟာ တနဖးသတမတလကျခငးရမရက စစေဆးမာျဖစပါတယ။

<html><body><?phpif (isset($_COOKIE["user"]))echo "Welcome" . $_COOKIE["user"] . "!<br/>";elseecho "Welcome guest!<br />";?></body></html>

Cookie တစခက ဖကပစျခငးcookie တစခကဖကပစတ႔အခါမာ expiration date က အတတမာက နခ႔တာေသခာေစဖ႔သငပါတယ။ဥပမာ

<?php//set the expiration date to one hour agosetcookie("user", "", time()-3600);?>

Browser တစခက Cookie က support မလပတ႔အခါ အျခား နညးလမးေတြကအသးျပျပး စာမကႏာတစခကေနတစခက အခကအလကေတြက ပ႔ေပးဖ႔လပရပါတယ။ နညးလမးတစခကေတာ႔ form ကအသးျပျခငးျဖစပါတယ။ ေအာကကဥပမာ ေလးမာ Submit button က user က ႏပလကတ႔အခါ welcome.php ဆက user input ေတြကလမးပ႔ေပးတာျဖစပါတယ။

<html><body><form action="welcome.php" method="post">Name: <input type="text" name="name"/>

PHP Guide Book Moe Myint Shein

43 Youth Dreams 43

Page 44: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

Age : <input type="text" name ="age"/><input type="submit"/></form></body></html>

အထကပါ code ေတြက form1.php လ႔ save လကတယဆပါစ႔။welcome.php ဖငထမာေအာကပါအတငး တနဖးေတြက retrieve လပယရမာျဖစပါတယ။

<html><body>Welcome <?php echo $_POST["name"]; ?>. <br />You are <?php echo $_POST["age"]; ?> years old.</body></html>အထကပါဥပမာမာ forum1.php က Browser မာ run လကတ႔အခါ အမညန႔ အသကကျဖညဖ႔ text box ႏစခရမာ ျဖစပါတယ။ ျဖညျပးေတာ႔ submit ကႏပလကရင ေအာကပါအတငး ျဖညလကတ႔အေပၚမာမတညျပး ျပနျပေပးပါလမ႔မယ။ကေနာကေတာ႔ အမညမာ moemyintshein အသကမာ 20 ကထညျပး submit လပလကတ႔အခါ…

Welcome moemyintshein.You are 20 years old.

PHP Sessions

PHP session variable တစခက user session တစခအတြက အခကအလကေတြကသမးဆညးဖ႔ ေသာလညးေကာငး ဆကတငေတြက ေျပာငးလဖ႔ရနေသာလညးေကာငး အသးျပရပါတယ။ user တစေယာကရ႕ အခကအလကေတြက ကငတြယျပးေတာ႔ application တစခရ႕ စာမကႏာေတြအားလးမာ ျပနသးႏငပါတယ။ application တစခကသးတ႔အခါမာ ကေနာတ႔က အဒါကဖြငတယ အေျပာငးအလအခ႕ကလပတယ.. ျပးရငပတလကတယ။. ဒါဟာ session ပါပ။ computerက က ေနာတ႔ဘယသဆတာကသတယ။ က ေနာတ႔ ဘယအခနမာ ဒါက စဖြငျပးေတာ႔ ဘယအခနမာ ပတလကတယဆတာက သကသပါတယ။ အငတာနတေပၚမာဆရငေတာ႔ ျပသနာတစခရလာပါတယ။ web server ကေတာ႔ ကေနာတ႔ ဘယသဆတာ ဘာေတြလပတယဆတာက မသႏငပါဘး.. ဘာလ႔လညးဆေတာ႔ HTTP အေနန႔ state က maintain မလပေပးပါဘး။ဒျပသနာက PHP session ကသးျပးေတာ႔ ေျဖရငးပါတယ။ သ႕က သးျပးေတာ႔ ေနာငအခါမာျပနလညအသးျပတ႔အခါ remember ျဖစေနေစဖ႔အတြက user ရ႕႔ အခကအလကေတြက server ေပၚမာသမးဆညးထားႏငပါတယ။ ဒါေပမယ႔ session information ေတြက ယာယပျဖစပါတယ။ user က website ကေနျပနလညထြကခြာသြားတာန႔ ပကသြားမာျဖစပါတယ။ အကယ၍ အျမတမးရေနေစခငတယဆရငေတာ႔ database ထမာ dataေတြကသမးရမာျဖစပါတယ။ session အေနန႔ unique id( UID) တစခက visitor တစဥးစတငးအတြက ဖနတးေပးပါတယ။ ျပးတ႔အခါ ဒ UID ေပၚမာအေျခချပးေတာ႔ variable ေတြကသမးဆညးပါတယ။ UID ကေတာ႔ cookieတစခထမာေသာလညးေကာငး URL ထမာ propagated လပျပးေသာလညးေကာငး သမးဆညးပါတယ။Starting a PHP Session

PHP Guide Book Moe Myint Shein

44 Youth Dreams 44

Page 45: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

PHP session တစခထမာ user information ေတြကမသမးခင အရင session တစခကအစ ျပေပးရပါမယ။session_start() function က <html> tag မလာခငမာ ေရးရပါတယ။

<?php session_start(); ?><html><body></body></html>အခလအတငးေရးေပးလကခ႔မယဆရင server ေပၚမာ user session က register လပလကျပးေတာ႔ user Informationေတြ စတင သမးမာျဖစပါတယ။ ဒါ႔အျပင user session အတြက UID တစခကလညး assign လပေပးပါတယ။

Storing a Session Variablesession variable ေတြက သမးဖ႔ ထတယ ဖ႔အတြက သးရတာကေတာ႔ PHP $_SESSION variable ပျဖစပါတယ။<?phpsession_start();//store session data$_SESSION['views']=1;?><html><body><?php//retrieve session dataecho "Pageviews=" . $_SESSION['views'];?></body></html>

output အေနန႔ Pageviews=1 ဆျပးရမာျဖစပါတယ။

အခထပျပမယ႔ ဥပမာမာေတာ႔ ရးရငးတ႔ page view counter တစခက ဖနတးမာျဖစပါတယ။ isset() functionကသးျပးေတာ႔ views ဆတ႔ variable က သတမတထားမထားစစပါမယ ..။ သတမတထားျပးသားဆရင counter က တစတးပါမယ။ မဖနတးရေသးဘးဆရငေတာ႔ views variable က ဖနတးျပးေတာ႔ တနဖးက တစလ႔ သတမတေပးပါမယ။

<?phpsession_start();

if(isset($_SESSION['views']))$_SESSION['views']=$_SESSION['views']+1;

else$_SESSION['views']=1;

PHP Guide Book Moe Myint Shein

45 Youth Dreams 45

Page 46: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

echo "Views=". $_SESSION['views'];?>

ဒါက run ၾကညမယဆရင…. relode လပလကတ႔အခါတငးမာ views ကတစတးတးသြားတာေတြ႔မာျဖစပါတယ။

Destroying a Sessionsession data ကျပနဖကမယဆရငေတာ႔ unset() function ဒါမမဟတ sessopm_destroy() functionကအသးျပႏငပါတယ။ unset() function ကေတာ႔ ေဖာျပထားတ႔ session variable က free လပပစဖ႔အတြကအသးျပ ပါတယ။<?phpunset($_SESSION['views']);?>session_destroy()function က ေခၚျပးေတာ႔ session ကလး၀ ဖကဆးလ႔လညးရပါတယ

<?phpsession_destroy();?>

PHP Mail FunctionPHP script ေရးျပး အးေမးေတြက တကရကပ႔လ႔ရပါတယ။PHP ရ႕ mail() function ကအသးျပရမာျဖစပါတယ။

Syntaxmail(to,subject,message,headers,parameters)

ေအာကကေတာ႔ သ႔မာထညလ႔ရတ႔ parameter ေတြန႔ သတ႔ရ႕ေဖာျပခကေတြျဖစပါတယ။

to, subject, message သးခကေတာ႔ မျဖစမေနထညေပးရမာျဖစျပး headers န႔ parameters ေတြကေတာ႔ Optionalပျဖစပါတယ။

PHP Simple E-Mail

PHP Guide Book Moe Myint Shein

46 Youth Dreams 46

Page 47: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

PHP န႔အးေမးပ႔မယဆရင အရငးဆးန႔ အလြယဆးကေတာ႔ text စာသားေတြက ပ႔တာပျဖစပါတယ။ေအာကက ဥပမာေလးမာ အရငဆး$to, $subject, $message, $from, $headers စတ႔ variable ေတြကေၾကျငာျပးေတာ႔ mail() function ထမာ အးေမးတစခကပ႔ဖ႔အတြက ေခၚသးတာျဖစပါတယ။

<?php$to= "[email protected]";$subject="Test mail";$message="Hello! This is a simple email message.";$from= "gtalk@moemyintshein";$headers="From: $from";mail($to,$subject,$message,$headers);echo "Mail Sent.";?>

PHP Mail FormPHP ကသးျပးေတာ႔ က ေနာတ႔ရ႕ website ေတြေပၚမာ feedback-form လမးဖနတးႏငပါတယ။ ေအာကကဥပမာမာ formကသးျပး အးေမးပ႔တာကျပထားတာျဖစပါတယ။

<html><body>

<?phpif (isset($_REQUEST['email']))//if "email" is filled out, send email{//send email$email = $_REQUEST['email'] ;$subject = $_REQUEST['subject'] ;$message = $_REQUEST['message'] ;mail( "[email protected]", "Subject: $subject",$message, "From: $email" );echo "Thank you for using our mail form";}

else//if "email" is not filled out, display the form{echo "<form method='post' action='mailform.php'>Email: <input name='email' type='text' /><br />Subject: <input name='subject' type='text' /><br />Message:<br />

PHP Guide Book Moe Myint Shein

47 Youth Dreams 47

Page 48: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

<textarea name='message' rows='15' cols='40'></textarea><br /><input type='submit' /></form>";}

?>

</body></html>

code ေတြက save လပတ႔အခါ mailform.php လ႔ save လပလကပါတယ။ သ႔ရ႕အလပလပပကေတာ႔ ပထမဆး email ရ႕ input field ကျဖညထားမထားစစပါတယ။ မျဖညထားေသးဘးဆရငေတာ႔ ဆလတာက ပထမဆးအၾကမ ဒ စာမကႏာကေရာကလာတာဆရငေတာ႔ HTML form ေလးကျပေပးပါတယ။ အကယ၍ ျဖညျပးသား ျဖစေနျပးဆရငေတာ႔ form ကေန အးေမးက ပ႔ေပးပါတယ။ submit buttion ကႏပလကတာန႔ ဒ page ကပ reload လပပါတယ။ input field မာျဖညထားျပးျဖစတ႔အတြက အးေမးကပ႔ေပးပါတယ။ အးေမးပ႔တာေတြျဖစတ႔အတြက SMTP န႔ smtp_post စတ႔ ဆကတငေတြက php.ini file မာသတမတတာေတြ.. လပရမာျဖစပါတယ။အခေတာ႔ အေျခချဖစလ႔ သေဘာတရားက နားလညရငရပါျပ.. အေသးစတက ကယတင ဆကေလ႔လာသငပါတယ။အထကမာေဖာျပခ႔တာေတြကေတာ႔ အရငးဆး အးေမးပ႔တပစပျဖစပါတယ ဒါေပမယ႔ တကယတမး ေတာ႔ လကေတြ႔ လပရတ႔အခါ အခအတငးေရးလ႔မျဖစပါဘး.. အဒါက လျခမႈမရပါဘး…။ ေရ႕အခနးမာ ကေနာတ႔ ပျပး securityေကာငးေအာင ေရးၾကပါမယ။

PHP Secure E-mails

PHP E-mail Injections

PHP Guide Book Moe Myint Shein

48 Youth Dreams 48

Page 49: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

အထကကအခနးမာက ေနာတ႔ေဖာျပခ႔တ႔ code ရ႕အားနညးခကကေတာ႔ unauthorized user ေတြက input form ရ႕ mailheader ထမာdata ေတြကထညသြငးႏငပါတယ။ ဥပမာ ေအာကမာျပထားသလမး user က form ရ႕ input fileld ထမာ ထညမယဆရင အဒလပစာေတြအကနလးဆက အးေမးေရာကမာျဖစပါတယ။

[email protected]%0ACc:[email protected]%0ABcc:[email protected],[email protected],[email protected],[email protected]%0ABTo:[email protected]

ေအာကပါ code က form ထက email field ကစစေဆးဖ႔အတြက input က validate လပဖ႔ပါေရးထားတာျဖစပါတယ။

<html><body><?phpfunction spamcheck($field){//filter_var() sanitizes the e-mail//address using FILTER_SANITIZE_EMAIL$field=filter_var($field, FILTER_SANITIZE_EMAIL);

//filter_var() validates the e-mail//address using FILTER_VALIDATE_EMAILif(filter_var($field, FILTER_VALIDATE_EMAIL)){return TRUE;}

else{return FALSE;}

}

if (isset($_REQUEST['email'])){//if "email" is filled out, proceed

//check if the email address is invalid$mailcheck = spamcheck($_REQUEST['email']);if ($mailcheck==FALSE){echo "Invalid input";}

PHP Guide Book Moe Myint Shein

49 Youth Dreams 49

Page 50: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

else{//send email$email = $_REQUEST['email'] ;$subject = $_REQUEST['subject'] ;$message = $_REQUEST['message'] ;mail("[email protected]", "Subject: $subject",$message, "From: $email" );echo "Thank you for using our mail form";}

}else{//if "email" is not filled out, display the formecho "<form method='post' action='mailform.php'>Email: <input name='email' type='text' /><br />Subject: <input name='subject' type='text' /><br />Message:<br /><textarea name='message' rows='15' cols='40'></textarea><br /><input type='submit' /></form>";}

?>

</body></html>

ဒ code မာက ေနာတ႔ PHP Filter ကအသးျပထားပါတယ။ input ကမနမမန စစေဆးဖ႔ျဖစပါတယ။- The FILTER_SANITIZE_EMAIL filter က string တစခက illegal email character ေတြအားလးက

ဖယရားေပးပါတယ။- The FILTER_VALIDATE_EMAIL ကေတာ႔ email လပစာရ႕ အရညအခငး… format ကကမကကက

စစေဆးေပးပါတယ။ PHP Filter အခနးမာ filter အေၾကာငးပေရးသြားပါမယ။

PHP Error Handling

PHP ရ႕ default error handling ကေတာ႔ ရးရးေလးပါပ။ ဖငရ႕နာမည လငးနပါတ ပါတ႔ error message န႔ ဘာ errorဆတာ ေျပာျပးေတာ႔ browser ကပ႔ေပးလကတာပါပ။ web application ေတြေရးတ႔အခါ error handling ကအေရးၾကးတ႔ အပငးပါပ။ က ေနာတ႔ေရးလကတ႔ code မာ error checking code မပါဘးဆရင က ေနာတ႔ရ႕ ပရဂရမက ပေရာဖကရငနယ မဆနပါဘး ျပးေတာ႔ security risk ေတြလညးရလာပါမယ။ ဒ အခနးမာေတာ႔ PHP ရ႕ error စစတ႔နညးလညး အခ႕ကေရးပါမယ။ ေအာကပါ နညးလမး ေတြပါ၀ငပါမယ။

- Simple “die()” statements

PHP Guide Book Moe Myint Shein

50 Youth Dreams 50

Page 51: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

- Custom errors and error triggers- Error reporting

Basic Error Handling: Using the die() functionပထမဆးဥပမာအေနန႔ text file တစခက ဖြငတ႔ ရးရငးတ႔ code ေလးပါ..။ အကယ၍ ဖငရရင ဘာ error မေပၚမာမဟတ ပါဘး ဖငမရခ႔ရငေတာ႔ က ေနာ႔ဆမာေတာ႔ အခအတငးေပၚပါတယ။

Warning: fopen(welcome.txt) [function.fopen]: failed to open stream: No such file or directory inC:\xampp\htdocs\php\20.php on line 2

ဒလမး error message မးက user ကမရရေစခငတ႔အတြက .. အရငဆး code ထမာ ဖငရမရ အရငစစခငးမယ ျပးမ access လပခငးပါမယ။

<?phpif(!file_exists("welcome.txt")){die("File not found");}else{

#file=fopen("welcome.txt"."r");}?>

အခလေရးလကတ႔အတြက welcome.txt ဖငသာရမေနဘးဆရင File not found ဆတ႔ error message ပေပၚမာ ျဖစပါတယ။ ပထမ code ထကစာရငေတာ႔ အမားၾကးပ ပေရာဖကရငနယဆနသြားတယေပါ႔..။ error ကျပျပးတာန႔ က နတ႔ code ေတြကဆကမ run ေတာ႔တ႔ ရးရငးတ႔ error handling တစခကသးခ႔တာပါ။ ဒါေပမယ႔ အျမတမးဒပစၾကးပသးလ႔ ေတာ႔မျဖစပါဘး.. တျခား error handling အတြက php function တစခ႕ကဆကေလ႔လာၾကပါမယ။

Creating a Custom Error Handlerဒါဟာလညး ရးရငးပါတယ။ error တစချဖစတ႔အခါမာ function တစခကေခၚဖ႔အတြက လပေပးရတာပါပ။ဒ function အေနန႔ အနညးဆးေတာ႔ parameters ႏစခ( error level န႔ error message) ကကငတြယႏငရပါမယ။ ဒါေပမယ႔ သ႔အေနန႔ parameter ငါးလးအထလကခႏငတာျဖစပါတယ။ (optional: file, line number, error context)။Syntaxerror_function (error_level, error_message, error_file, error_line, error_context)

PHP Guide Book Moe Myint Shein

51 Youth Dreams 51

Page 52: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

Error Report Levelsuser က သတမတတ႔ error handler ေတြကအသးျပႏငတ႔ error အမးအစား ေတြရ႕ error report levels ေတြျဖစပါတယ

နားမလညလ႔ အရမးစတမညစေနပါန႔ဥး..။ ေအးေအးေဆးေဆးေပါ႔ေနာ…။ ဒါေတြရတယလ႔ေတာ႔သသြားတာေပါ႔အခ က ေနာတ႔ error ေတြက ကငတြယဖ႔အတြက function တစခက ဖနတးပါမယ။

function customError($errno, $errstr){echo "<b>Error:</b> [$errno] $errstr<br />";echo "Ending Script";die();}

ဒ code ေလးကေတာ႔ ရးရငးတ႔ error handling function တစခပ ျဖစပါတယ။ အဒါေလး က စ run တ႔အခါ မာ error levelန႔ error message ကရယျပးေတာ႔ output ျပေပးျပးေတာ႔ script ကအဆးသတတာပျဖစပါတယ။ကေနာတ႔ error handling function က ဖနတးျပးျပဆရင ဘယအခနမာ trigger လပမယ (စ run မယ ) ဆတာက ဆးျဖတ ရပါမယ။

Set Error Handler

PHP ရ႕ default error handler ကေတာ႔ built in error handler ပါပ။ အေပၚမာ ေရးခ႔တ႔ function က default errorhandler အေနန႔ script အေတာအတြငးမာ ထားပါမယ။တကယေတာ႔ error တစခ႕အတြကပ ဒ error handler ကသးဖ႔

PHP Guide Book Moe Myint Shein

52 Youth Dreams 52

Page 53: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

သတမတေပးႏငပါတယ။ ဒလနညးန႔ မတညတ႔ error ေတြက မတညတ႔ နညးလမးေတြန႔ ကငတြယ ေျဖရငး ႏငတာ ျဖစပါတယ။ ဒဥပမာမာေတာ႔ error ေတြအားလးအတြက က ေနာတ႔ရ႕ error handler ကသးပါမယ။ set_error_handler(“customError”);error ေတြအားလးကကငတြယဖ႔ က ေနာတ႔ရ႕ function ကသးမာျဖစလ႔ set_error_handler() function အေနန႔ parameter တစလးပလအပပါတယ။ ဒတယ parameter ကေတာ႔ error level က သတမတေပးဖ႔အတြက ထပေပါငးထညႏငပါတယ။ေအာကက ဥပမာေလးမာေတာ႔ မရတ႔ variable က Output ျပေပးဖ႔ ၾကးစားမာျဖစပါတယ။ က ေနာတ႔ရ႕ error handlerကစမးၾကညၾကတာေပါ႔။

<?php//error handler functionfunction customError($errno, $errstr){echo "<b>Error:</b> [$errno] $errstr";}//set error handlerset_error_handler("customError");//trigger errorecho($test);?>

သ႔က run မယဆရင- Error: [8] Undefined variable: test ဆျပးေတာ႔ output ရမာပါ။

Trigger an Error

user က data ေတြက input ေပးႏငတ႔ script တစခမာ illegal input ေတြ႔တ႔အခါမာerrors ေတြက trigger လပဖ႔အသး၀င ပါတယ။ PHP မာေတာ႔ trigger_error() function ကသးျပး လပေဆာငႏငပါတယ။ ေအာကက ဥပမာမာေတာ႔ testvaruable က 1 ထကၾကးမယဆရင error တစခ ျဖစမာျဖစပါတယ။

<?php$test=2;if ($test>1){trigger_error("Value must be 1 or below");}?>သ႔က run တ႔အခါမာ က ေနာကေတာ႔ ေအာကပါအတငး output ရပါတယ။Notice: Value must be 1 or below in C:\xampp\htdocs\php\30.php on line 5

PHP Guide Book Moe Myint Shein

53 Youth Dreams 53

Page 54: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

Chapter 6. PHP & AJAX

PHP and AJAXAJAX INTRODUCTIONAJAX= Asynchronous JavaScript And XMLAJAX က web browser န႔ web server ၾကားထကေန data ေတြက ပ႔ဖ႔န႔ လကခဖ႔အတြက JavaScript ကအသျပပါတယ။AJAX နညးပညာေၾကာင user က အေျပာငးအလတစခ လပလကတ႔အခါမာ web page တစခလးက relodeလပလကတာမးမဟတပန႔ ေနာကကြယမာ web server န႔ data exchange လပတ႔အတြက web page ေတြကပျပးေတာ႔ responsive ျဖစပါတယ။ AJAX ကေအာကပါ standards ေတြေပၚမာအေျခခထားပါတယ။ -JavaScript-XML-HTML-CSSPHP ကေလ႔လာတ႔အခါမာ AJAX ကမသလ႔မျဖစပါဘး… ။ JavaScript, XML, HTML,CSS ဒါေတြမသရင PHPေလ႔လာလညး သပေတာ႔မထးပါဘး.. အဒါအကနသရမာျဖစပါတယ .. အားလးဆကစပျပးအသးခရမာျဖစလ႔ပါ။AJAX အေနန႔ major browser ေတြအားလးက support လပၾကပါတယ။ ဒါေၾကာင AJAX application ေတြက browser န႔ platform ေပၚမာမခမႈမရဘ … Cross Platform, Cross Browser Technology လ႔ဆႏငပါတယ။AJAX Uses XML And HTTP Requestsရးရးသာမနနညးန႔ေရးထားတ႔ web application တစခမာ html form တစခကသးျပး input တစခက web server ဆက submit လပပါမယ။ web server က data က process လပျပးတ႔အခါမာ user ဆက web page အသစတစခက returnျပနလာပါမယ။ user က input ေပးတ႔အခါတငးမာ web page အသစတစခက server က ျပနေပးရတာျဖစတ႔အတြက ဒရးရးနညးန႔ေရးတာက ေႏးပါတယ ျပးေတာ႔ user friendly မျဖစပါဘး။ AJAX နညးပညာကသးရင web application ေတြက web page တစခက relode လပစရာမလပန႔ data ေတြက ပ႔ႏင ထတႏငတာျဖစပါတယ။ ဒကစၥက ေနာကကြယမာ serverဆက HTTP request ေတြေပးပ႔ျခငးန႔အလပလပတာျဖစပါတယ။ server က data ျပနလာတ႔အခါ JavaScript ကသးျပး web page ရ႕အစတအပငးေလးကပ modify လပတာျဖစပါတယ။ ေရ႕လာမယ႔ ဥပမာေတြကၾကညရင နားလညမာပါ။

PHP and AJAX

AJAX မာေတာ႔ server ရယလ႔မရပါဘး.. ။ AJAX ဆတာ က ေနာတ႔ရ႕ browser ေပၚမာပ run တ႔ နညးပညာတစချဖစပါတယ။ browser န႔ web server ရ႕ၾကားမာ HTTP requests (asynchronous data transfer)ကသးပါတယ။ ဆလတာက web page တစခလးက server ဆကပ႔တာမဟတပ အဒ web page ထက အခကအလက အနညးငယအစတအပငးေလးကပ ပ႔တာျဖစပါတယ။ ေျပာရရငေတာ႔ AJAX က web server software ေတြေပၚမာမမခတ႔ web browser technology တစချဖစပါတယ။ ဒ tutorial ထမာေတာ႔ PHP server ေပၚမာ runတ႔ဥပမာေတြကအသားေပးေဖာျပေပးပါမယ။

PHP Guide Book Moe Myint Shein

54 Youth Dreams 54

Page 55: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

AJAX XMLHttpReuest

XMLHttpRequest object ကေတာ႔ AJAX ရ႕အဓကေသာ႔ခကျဖစပါတယ။ July 2000 Internet Explorer ထြကကတညးက သးလ႔ရတာျဖစပါတယ။ ဒါေပမယ႔ 2005 မာ AJAX န႔ Web 2.0 အေၾကာငးေျပာျဖစၾကတ႔အခါမ အျပညအ၀အသးခၾကတာ ျဖစပါတယ။ မတညတ႔ browser ေတြအေနန႔ XMLHttpRequest object ကဖနတးဖ႔အတြက ကြျပားတ႔နညးလမးေတြက သးၾကပါတယ။ Internet Explorer က ActiveXObject ကအသးျပပါတယ။ တျခား browser ေတြကေတာ႔ build inJavaScript object တစချဖစတ႔ XMLHttpRequest ကအသးျပပါတယ။ ေအာကက code ေလးက ဒျပသနာကေျပလည ေစတ႔အရးရငးဆး နညးလမးပါ။var XMLHttp=nullif (window.XMLHttpRequest){XMLHttp=new XMLHttpRequest()}

else if (window.ActiveXObject){XMLHttp=new ActiveXObject("Microsoft.XMLHTTP")}

အရငဆး XMLHttpRequest object ကအသးျပဖ႔အတြက XMLHttp variable တစခက ဖနတးပါတယ။ တနဖးကေတာ႔ nullလ႔ သတမတေပးခ႔ပါတယ။ ျပးတ႔ေနာကမာ window.XMLHttpRequest ဆတ႔ object ေလး available ျဖစမျဖစ စစပါတယ။ Firefox, Opera, Safari ေနာကပငး version ေတြမာဆရင available ျဖစမာျဖစပါတယ။ တကယလ႔ availableျဖစတယဆရင XMLHttp=new XMLHttpRequest() ဆျပးေတာ႔ object အသစတစခဖနတးပါတယ။ availableမျဖစရငေတာ႔ window.ActiveXObject က available ျဖစမျဖစစစပါတယ။ Internet Explorer 5.5 န႔ေနာကပငးေတြ အားလးမာ available ျဖစပါတယ။ available ျဖစတယဆရင XMLHttp=new ActiveXObject() ဆျပးေတာ႔ objectအသစတစခက ဖနတးပါတယ။ ပရဂရမမာေတြအေနန႔ ဒထကပေကာငးတ႔ ပျမနတ႔ XMLHttpRequest object က ပႏစသက ႏငပါတယ။ ေအာကက ဥပမာေလးကေတာ႔ Microsoft.XMLHTTP အစား Xsxml2.XMLHTTP ဆတ႔ Microsoftရ႕ latest version က load လပတာျဖစပါတယ။ အဒါကေတာ႔ IE 6 န႔ေနာကပငးမာ မ available ျဖစတာျဖစပါတယ။

function GetXmlHttpObject(){var xmlHttp=null;

try{// Firefox, Opera 8.0+, SafarixmlHttp=new XMLHttpRequest();}catch (e){// Internet Explorertry{

PHP Guide Book Moe Myint Shein

55 Youth Dreams 55

Page 56: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}catch (e){xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}}return xmlHttp;}ဒဥပမာက ဘာကေျပာတာလဆရင အရငဆးXMLHttpRequest object ကသးဖ႔အတြက XMLHttp ဆတ႔ variable ေလးကဖနတးပါတယ။ အဒေနာက web standarads (Mozila, Opera န႔ Safari)အရ XMLHttp= new XMLHttpRequest() objectကဖနတးဖ႔ ၾကးစားပါတယ။ ေနာကတခါ Microsoft နညးလမးန႔ IE 6 န႔ေနာကပငး အတြက XMLHttp=newActiveXObject(“Msxml2.XMLHTTP object ကဖနတးဖ႔ၾကးစားပါတယ။ အဒမာ error ျဖစတယဆမ အေဟာငးနညးလမး (IE 5.5) XMLHttp=new ActiveXObject(“ Microsoft.XMLHTTP”) ကၾကးစားပါတယ။

AJAX Suggestေအာကက AJAX ဥပမာမာ user က web form တစခထက data ရကထညျပးေတာ႔ web page တစခက web server န႔ ဘယလ communticate လပတယဆတာက သရပျပသြားမာျဖစပါတယ။ ေအာကကလပစမးေလး ေရးမာပါ။ boxေလးထမာ character ေလးတစခခကရကထညတာန႔ suggestion မာေပၚလာမယ။

ဒ ဥပမာမာ page သးခပါ၀ငပါမယ။- ရးရး HTML page- JavaScript page- PHP page

The HTML Formအခကေတာ႔ HTML page ျဖစပါတယ။ သ႔မာ form အတြက HTML code ရယ JavaScript ကေခၚတ႔ link ရယပါ၀ငပါတယ။ဒ html file က ajaxtesting.html လ႔ေပးပါမယ။

<html><head><script src="clienthint.js"></script></head>

<body>

PHP Guide Book Moe Myint Shein

56 Youth Dreams 56

Page 57: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

<form>First Name:<input type="text" id="txt1"onkeyup="showHint(this.value)"></form>

<p>Suggestions: <span id="txtHint"></span></p>

</body></html>ဒဥပမာမဆရင txt1 ဆတ႔ Input filed ေလးပါတ႔ ရးရး HTML form ေလးပါပါတယ။ အလပလပပကေတာ႔

1. user က input field ေလးထမာ keyboard ေပၚက key တစခက ႏပျပးေတာ႔ release လပလကတ႔အခါeventတစခက အစပးေပးပါတယ။

2. evernt ကအစပးျပးတ႔အခနမာ showHint() ဆတ႔ function ေလးကေခၚျပးေတာ႔ execute လပပါတယ။3. form ရ႕ေအာကမာ txtHint ဆတ႔ <span> ေလးရပါတယ။ အဒါကေတာ႔ showHint() ကျပနလာတ႔ return data

ေလး ေပၚမ႔ေနရာေလးပျဖစပါတယ။

ေအာကက JavaScript code ကေတာ႔ clienthint.js လ႔သမးမာျဖစျပး HTML document န႔ လငလပထားတာျဖစပါတယ။

var xmlHttp;

function showHint(str){if (str.length==0){document.getElementById("txtHint").innerHTML="";return;}

xmlHttp=GetXmlHttpObject();if (xmlHttp==null){alert ("Browser does not support HTTP Request");return;}

var url="gethint.php";url=url+"?q="+str;url=url+"&sid="+Math.random();xmlHttp.onreadystatechange=stateChanged;xmlHttp.open("GET",url,true);xmlHttp.send(null);

PHP Guide Book Moe Myint Shein

57 Youth Dreams 57

Page 58: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

}

function stateChanged(){if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){document.getElementById("txtHint").innerHTML=xmlHttp.responseText;}}

function GetXmlHttpObject(){var xmlHttp=null;try{// Firefox, Opera 8.0+, SafarixmlHttp=new XMLHttpRequest();}catch (e){// Internet Explorertry{xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}catch (e){xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}}return xmlHttp;}// JavaScript Document

ဒဥပမာေလးကရငးရရငေတာ႔ showHint()function က inpurt field ထမာ က ေနာတ႔ character တစလးရကလကတ႔ အခါတငးအလပလပပါတယ။ အကယ၍ input filed ထမာ input ရျပးသား ဆလတာက str.length>0 ျဖစသြားတယဆရင ေအာကပါအတငး execute လပပါတယ။

1. server ကေပးပ႔မယ url filename က သတမတပါတယ။ ဒမာေတာ႔ gethint.php ပါ။2. input filed ထမာေရာကေနမယ႔ content (character) န႔ ယဥတြျပးေတာ႔ parameter (q)

ေလးကေပါငးထညပါတယ။3. server က cached file ကသးမတာကေန ကာကြယဖ႔ random number တစခက ထပေပါငးထညပါတယ။

PHP Guide Book Moe Myint Shein

58 Youth Dreams 58

Page 59: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

4. GetXmlHttpObject function ကေခၚျပးေတာ႔ XMLHTTP object ကဖနတးယပါတယ။ ျပးေတာ႔ ေျပာငးလမႈတစခ ျဖစတ႔အခါ အဒ object အေနန႔StateChanged ဆတ႔ function ေလးက execute လပေစပါတယ။

5. ေပးလကတ႔ url အတငး XMLHTTP object က ဖြငလကပါတယ6. server ဆက HTTP request ကလမးပ႔လကပါတယ။

input filed က ဗလာျဖစေနတယဆရင function က txtHint ရ႕ placeholder (suggestion ေပၚတ႔ေနရာ) မာရေနတ႔ content (စာေတြ) အားလးက ရငးလငးပစလကပါတယ။

The stateChanged() FunctionXMLHTTP object ရ႕ state ေျပာငးလတ႔အခါတငးမာ ဒ function က execute(တြကခက ) လပပါတယ။state က 4 ဒါမမဟတ complete အျဖစေျပာငးတ႔အခါ txtHint ရ႕ content က သ response လပတ႔ text န႔ျဖညပါတယ။

The GetXmlHttpObject() FunctionAJAX applications အေနန႔ XML support လပတ႔ browser န႔ပ run မာျဖစတပအတြက GetXMLHttpObjet()ကေခၚျပးေတာဒျပသကနညးလမးတစခ မဟတတစခန႔ေျဖရငးႏငေစဖ႔ XMLHTTP object အမးမးကဖနတးပါတယ။ အခ က နတ႔ PHP page အတြကျဖစပါတယ။JavaScript code ကေခၚတ႔လအပတ႔ server page ကေတာ႔ gethint.phpလ႔ေခၚတ႔ Php file ေလးပါ။ ဒ gethint.php ဖငေလးထက code မာေတာ႔ နာမညေတြရ႕ array တစခက စစျပးေတာ႔ clientဆက သကဆငရာ အမညက return ျပနေပးပါမယ။

<?php// Fill up array with names$a[]="Anna";$a[]="Brittany";$a[]="Cinderella";$a[]="Diana";$a[]="Eva";$a[]="Fiona";$a[]="Gunda";$a[]="Hege";$a[]="Inga";$a[]="Johanna";$a[]="Kitty";$a[]="Linda";$a[]="Nina";$a[]="Ophelia";$a[]="Petunia";$a[]="Amanda";$a[]="Raquel";$a[]="Cindy";$a[]="Doris";$a[]="Eve";

PHP Guide Book Moe Myint Shein

59 Youth Dreams 59

Page 60: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

$a[]="Evita";$a[]="Sunniva";$a[]="Tove";$a[]="Unni";$a[]="Violet";$a[]="Liza";$a[]="Elizabeth";$a[]="Ellen";$a[]="Wenche";$a[]="Vicky";//get the q parameter from URL$q=$_GET["q"];//lookup all hints from array if length of q>0if (strlen($q) > 0){$hint="";for($i=0; $i<count($a); $i++){if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q)))){if ($hint==""){$hint=$a[$i];}

else{$hint=$hint." , ".$a[$i];}

}}

}

//Set output to "no suggestion" if no hint were found//or to the correct valuesif ($hint == ""){$response="no suggestion";}else{$response=$hint;

PHP Guide Book Moe Myint Shein

60 Youth Dreams 60

Page 61: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

}

//output the responseecho $response;?>

JavaScript ဖငကေန (strlen($q)>0) ျဖစတ႔အခါတငး text တစခခက လမးပ႔တ႔အခါ ေအာကပါအတငးျဖစေပၚပါတယ။1. JavaScript ကေနလမးပ႔တ႔ character န႔ match ျဖစတ႔ အမညကရာတယ။2. နာမညတစခထကပျပးအတြမတယဆရင response string မာအမညေတြအားလးပါလာမယ။3. ဘာမ match ျဖစတာမေတြ႔ရငေတာ႔ no suggestion ဆျပး response ျဖစမယ။4. တစခထကပျပးေတြ႔ရင ဒနာမညေတြက response ေပးမယ5. ဒ response ေတြအားလးက txtHint placeholder ကပ႔ေပးမယ။

အခအခနမာ clienthint.js , gethint.php ajaxtesting.html ဖငသးခ ကတညတ႔ folder directory ေအာကမာ saveခ႔တာျဖစရပါမယ .. အဒါဆရငေတာ႔ ajaxtesting.html က browser မာ run တ႔အခါ PHP န႔ AJAX န႔တြသးထားတ႔ ေပာစရာ web application ပစေလးတစခကရပါျပခငဗာ။ :D

PHP AND AJAX XML EXAMPLE

အခတခါကေတာ႔ AJAX က XML file န႔ယဥတြသးႏငပါတယ။ ေအာကက ဥပမာမာ web page တစခက ေန AJAXနညးပညာကသးျပးေတာ႔ XML file တစခစက အခကအလကေတြက ဘယလေခၚယသးႏင (fetch) လပႏငတယဆတာက ေလ႔လာမာျဖစပါတယ။ Select a CD: ဆျပးေတာ႔ စာသားေလးရမယေဘးမာ drop down box ေလးရမယ အဒထမာ ေရြးခယစရာသးခရမယ .. တစခကေရြးလကရင ေအာကမာ အဒါန႔ ပတသကတ႔ CD info ကျပေပးမယ။ ဒါမးေလးပါ။ဒ ဥပမာေလးမာလညး HTML form တစခ ၊ XML file တစခ ၊ JavaScript တစခ ၊ PHP page တစခ စစေပါငးေလးခပါ၀ငပါမယ။THE HTML FORMဒေအာကက ဥပမာေလးမာေတာ႔ HTML form ေလးပါမယ။ JavaScript ဖငဆကလညး လငခတထားတာျဖစပါတယ။ဒ HTML code ေတြကေတာ႔ ajaxxml.html လ႔ save လကပါတယ။

<html><head><script src="selectcd.js"></script></head><body><form>Select a CD:<select name="cds" onchange="showCD(this.value)"><option value="Bob Dylan">Bob Dylan</option><option value="Bee Gees">Bee Gees</option><option value="Cat Stevens">Cat Stevens</option></select></form>

PHP Guide Book Moe Myint Shein

61 Youth Dreams 61

Page 62: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

<p><div id="txtHint"><b>CD info will be listed here.</b></div></p></body></html>

ဒါက ရးရငးတ႔ HTML form ေလးပျဖစပါတယ။ cds ဆတ႔ drop down box ေလးပါမယ။ အဒါရ႕ေအာကမာေတာ႔ txtHintလ႔ေခၚတ႔ div တစခပါပါမယ။ အဒ div က web server ကေနျပနလာမယ႔ အခကအလကေတြက ျပေပးမယ႔ ေနရာ placeholder အေနန႔အသးျပတာျဖစပါတယ။ user က data တစခက ေရြးလကတ႔အခါ showCD ဆတ႔ function က execute(run) လပမာျဖစပါတယ။ onchange event ေပၚမာမတညျပးေတာ႔ function ကစ run မာျဖစပါတယ။ ဆလတာက user က drop down box ထက တနဖးက ေျပာငးလကတ႔အခါတငး ေရြးခယမႈေျပာငးလကတ႔အခါတငးမာေပါ႔ showCDဆတ႔ function ကသြားေခၚမာျဖစပါတယ။XML file အေနန႔ကေတာ႔ ေအာကက code ေလးက cd_catalog.xml လ႔ save ျပး ခနက ajaxxml.html ဆတ႔ fileန႔တညတ႔ folder ေအာကမာထားလကပါတယ။

<CATALOG>

<CD><TITLE>Empire Burlesque</TITLE><ARTIST>Bob Dylan</ARTIST><COUNTRY>USA</COUNTRY><COMPANY>Columbia</COMPANY><PRICE>10.90</PRICE><YEAR>1985</YEAR></CD>−<CD><TITLE>Hide your heart</TITLE><ARTIST>Bonnie Tyler</ARTIST><COUNTRY>UK</COUNTRY><COMPANY>CBS Records</COMPANY><PRICE>9.90</PRICE><YEAR>1988</YEAR></CD>−<CD><TITLE>Greatest Hits</TITLE><ARTIST>Dolly Parton</ARTIST><COUNTRY>USA</COUNTRY><COMPANY>RCA</COMPANY><PRICE>9.90</PRICE><YEAR>1982</YEAR></CD>−<CD><TITLE>Still got the blues</TITLE><ARTIST>Gary Moore</ARTIST><COUNTRY>UK</COUNTRY><COMPANY>Virgin records</COMPANY><PRICE>10.20</PRICE>

PHP Guide Book Moe Myint Shein

62 Youth Dreams 62

Page 63: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

<YEAR>1990</YEAR></CD>−<CD><TITLE>Eros</TITLE><ARTIST>Eros Ramazzotti</ARTIST><COUNTRY>EU</COUNTRY><COMPANY>BMG</COMPANY><PRICE>9.90</PRICE><YEAR>1997</YEAR></CD>−<CD><TITLE>One night only</TITLE><ARTIST>Bee Gees</ARTIST><COUNTRY>UK</COUNTRY><COMPANY>Polydor</COMPANY><PRICE>10.90</PRICE><YEAR>1998</YEAR></CD>−<CD><TITLE>Sylvias Mother</TITLE><ARTIST>Dr.Hook</ARTIST><COUNTRY>UK</COUNTRY><COMPANY>CBS</COMPANY><PRICE>8.10</PRICE><YEAR>1973</YEAR></CD>−<CD><TITLE>Maggie May</TITLE><ARTIST>Rod Stewart</ARTIST><COUNTRY>UK</COUNTRY><COMPANY>Pickwick</COMPANY><PRICE>8.50</PRICE><YEAR>1990</YEAR></CD>−<CD><TITLE>Romanza</TITLE><ARTIST>Andrea Bocelli</ARTIST><COUNTRY>EU</COUNTRY><COMPANY>Polydor</COMPANY><PRICE>10.80</PRICE><YEAR>1996</YEAR></CD>−<CD><TITLE>When a man loves a woman</TITLE><ARTIST>Percy Sledge</ARTIST><COUNTRY>USA</COUNTRY><COMPANY>Atlantic</COMPANY><PRICE>8.70</PRICE><YEAR>1987</YEAR></CD>−<CD><TITLE>Black angel</TITLE><ARTIST>Savage Rose</ARTIST>

PHP Guide Book Moe Myint Shein

63 Youth Dreams 63

Page 64: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

<COUNTRY>EU</COUNTRY><COMPANY>Mega</COMPANY><PRICE>10.90</PRICE><YEAR>1995</YEAR></CD>−<CD><TITLE>1999 Grammy Nominees</TITLE><ARTIST>Many</ARTIST><COUNTRY>USA</COUNTRY><COMPANY>Grammy</COMPANY><PRICE>10.20</PRICE><YEAR>1999</YEAR></CD>−<CD><TITLE>For the good times</TITLE><ARTIST>Kenny Rogers</ARTIST><COUNTRY>UK</COUNTRY><COMPANY>Mucik Master</COMPANY><PRICE>8.70</PRICE><YEAR>1995</YEAR></CD>−<CD><TITLE>Big Willie style</TITLE><ARTIST>Will Smith</ARTIST><COUNTRY>USA</COUNTRY><COMPANY>Columbia</COMPANY><PRICE>9.90</PRICE><YEAR>1997</YEAR></CD>−<CD><TITLE>Tupelo Honey</TITLE><ARTIST>Van Morrison</ARTIST><COUNTRY>UK</COUNTRY><COMPANY>Polydor</COMPANY><PRICE>8.20</PRICE><YEAR>1971</YEAR></CD>−<CD><TITLE>Soulsville</TITLE><ARTIST>Jorn Hoel</ARTIST><COUNTRY>Norway</COUNTRY><COMPANY>WEA</COMPANY><PRICE>7.90</PRICE><YEAR>1996</YEAR></CD>−<CD><TITLE>The very best of</TITLE><ARTIST>Cat Stevens</ARTIST><COUNTRY>UK</COUNTRY><COMPANY>Island</COMPANY><PRICE>8.90</PRICE><YEAR>1990</YEAR></CD>−

PHP Guide Book Moe Myint Shein

64 Youth Dreams 64

Page 65: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

<CD><TITLE>Stop</TITLE><ARTIST>Sam Brown</ARTIST><COUNTRY>UK</COUNTRY><COMPANY>A and M</COMPANY><PRICE>8.90</PRICE><YEAR>1988</YEAR></CD>−<CD><TITLE>Bridge of Spies</TITLE><ARTIST>T'Pau</ARTIST><COUNTRY>UK</COUNTRY><COMPANY>Siren</COMPANY><PRICE>7.90</PRICE><YEAR>1987</YEAR></CD>−<CD><TITLE>Private Dancer</TITLE><ARTIST>Tina Turner</ARTIST><COUNTRY>UK</COUNTRY><COMPANY>Capitol</COMPANY><PRICE>8.90</PRICE><YEAR>1983</YEAR></CD>−<CD><TITLE>Midt om natten</TITLE><ARTIST>Kim Larsen</ARTIST><COUNTRY>EU</COUNTRY><COMPANY>Medley</COMPANY><PRICE>7.80</PRICE><YEAR>1983</YEAR></CD>−<CD><TITLE>Pavarotti Gala Concert</TITLE><ARTIST>Luciano Pavarotti</ARTIST><COUNTRY>UK</COUNTRY><COMPANY>DECCA</COMPANY><PRICE>9.90</PRICE><YEAR>1991</YEAR></CD>−<CD><TITLE>The dock of the bay</TITLE><ARTIST>Otis Redding</ARTIST><COUNTRY>USA</COUNTRY><COMPANY>Atlantic</COMPANY><PRICE>7.90</PRICE><YEAR>1987</YEAR></CD>−<CD><TITLE>Picture book</TITLE><ARTIST>Simply Red</ARTIST><COUNTRY>EU</COUNTRY><COMPANY>Elektra</COMPANY><PRICE>7.20</PRICE>

PHP Guide Book Moe Myint Shein

65 Youth Dreams 65

Page 66: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

<YEAR>1985</YEAR></CD>−<CD><TITLE>Red</TITLE><ARTIST>The Communards</ARTIST><COUNTRY>UK</COUNTRY><COMPANY>London</COMPANY><PRICE>7.80</PRICE><YEAR>1987</YEAR></CD>−<CD><TITLE>Unchain my heart</TITLE><ARTIST>Joe Cocker</ARTIST><COUNTRY>USA</COUNTRY><COMPANY>EMI</COMPANY><PRICE>8.20</PRICE><YEAR>1987</YEAR></CD></CATALOG>

ေအာကက code ကေတာ႔ selectcd.js ဆျပးေတာ႔ သမးမယ႔ JavaScript code ပျဖစပါတယ။

var xmlHttp

function showCD(str){xmlHttp=GetXmlHttpObject()if (xmlHttp==null){alert ("Browser does not support HTTP Request")return}var url="getcd.php"url=url+"?q="+strurl=url+"&sid="+Math.random()xmlHttp.onreadystatechange=stateChangedxmlHttp.open("GET",url,true)xmlHttp.send(null)}

function stateChanged(){if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){document.getElementById("txtHint").innerHTML=xmlHttp.responseText}}function GetXmlHttpObject(){var xmlHttp=null;try{// Firefox, Opera 8.0+, SafarixmlHttp=new XMLHttpRequest();}catch (e){

PHP Guide Book Moe Myint Shein

66 Youth Dreams 66

Page 67: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

// Internet Explorertry{xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}

catch (e){xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}

}return xmlHttp;}

stateChanged() န႔ GetXmlHttpObject ေတြကေတာ႔ ေရ႕ဥပမာတစခကအတငးပါပ။showCD() function ရ႕အလပလပပကေတာ႔ drop down box ထက item တစခကေရႊးလကျပဆတာန႔ အလပလပတာျဖစပါတယ။ သလညးပ အေရ႕မာေလ႔လာခ႔တ႔ showHint() လမးပအလပလပတာျဖစပါတယ။JavaScript ကေနလမးေခၚမယ႔ server page ကေတာ႔ getcd.php ဆတ႔ဖငေလးျဖစပါတယ။ ေအာကပါအတငး ေရးျပး သမးပါမယ။ XML document cd_catalog.xml က load လပဖ႔အတြက XML DOM ကသးျပး PHPန႔ေရးထားတာျဖစပါတယ။ XML file ထကေန query run ျပးေတာ႔ HTML အေနန႔ result က return ျပနလာတာျဖစပါတယ။

<?php$q=$_GET["q"];$xmlDoc = new DOMDocument();$xmlDoc->load("cd_catalog.xml");$x=$xmlDoc->getElementsByTagName('ARTIST');for ($i=0; $i<=$x->length-1; $i++){//Process only element nodesif ($x->item($i)->nodeType==1){if ($x->item($i)->childNodes->item(0)->nodeValue == $q)

{$y=($x->item($i)->parentNode);}

}}$cd=($y->childNodes);for ($i=0;$i<$cd->length;$i++){//Process only element nodesif ($cd->item($i)->nodeType==1){echo($cd->item($i)->nodeName);echo(": ");echo($cd->item($i)->childNodes->item(0)->nodeValue);echo("<br />");}

}?>

PHP Guide Book Moe Myint Shein

67 Youth Dreams 67

Page 68: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

JavaScript ကေန PHP page က query လမးပ႔ျပး ေခၚလကတ႔အခါ ေအာကပါအတငးျဖစပြားပါတယ။1. PHP က cd_catalog.xml file ရ႕ XML DOM object က ဖနတးလကပါတယ။2. artist elements (nodetypes=1) ေတြအကနလးက JavaScript ကေနပ႔လကတာန႔ match ျဖစတ႔ အမညက loop

ပတျပးရာပါတယ။3. မနကနတ႔ artist ပါတ႔ CD ကေတြ႔သြားပါတယ။4. album အခကအလကေတြက Output အေနန႔ရျပးေတာ႔ txtHint placeholder ဆကလမးပ႔လကပါတယ။

ေနာကထပ ဥပမာတစချပးသြားပါျပ။ ဒဟာလညးေပာစရာေကာငးမယလ႔ထငပါတယ။

PHP and AJAX RSS Reader

RSS Reader ကသးျပးေတာ႔ RSS Feeds ေတြကဖတလ႔ရပါတယ။ RSS ဆတာကေတာ႔ သတငးေတြ up to date အခက အလကေတြက ျမနျမနဆနဆန သရေစတ႔ဟာ ျဖစပါတယ။AJAX RSS Readerအခေအာကက ဥပမာမာေတာ႔ webpage က refresh လပစရာမလဘန႔ RSS ရ႕ content ေတြက load လပေပးတ႔ RSSreader တစခက ေရးမာျဖစပါတယ။

ဒဥပမာမာေတာ႔ HTML page တစခ ၊ JavaScript တစခ န႔ PHP တစခပါ၀ငမာျဖစပါတယ။The HTML Formဒေဖာငေလးမာ getrss.js ဆတ႔ JavaScript ကလမးခတထားပါတယ။

<html><head><script type="text/javascript" src="getrss.js"></script></head><body><form>Select an RSS-Feed:<select onchange="showRSS(this.value)"><option value="Google">Google News</option><option value="MSNBC">MSNBC News</option></select></form><p><div id="rssOutput"><b>RSS Feed will be listed here.</b></div></p></body></html>

ကေနာတ႔ေတြ႔တ႔အတငးပ ဒ HTML page မာ drop-down box တစခန႔ HTML form တစခပါ၀ငပါတယ။

PHP Guide Book Moe Myint Shein

68 Youth Dreams 68

Page 69: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

user က drop down box ထကေန option တစခကေရြးလကတ႔အခါ event တစခ စတငျဖစပါတယ။ အဒအခါမာ showRSS() ဆတ႔ function ကလမးေခၚပါတယ။ ေအာကမာပါတ႔ rssOutput ဆတ႔ <div> ကေတာ႔ showRSS() function ကေန return ျပနလာတ႔ data ေတြက ေပၚေပးမယ႔ placeholder အေနန႔သးတာျဖစပါတယ။The JavaScriptေအာကက code ကေတာ႔ getrss.js ဆတ႔ JavaScript code ပျဖစပါတယ။var xmlHttpfunction showRSS(str){xmlHttp=GetXmlHttpObject()if (xmlHttp==null){alert ("Browser does not support HTTP Request")return}

var url="getrss.php"url=url+"?q="+strurl=url+"&sid="+Math.random()xmlHttp.onreadystatechange=stateChangedxmlHttp.open("GET",url,true)xmlHttp.send(null)}

function stateChanged(){if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){document.getElementById("rssOutput").innerHTML=xmlHttp.responseText}

}function GetXmlHttpObject(){var xmlHttp=null;try{// Firefox, Opera 8.0+, SafarixmlHttp=new XMLHttpRequest();}catch (e){// Internet Explorertry{xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}

catch (e){xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}

}return xmlHttp;}

JavaScript code ကေနလမးေခၚလကတ႔ getrss.php ဆတ႔ PHP file ေလးကေတာ႔ ေအာကပါအတငးပါ.. သကေတာ႔ server page ေပါ႔။

PHP Guide Book Moe Myint Shein

69 Youth Dreams 69

Page 70: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

<?php//get the q parameter from URL$q=$_GET["q"];//find out which feed was selectedif($q=="Google"){$xml=("http://news.google.com/news?ned=us&topic=h&output=rss");}elseif($q=="MSNBC"){$xml=("http://rss.msnbc.msn.com/id/3032091/device/rss/rss.xml");}$xmlDoc = new DOMDocument();$xmlDoc->load($xml);//get elements from "<channel>"$channel=$xmlDoc->getElementsByTagName('channel')->item(0);$channel_title = $channel->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;$channel_link = $channel->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue;$channel_desc = $channel->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;//output elements from "<channel>"echo("<p><a href='" . $channel_link. "'>" . $channel_title . "</a>");echo("<br />");echo($channel_desc . "</p>");//get and output "<item>" elements$x=$xmlDoc->getElementsByTagName('item');for ($i=0; $i<=2; $i++){$item_title=$x->item($i)->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;$item_link=$x->item($i)->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue;$item_desc=$x->item($i)->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;echo ("<p><a href='" . $item_link. "'>" . $item_title . "</a>");echo ("<br />");echo ($item_desc . "</p>");}?>

JavaScript ကေန option တစခလမးပ႔လကတ႔အခါ ေအာကပါအတငး အလပလပပါတယ။- ဘယ RSS feed ကေရြးထားတယဆတာ PHP ကစစပါတယ။- XML DOM object တစခက ေရြးခယထားတ႔ RSS feed အတြက ဖနတးလကပါတယ။- RSS channel ထက ေတြ႔ရလာတ႔ element ေတြက output ျပေပးပါတယ။- RSS items ေတြထက ပထမဆးသးခက loop ပတေနျပးေတာ႔ output ျပေပးပါတယ။

အခအခနမာ rssreader.html က run မယဆရင ရပါျပခငဗာ။

PHP Guide Book Moe Myint Shein

70 Youth Dreams 70

Page 71: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

PHP AND AJAX POLLေအာကက Ajax example မာ web page က relode လပစရာမလဘန႔ result က ျပနျပေပးႏငတ႔ Poll တစခက ျပသသြားမာ ျဖစပါတယ။ ဒဥပမာမာလညး စာမကႏာေလးခပါ၀င ပါမယ။

- HTML form တစခ- JavaScript တစခ- PHP page တစခ န႔- Yes, No result ေတြက သမးဖ႔ text file တစခ တ႔ပါ၀ငပါတယ။

The HTML Formအခအရငဆး ကေတာ႔ HTML စာမကႏာျဖစပါတယ။ ရးရး HTML form တစခပျဖစျပးေတာ႔ JavaScript ဖငကလမးျပး linkခတထားပါတယ။

<html><head><script src="poll.js"></script></head><body><div id="poll"><h2>Do you like PHP and AJAX so far?</h2><form>Yes:<input type="radio" name="vote"value="0" onclick="getVote(this.value)"><br />No:<input type="radio" name="vote"value="1" onclick="getVote(this.value)"></form></div></body></html>

ဒါေလးက ajaxpoll.html ဆျပး save ထားလကပါတယ။သ႔ရ႕အလပလပပေလးကေတာ႔ <div> tag ထမာ Yes န႔ No ဆတ႔ radio button ေလး ႏစခပါ၀ငတ႔ form ေလးရပါတယ။user က yes ဒါမ မဟတ no ကေရြးလကတ႔အခါမာ event တစခ အလပလပပါတယ။ အဒ event ျဖစတ႔အခါမာ getVote()ဆတ႔ function ကေခၚျပး execute လပပါတယ။ getVote() function ကေနျပနလာတ႔ data ေတြက form မာ replaceျပနလပပါတယ။

The Text Filetext file ကေတာ႔ poll_result.txt ဆျပးေတာ႔ save ပါမယ။ poll ကရတယ .. data ေတြကသမးဖ႔ပျဖစပါတယ။ေအာကကပစအတငးသမးမာပါ။0||0

ပထမလာတ႔ နပါတက Yes votes ေတြက ကယစားျပျပးေတာ႔ || ရ႕ေနာကက နပါတကေတာ႔ No votes ေတြကကယစား ျပပါတယ။ ဥပမာ.. က ေနာတ႔ Yes ကတခါ vote လကရင အေရ႕က 0 က 1 ျဖစသြားပါမယ။

The JavaScriptJavaScript code ေတြကေတာ႔ poll.js န႔သမးပါမယ။

PHP Guide Book Moe Myint Shein

71 Youth Dreams 71

Page 72: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

var xmlHttp

function getVote(int){xmlHttp=GetXmlHttpObject()if (xmlHttp==null){alert ("Browser does not support HTTP Request")return}var url="poll_vote.php"url=url+"?vote="+inturl=url+"&sid="+Math.random()xmlHttp.onreadystatechange=stateChangedxmlHttp.open("GET",url,true)xmlHttp.send(null)}

function stateChanged(){if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){document.getElementById("poll").innerHTML=xmlHttp.responseText;}}

function GetXmlHttpObject(){var objXMLHttp=nullif (window.XMLHttpRequest){objXMLHttp=new XMLHttpRequest()}else if (window.ActiveXObject){objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")}return objXMLHttp}Yes ဒါမမဟတ No က HTML form ထမာ select လပလကတ႔အခါ getVote() function ကလာအလပလပပါတယ။

အခေနာကဆးကေတာ႔ JavaScript ထကေနလမးေခၚထားတ႔ poll_vote.php ပျဖစပါတယ။

<?php$vote = $_REQUEST['vote'];//get content of textfile$filename = "poll_result.txt";$content = file($filename);//put content in array$array = explode("||", $content[0]);$yes = $array[0];$no = $array[1];if ($vote == 0){

PHP Guide Book Moe Myint Shein

72 Youth Dreams 72

Page 73: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

$yes = $yes + 1;}if ($vote == 1){$no = $no + 1;}//insert votes to txt file$insertvote = $yes."||".$no;$fp = fopen($filename,"w");fputs($fp,$insertvote);fclose($fp);?><h2>Result:</h2><table><tr><td>Yes:</td><td><img src="poll.gif"width='<?php echo(100*round($yes/($no+$yes),2)); ?>'height='20'><?php echo(100*round($yes/($no+$yes),2)); ?>%</td></tr><tr><td>No:</td><td><img src="poll.gif"width='<?php echo(100*round($no/($no+$yes),2)); ?>'height='20'><?php echo(100*round($no/($no+$yes),2)); ?>%</td></tr></table>

Yes (0) , No (1) စတ႔တနဖးေတြက JavaScript ကေနလမးပ႔ေပးပါတယ။ အဒေနာကမာေတာ႔- poll_result.txt file ရ႕ content ကယပါတယ။- အဒ contact ေတြက variable ေျပာငးျပးေတာ႔ variable တစခက JavaScript ကပ႔လာတ႔အတငး တစတးပါတယ။- အဒ တစတးျပးတ႔ result က poll_result.txt ဖငမာေရးလကျပး update လပပါတယ။- poll result ရ႕ graphical representation ကထတျပလကပါတယ။

အခအခနမာ ajaxpoll.html က run လကမယဆရင ေမာလငထားသလပ Do you like PHP & AJAX so far? ဆတ႔ pollေလးက Yes, No button ႏစခန႔ေတြ႔ရမာျဖစပါတယ။ajaxpoll.html ဖငက လသလျပငျပးေတာ႔ ဥပမာ Do you like PHP &AJAX so far ဆတ႔ေနရာမာ နငငါ႔ကခစလား… Yes ေနရာမာ ခစတယ လ႔ ထားလက No ေနရာမာ Yesလ႔ေျပာငးျပးေရးလကေပါ႔ . ျပးရင run ၾကညလကေပါ႔…။ :P

PHP Guide Book Moe Myint Shein

73 Youth Dreams 73

Page 74: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

Chapter 7. PHP MyAdmin Basic

ကေနာတ႔အေနန႔ PHP, MySQL န႔ Apache အားလးပါ၀ငျပးသား Package ျဖစတ႔ XAMPP ကအသးျပေနတာျဖစ ပါတယ.။ အလအေလ ာကအေနန႔ root ဆတ႔ username န႔ password မပါဘ ဖနတးထားေပးမာျဖစပါတယ ..။ အခ Database ေတြန႔ ကေနာတ႔ စတငအလပလပေတာ႔မာျဖစလ႔ အမနတကယ username န႔ password တစခက ဖနတးသငပါတယ..။ MySQLရ႕ User အခြငအေရးေတြက အနညးငယေလာက ျဖစျဖစ နားလညဖ႔လအပပါတယ..။

Creating a New UserDatabase ေတြန႔ အလပလပေတာ႔မယဆရင http://localhost/phpmyadmin/ ဆျပး၀ငလကပါ…. PHP My Admin ဆတ႔ tool ကအသးျပျပး Database ေတြန႔ အလပလပရမာျဖစပါတယ..။ User အသစတစေယာကက SQL Statement ကသးျပး ဖနတးႏငသလ ဒါမမဟတ PHP Admin ထမာအသငပါတ႔ Create New User ကႏပျပးေတာ႔လ လြယကစြာဖနတးႏငပါတယ ..။

http://localhost/phpmyadmin/ လ႔ Web Browser မာရကတ႔အခါ ေအာကကေနရာကေရာကလာမာျဖစပါတယ … အဒမာ Privileges ဆတ႔ေနရာက၀ငလကပါ..။

PHP Guide Book Moe Myint Shein

74 Youth Dreams 74

Page 75: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

Privileges ဆတ႔ေနရာက၀ငလကတ႔အခါ ေအာကပါတငး Create a new User ဆတာကေတြ႔ပါမယ .. အဒ လငေလးက ႏပျပး user အသစကဖနတးႏငပါတယ။

PHP Guide Book Moe Myint Shein

75 Youth Dreams 75

Page 76: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

ကေနာ႔အေနန႔ Username ေနရာမာ moemyintshein host က localhost န႔ password က moemyintsheinလ႔ပေပးလကျပးေတာ႔ Global privileges မာ Check All လပေပးပါတယ .. ျပးတ႔ အခါ Go ကႏပျပးေတာ႔ userအသစကဖနတးလကပါတယ ခငဗာ..။ အဒအခါမာ ေအာကပါအတငး SQL Statement ေတြက အလအေလ ာကဖနတးေပးျပးေတာ႔ execute လပထားကာ.. you have added a new user ဆတ႔ Messageျပမာျဖစပါတယ .. ဒါဆရငေတာ႔ user အသစကဖနတးျပးသြားပါျပ..။

အကယလ႔ ကယ႔အေနန႔ အစကတညးက SQL statement ကပ သးျပးဖနတးမယဆရငလရပါတယ..။ ခနက moemyintshein ဆတ႔ နာမညန႔ localhost ဆတ႔ က ေနာတ႔ Server မာ user အသစဖနတးထားတ႔အတြက အခတစခါေတာ႔ SQL statement ကသးျပး username က moemyintshein1 password က moemyint12345 လ႔ေပးျပး အသစထပဖနတးပါမယခငဗာ…

PHP Guide Book Moe Myint Shein

76 Youth Dreams 76

Page 77: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

အထကပါပအတငး PHP Admin main page ရ႕ ညာဘကေထာငအေပၚနားေလးမာ button ငါးခရတ႔အနက အလယက SQL ဆတာေလးက ႏပလကတ႔အခါ သ႔ေအာကမာ SQL query က execute လပႏငတ႔ pop up page တစခ တတလာပါတယ.. အဒအထမာ က ေနာက GRANT ALL ON *.* TO moemyintshein1@localhost IDENTIFIED BY "moemyint12345";လ႔ရကျပး Go ကႏပလကတ႔အခါ localhost ဆတ႔ Server မာ moemyint12345 ဆတ႔ password န႔ moemyintshein1ဆတ႔ user အသစတစေယာကကလညး ထပဖနတးလကျပျဖစပါတယ။အခအခနမာ privileges ထမာ ၀ငၾကညတ႔အခါ ေအာကပါတငး အစက root ပရရာကေန user အသစႏစေယာကပါ ရေနမာျဖစပါတယ ..။

user အသစဖနတးျပးတ႔ေနာကမာ PHP MyAdmin ထမာ Table ေဆာကရေအာငဗာ။Database အရငေဆာကဖ႔လပါတယ Home ပစေလးက ႏပလကရင Databases ဆတာ သ႔ရ႕ menu ေတြထမာ ရာလ႔ေတြ႔ပါတယ။ အဒမာ Create New Database ဆတ႔ေအာကေလးမာ ေဆာကႏငပါတယ။ Database နာမညက mydomain လ႔ေပးလကပါတယ။ Collation ဆတ႔အထမာေတာ႔ အဆငေျပမာကေပးေပါ႔။ က ေနာက utf8_general_ciန႔ေဆာကလကတယ။ျပးတ႔ေနာကမာေအာကပါပအတငးေပၚလာျပးေတာ႔ က ေနာက mydomain ဆတ႔ အဒ database ထမာ people ဆတ႔ tableက field ေလးခ ေပးျပး Go ကႏပျပး ေဆာကလကပါတယ။

PHP Guide Book Moe Myint Shein

77 Youth Dreams 77

Page 78: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

Go ကႏပျပးေဆာကလကတ႔အခါ mydomain ဆတ႔ database ထမာ people ဆတ႔ table ေရာကေနပါျပ ေအာကပါအတငး ျဖညျပး table က save လပလကပါမယ။

PHP MyAdmin ထမာေတာ႔ အခလသြားျဖညမယ။

PHP Guide Book Moe Myint Shein

78 Youth Dreams 78

Page 79: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

ဒအတငးေတြျဖညျပးေတာ႔ save လကပါတယ။ length value က input character ေတြရ႕ လကအခႏငဆး အရညက သတမတေပးတာျဖစပါတယ။ Type အတြက ထားလ႔ ရတာေတြကလညး အမားၾကးပါ။ က ေနာဒမာေလာကထညထားတာပါ။ efficient ျဖစခငမျဖစမယ။ PHP MyAdmin ရ႕ function တစခ႕က အေျခခေလာက သးတတေအာင ဒဟာက ထညေပးထားတာပါ။ ျပးရင Drop ကသးျပး အခနမေရြး ဒ table ေတြဖကလ႔ရပါတယ။Primary Key အေနန႔ သးမယ႔ id fileld က auto_increment ေပးထားပါတယ။ record ေတြက Input ထပထညတ႔အခါ ကေနာတ႔ ရကရကေနစရာမလေတာ႔ဘးေပါ႔။ Default ကေတာ႔ 0 ေပးလကပါတယ။အတနဖးေတြ အားလးထညျပး save လပလကေတာ႔ ေအာကပါ screen ေပၚလာပါတယ။

PHP Guide Book Moe Myint Shein

79 Youth Dreams 79

Page 80: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

သင table ဖနတးတတပါျပ။ အဒ table ကျဖစေစတ႔ SQL command ေတြကလညး ေတြ႔ရမာပါ။ အခေတာ႔ သင သခငမ သပါလမ႔မယ။ ဒါေပမယ႔ ဘယဟာက ဘာကဆလတယဆတာ PHP Database အခနးက ေလ႔လာလရငေတာ႔ သေဘာေပါကလာမာပါ။

Table ထက Data ေတြထညရေအာငဗာ။Insert ဆတာ အေပၚမာေတြ႔မယထငပါတယ။ ႏပလကၾကရေအာင။ျပးတ႔အခါ အခလပျဖညထညႏငပါတယ။

သ႔ရ႕ အေပၚမာရတ႔ tab ေတြ ကလကသာႏပၾကညပါ။

PHP Guide Book Moe Myint Shein

80 Youth Dreams 80

Page 81: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

Insert ကျပနႏပျပး ေဒတာေတြ field တစခစအတြက ထပထပျဖညသြားႏငပါတယ။ေအာကနားေလးမာ သ႔ setting ေတြခနလ႔ရတာရပါတယ။

တချဖညျပးလ႔ save လပတငး SQL code ေတြပါတ႔ window လညးေပၚလာပါတယ။id column က auto increment ျဖစတ႔အတြက ဘာမမထညေပးပါဘး။ေအာကပါပစအတငးျဖစသြားေအာင ျဖညေပးၾကညပါလား။

အလျဖစျပလားမျဖစေသးဘးလားဆတာ Browse Tab က၀ငျပး ၾကညႏငပါတယခငဗာ။ အဒမာေဘးနားက ခတပန႔ ဟာေလးက တစခခငးဆက edit ေပးလပျပးေတာ႔ အမႈကပးေလးကေတာ႔ Delete ေပးလပတာေလးပါ။အခ ဘယလ back up လပလေလ႔လာရေအာင။Database ထကသြားရေအာင ဒဘက ဘယဘကေပါ႔ အခ database ကေတာ႔ mydomain ေပါ႔။အေပၚက Tab ေတြထက Export ကေရြးမယ။

PHP Guide Book Moe Myint Shein

81 Youth Dreams 81

Page 82: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

အခကေတာ႔ people တစခပ ေဆာကထားေတာ႔ သပရတယ အဒအကြကေလးထမာexport လခငတ႔ backup လပခငတ႔ table ေတြကထပေရြးႏငတယ။ က ေနာကေတာ႔ ျပးတ႔အခါ save as file က checked လပျပးေတာ႔ zippedကထပေရြးျပးေတာ႔ GO လပလကပါတယ။ အဒအခါ ေဒါငးလပဆြရပါျပ mydomain.sql ဆတ႔ ဖငေလးက Zip န႔ရပါတယ ခငဗာ။ အဒါ backup ပါပ။အခေလာကဆ ေလ႔လာသအေနန႔ Database ေဆာကတတျပ။ table ေတြ ေဆာကတတျပ။ fileds ေတြဖနတးတတျပး records ေတြထ အခကအလကထညတတတယ။ record ကျပနၾကညတတတယ။ edit လပတတတယ။ ဖကတတတယ ေနာကဆး back up ပါလပတတပါျပ။ အဒါအေျခခပါပ။ က နတာေတြက တျဖညးျဖညးန႔ ဆကေလ႔လာသြားပါခငဗာ။ ကေနာကေတာ႔ ဒေလာကပ အစျပေပးလကပါတယ။

PHP Guide Book Moe Myint Shein

82 Youth Dreams 82

Page 83: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

Chapter 8.PHP and Database

ကၽႊနေတာတ႔အေနန႔ တကယဆ PHP, My SQL Database န႔ Apache Web Server တ႔က တစခစသးသန႔ သြငးျပးခတဆက အသးျပသငေပမယ႔ ဒစာအပရ႕ ရညရႊယခကက ျမနျမန န႔ လြယလြယ PHP ရ႕အေျခခေတြက နားလညေစခငတာ ျဖစတ႔အတြက XAMPP ကသာ အဆငသင Install လပျပး ေလ႔လာၾကတာျဖစပါတယ ..။ သငခနးစာေတြမာ လြယကေစဖ႔အတြက က ေနာတ႔ရ႕ PHP က ေလ႔လာရာမာ… အသးျပေနတ႔ XAMPP န႔ သ႔ရ႕ တြပါတ႔ PHP Myadmin စတ႔ tool ေတြန႔ ပါတသတျပး အေရ႕ကအခနးမာ အနညးငယ ေလ႔လာခ႔ျပး ျဖစပါတယ။

PHP DatabasePHP MySQL IntroductionWhat is MySQL?MySQL ဆတာကေတာ႔ ေပၚပလာအျဖစဆး open source database system ပျဖစပါတယ။ MySQL ဆတာ database ပါ။MySQL ထမာ ေဒတာေတြက tables လ႔ေခၚတ႔ database objects ေတြထမာသမးဆညးပါတယ။ table ဆတာကေတာ႔ ဆကႏြယေနတ႔ ေဒတာေတြစေပါငးပါ၀ငတ႔ column ေတြ rows ေတြပါ၀ငတ႔အစအေ၀းတစခပါ။ အခအခနမာ Databaseရ႕အေျခခေလးနညးနညးက နားလညေအာငၾကညၾကရေအာငလား..။Database Tablesdatabase တစခမ table တစခဒါမမဟတ အမားၾကးပါ၀ငပါတယ။ table တစခစမာ အမညရပါမယ ဥပမာ (Customers,Orders). Table ေတြမာ rows လ႔ေခၚတ႔ records ေတြပါ၀ငပါတယ။ေအာကကဥပမာကေတာ႔ Persons လ႔ေခၚတ႔ table ရ႕ ဥပမာေလးပါ။

ဒ table မာဆရင လတစေယာကအတြက တစခ စစေပါငး သးခ records (rows) သးခန႔ column(LastName, firstName,Address န႔ City) ဆျပးေတာ႔ ေလးခပါ၀ငပါတယ။

Queriesquery ဆတာကေတာ႔ ေမးခြနး ဒါမမဟတ ေတာငးဆမႈလ႔ေျပာႏငပါတယ။ေအာကက query ေလးကၾကညပါ။SELECT LastName FROM Personsဒ query ေလးက Persons table ထက LastName ဆတ႔ column ထက အခကအလကေတြအားလကေရႊးထတေပးမာ ျဖစပါတယ။ ေအာကကပစအတငးထြကလာပါမယ။

Create a Connection to a MySQL Database

PHP Guide Book Moe Myint Shein

83 Youth Dreams 83

Page 84: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

database တစခထက data ေတြ access မလပႏငခငမာပ database အတြက connection ကဖနတးေပးရပါမယ။ PHPမာေတာ႔ mysql_connect() function ကသးျပးလပႏငပါတယ။Syntaxmysql_connect(servername,username,password);

ေအာကက code ထမာ ဆရင က ေနာတ႔အေနန႔ connection က ($con) ဆတ႔ variable ေလးထမာသမးပါတယ .. scriptထမာ ေနာကပငးမာျပနလညးေခၚအသးျပဖ႔အတြကပါ။ အကယ၍ connection fail ျဖစခ႔ရငေတာ႔ die ကအလပလပပါမယ။

<?php$con=mysql_connect("localhost","user","password");if (!$con){

die('Could not connect:' > mysql_error());}//some code?>PHP Script ဆးသြားတ႔အခါမာ connection အလအေလာက ပတသြားပါတယ။ ၾကပတခငရငေတာ႔ mysql_done()fucntoin ကသးႏငပါတယ။

<?php$con = mysql_connect("localhost","peter","abc123");if (!$con){die('Could not connect: ' . mysql_error());}

// some codemysql_close($con);?>PHP MySQL Create Database and TablesCreate a DatabaseMySQL မာ database တစခက ဖနတးဖ႔အတြက CREATE DATABASE statement ကအသးျပပါတယ။SyntaxCREATE DATABASE database_nameအခက PHP tutorial ျဖစတ႔အတြက လအပသေလာက database အပငးကပေရးမာျဖစပါတယ ..။ တတႏငသေလာကေတာ႔ SQL ကလညး ထညေရးေပးထားပါတယ။ သးျခား SQL ကထပေလ႔လာႏငရငလညးေကာငးပါတယ။

PHP Guide Book Moe Myint Shein

84 Youth Dreams 84

Page 85: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

အထကပါ စာေၾကာငးက execute လပဖ႔အတြကmysql_query() function ကအသးျပရပါမယ။ ဒ function ကေတာ႔ MySQL connection ဆက query ဒါမမဟတ command တစခပ႔ဖ႔အတြက အသးျပရတာပါ။ေအာကပါဥပမာကေတာ႔ my_db လ႔ေခၚတ႔ database တစခက ဖနတးတာပါ။

<?php$con = mysql_connect("localhost","user","moemyintshein");if (!$con){die('Could not connect: ' . mysql_error());}

if (mysql_query("CREATE DATABASE my_db",$con)){echo "Database created";}

else{echo "Error creating database: " . mysql_error();}

mysql_close($con);?>Table တစခတညေဆာကျခငးCREATE TABLE statement ကသးျပးေတာ႔ MySQL မာtable တစခကတညေဆာကရပါတယ။ သ႔ရ႕ syntax ကေတာ႔CREATE TABLE table_name(colomn_name1 data_type,colomn_name2 data_type,column_name3 data_type,…)အခလပစပျဖစပါတယ။ SQL အေၾကာငး သငခငရင SQL Tutorial ကေလ႔လာပါခငဗာ။PHP ထမာထညသးဖ႔အတြက mysql_query() function ထက CREATE TABLE statement ကေပါငးထညျပး executeလပမာျဖစပါတယ။ဥပမာ။ ။ေအာကပါဥပမာမာ Persons ဆတ႔ table တစခကဖနတးျပးေတာ႔ သ႔မာ FirstName, LastName န႔ Age ဆတ႔ Colomnသးခ ပါ၀ငမာျဖစပါတယ။

<?php$con = mysql_connect("localhost","root","moemyintshein");if (!$con)

PHP Guide Book Moe Myint Shein

85 Youth Dreams 85

Page 86: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

{die('Could not connect: ' . mysql_error());}

// Create databaseif (mysql_query("CREATE DATABASE my_db1",$con)){echo "Database created";}

else{echo "Error creating database: " . mysql_error();}

// Create tablemysql_select_db("my_db1", $con);$sql = "CREATE TABLE Persons(FirstName varchar(15),LastName varchar(15),Age int)";

// Execute querymysql_query($sql,$con);

mysql_close($con);?>မတသားရန- table တစခက မဖနတးခငမာ database ကေရြးေပးရပါတယ။ mysql_select_db() ဆတ႔ functionကသးျပးေတာ႔ database ကေရြးေပးႏငပါတယ။ varchar type ကသးျပးေတာ႔ database field တစခက ဖနတးတ႔အခါမာ အဒ filed ရ႕ maximum length ဥပမာ varchar(15) ဆျပးေတာ႔ သတမတေပးရပါမယ။ data type ကေနျပးေတာ႔ ဒ column ကကငတြယႏငတ႔ data အမးအစားကသတမတေပးတာျဖစပါတယ။

Primary Keys and Auto Increment Fields

table တစခထမာရတ႔ rows ေတြက ခြျခမးစတျဖာႏငဖ႔အတြက primary key တစခက အသးျပရပါတယ။ primary keyတစခစဟာ table ထမာ တစခထညးရရပါမယ။ primary key ဟာ null (ဘာမမရတ႔တနဖး) မျဖစရပါဘး ဘာျဖစလ႔လညးဆေတာ႔ database engine အေနန႔ record ေတြက ရာေဖြဖ႔အတြက တနဖး တစခခေတာ႔ လအပတ႔အတြက ျဖစပါတယ။ ေအာကပါ ဥပမာမာ personID ဆတ႔ field က primary key filed အေနန႔ ထားတာျဖစပါတယ။ primary keyfiled ေတြဟာ အမားအားျဖင ID number ျဖစၾကျပးေတာ႔ AUTO_INCREMENT setting ကအသးျပထားတာေတြ မားပါတယ။ AUTO_INCREMENT ဆတာကေတာ႔ record (column) တစခက အသစေပါငးထညတငးမာ အဒ primary

PHP Guide Book Moe Myint Shein

86 Youth Dreams 86

Page 87: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

key အျဖစသတမတထားတ႔ field ထကတနဖးက အလအေလာက တစေပါငးေပးတာ ကေျပာတာပါ။ primary key filed က null မျဖစတာေသခာေစဖ႔အတြက ဒ field အတြက NOT NULL ဆတ႔ setting က ထညေပးထားၾကရပါတယ။

$sql = "CREATE TABLE Persons(personID int NOT NULL AUTO_INCREMENT,PRIMARY KEY(personID),FirstName varchar(15),LastName varchar(15),Age int)";mysql_query($sql,$con);

PHP MySQL Insert IntoINSERT INTO statement က table တစခထက column(record) အသစေတြ ထပေပါငးထညဖ႔အတြက အသးျပပါတယ။

SyntaxINSERT INTO statement က ပစႏစမးန႔ ေရးႏငပါတယ။ပထမ ပစကေတာ႔ data ေတြကေပါငးထညမယ႔ ေနရာ column နာမညေတြက မေဖာျပဘန႔ သတ႔ရ႕တနဖးေတြကပထညတ႔ ပစပါ။

INSERT INTO table_nameVALUES (value1, value2, value3,...)

ဒတယပစကေတာ႔ column name ေရာ ထညမည တနဖးေရာက ႏစခစလး ေဖာျပသတမတေပးတ႔ ေအာကပါပစပါ။

INSERT INTO table_name (column1, column2, column3,...)VALUES (value1, value2, value3,...)

PHP ကအသးျပျပးေတာ႔ ဒ statement ေတြက execute လပႏငဖ႔အတြက mysql_query() functionကအသးျပၾကရပါမယ။ ဒ function က MySQL connection တစခဆက query တစခ ဒါမမဟတ command တစခ ပ႔ဖ႔အတြက အသးျပၾကရပါတယ။

ဥပမာအေရ႕မာ က ေနာတ႔ Persons လ႔နာမညေပးထားတ႔ table တစခက Firstname, Lastname န႔ Age ဆတ႔ Column သးခန႔ ဖနတးခ႔ၾကပါတယ။ ဒဥပမာမာ အဒ table ကပ ျပနအသးျပပါမယ။ ဒမာ ဒ Persons ဆတ႔ table ထကပ recordအသစႏစခက ေအာကပါအတငး ေပါငးထညလကပါမယ။

<?php$con = mysql_connect("localhost","peter","abc123");if (!$con)

PHP Guide Book Moe Myint Shein

87 Youth Dreams 87

Page 88: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

{die('Could not connect: ' . mysql_error());}

mysql_select_db("my_db", $con);mysql_query("INSERT INTO Persons (FirstName, LastName, Age)VALUES ('Peter', 'Griffin', '35')");mysql_query("INSERT INTO Persons (FirstName, LastName, Age)VALUES ('Glenn', 'Quagmire', '33')");mysql_close($con);?>

Insert Data From a Form Into a Database

အခက ေနာတ႔ Persons table ထက record အသစေတြေပါငးထညေပးႏငမယ႔ HTML form တစခက ဖနတးပါမယ။ေအာကကေတာ႔ HTML form ပါ။

<html><body><form action="insert.php" method="post">Firstname: <input type="text" name="firstname" />Lastname: <input type="text" name="lastname" />Age: <input type="text" name="age" /><input type="submit" /></form></body></html>

user က ဒဥပမာထက HTML form ထမာပါတ႔ Submit button က ႏပလကတ႔အခါ form ထမာထညထားတ႔ data ေတြက insert.php ဆက ပ႔ေပးမာပါ။

insert.php ဖငကေန database တစခဆက ဆကသြယပါမယ။ ျပးေတာ႔ PHP $_POST variables ေတြကသးျပး formထက တနဖးေတြက ထတယ မာျဖစပါတယ။ အဒေနာကမာေတာ႔ mysql_query() function ကေနျပးေတာ႔ INSERT INTOstatement ကအလပလပသြားမာျဖစျပး Persons table ထက record အသစတစခ ထပေပါငးထညေပးမာျဖစပါတယ။

insert.php အတြက code ပါ။

<?php$con = mysql_connect("localhost","peter","abc123");if (!$con){die('Could not connect: ' . mysql_error());}

mysql_select_db("my_db", $con);$sql="INSERT INTO Persons (FirstName, LastName, Age)VALUES('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";if (!mysql_query($sql,$con)){die('Error: ' . mysql_error());}

echo "1 record added";mysql_close($con)?>

PHP Guide Book Moe Myint Shein

88 Youth Dreams 88

Page 89: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

PHP MySQL Select

Select Data From a Database Table

SELECT statement က database တစခထကေန data က select လပဖ႔အတြက အသးျပပါတယ။

Syntax

SELECT column_name(s)FROM table_name

ထးစအတငး PHP န႔အလပလပဖ႔အတြက mysql_query() function ကက ေနာတ႔ သးၾကရပါမယ။

ဥပမာေအာကပါ ဥပမာမာ Persons table ထမာသမးထားတ႔ data ေတြအားလးက ထတႏႈတျပမာျဖစပါတယ။ * ဆတ႔ characterေလးက table ထမာရတ႔ data ေတြအားလးက ေရြးခယထတေပးပါတယ။

<?php$con = mysql_connect("localhost","peter","abc123");if (!$con){die('Could not connect: ' . mysql_error());}

mysql_select_db("my_db", $con);$result = mysql_query("SELECT * FROM Persons");while($row = mysql_fetch_array($result)){echo $row['FirstName'] . " " . $row['LastName'];echo "<br />";}

mysql_close($con);?>

အထကပါ ဥပမာမာ mysql_query() ကေန return ျပနလာတ႔ data ေတြက $result ဆတ႔ variable ထမာ သမးဆညးထားပါတယ။

အဒေနာကမာ array အေနန႔ recrodset ထက ပထမဆး row က return ျပနဖ႔အတြက mysql_fetch_array () functionက အသးျပထားပါတယ။ mysql_fetch_array() က ေခၚသးတငးမာ recordset ထမာ ရတ႔ ေနာကထပ ေနာကထပ rowေတြက return ျပနေပးပါတယ။ while loop ကေန recordset ထက record ေတြအားလးကျဖတသြားျပး Loopingပတပါတယ။ row တစခစရ႕ တနဖးေတြက Screen မာ Print ထတျပေပးဖ႔အတြက PHP $row variable($row['FirstName'] and $row['LastName']) ကက ေနာတ႔ အသးျပထားပါတယ။

ဒ code ရ႕ Output အေနန႔ ေအာကပါအတငး ထြကလာမာျဖစပါတယ။

PHP Guide Book Moe Myint Shein

89 Youth Dreams 89

Page 90: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

Peter GriffinGlenn Quagmire

Display the Result in an HTML Table

ေအာကပါ ဥပမာအေနန႔ကေတာ႔ အထကပါဥပမာအတငးပ တညတ႔ data ေတြကထတႏႈတမာ ျဖစပါတယ။ ဒါေပမယ႔ သကေတာ႔ HTML tab;e တစခန႔ data ေတြကထတျပေပးသြားမာျဖစပါတယ။

<?php$con = mysql_connect("localhost","peter","abc123");if (!$con){die('Could not connect: ' . mysql_error());}

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT * FROM Persons");

echo "<table border='1'><tr><th>Firstname</th><th>Lastname</th></tr>";while($row = mysql_fetch_array($result)){echo "<tr>";echo "<td>" . $row['FirstName'] . "</td>";echo "<td>" . $row['LastName'] . "</td>";echo "</tr>";}

echo "</table>";mysql_close($con);?>

အထကပါ code ေတြရ႕ output ကေတာ႔ ေအာကပါအတငးျဖစပါလမ႔မယ။

PHP MySQL The Where Clause

WHERE clause ကေတာ႔ သတမတထားတ႔ အေျခအေနတစခက ျပညစေစတ႔ record ေတြကပ ထတျပဖ႔အတြက အသးျပ ၾကရပါတယ။

PHP Guide Book Moe Myint Shein

90 Youth Dreams 90

Page 91: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

SyntaxSELECT column_name(s)FROM table_nameWHERE column_name operator value

ဥပမာ

ေအာကပါဥပမာမာ Persons table ထက FirstName က Peter ျဖစေနတ႔ row ေတြအားလးက ထတႏႈတျပေပးပါ လ႔ ေဖာျပ ထားပါတယ။

<?php$con = mysql_connect("localhost","peter","abc123");if (!$con){die('Could not connect: ' . mysql_error());}

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT * FROM PersonsWHERE FirstName='Peter'");

while($row = mysql_fetch_array($result)){echo $row['FirstName'] . " " . $row['LastName'];echo "<br />";}

?>

output အေနန႔ကေတာ႔ -

Peter Griffinဆျပးေတာ႔ ရပါမယခငဗာ။

PHP MySQL Order By Keyword

ORDER BY keyword ကေတာ႔ recordset တစခထက data ေတြက စထားဖ႔အတြက အသးျပပါတယ။ORDER BY keyword ေလးက default အေနန႔ ascending order န႔ record ေတြက စေပးပါတယ။descending order န႔ record ေတြက စမယဆရင DESC keyword ကထညသြငး အသးျပႏငပါတယ။

Syntax

SELECT column_name(s)FROM table_nameORDER BY column_name(s) ASC|DESC

ဥပမာ

PHP Guide Book Moe Myint Shein

91 Youth Dreams 91

Page 92: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

ေအာကပါဥပမာမာ Persons table ထမာ သမးထားတ႔ data ေတြအားလးက ထတျပေပးပါတယ။ ျပတ႔ေနရာမာ Agecolumn န႔ result ေတြက စထားေပးတာျဖစပါတယ။

<?php$con = mysql_connect("localhost","peter","abc123");if (!$con){die('Could not connect: ' . mysql_error());}

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT * FROM Persons ORDER BY age");

while($row = mysql_fetch_array($result)){echo $row['FirstName'];echo " " . $row['LastName'];echo " " . $row['Age'];echo "<br />";}

mysql_close($con);?>

ဒ code ရ႕ output ကေတာ႔

Glenn Quagmire 33Peter Griffin 35ျဖစပါတယ။

Order by Two Columns

column တစခမက သးျပးေတာ႔လညး order by လပႏငပါတယ။ တစခထကပတ႔ column ေတြန႔ Orderingလပမယဆရငေတာ႔ ပထမ column ကတနဖးေတြ တညေနမသာ ဒတယ column ကအသးျပပါတယ။

SELECT column_name(s)FROM table_nameORDER BY column1, column2

PHP MySQL Update

UPDATE statement ကေတာ႔ table တစခထက ရေနျပးသာ record ေတြက ျပငဆငရန Update လပရနအသးျပပါတယ။

SyntaxUPDATE table_nameSET column1=value, column2=value2,...WHERE some_column=some_value

PHP Guide Book Moe Myint Shein

92 Youth Dreams 92

Page 93: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

ဒမာအေရးၾကးတာက WHERE clause ပျဖစပါတယ။ UPDATE syntax ကအသးျပတ႔ခါ WHERE clause အေနန႔ ဘယ record ေတြက ျပငဆငမာလညးဆတာက သတမတေပးရတာျဖစပါတယ။ WHERE မပါဘ သးမယဆ records ေတြ အားလး Update ေတြျဖစကနမာျဖစပါတယ။

ဥပမာကေနာတ႔ ဖနတးခ႔တ႔ Persons ဆတ႔ Table မာ ေအာကပါအတငးရေနတာပါ..။

ေအာကပါ ဥပမာေလးက Persons table ထမာ ရတ႔ data တစခ႕က update လပေပးပါတယ။

<?php$con = mysql_connect("localhost","peter","abc123");if (!$con){die('Could not connect: ' . mysql_error());}

mysql_select_db("my_db", $con);

mysql_query("UPDATE Persons SET Age = '36'WHERE FirstName = 'Peter' AND LastName = 'Griffin'");mysql_close($con);?>

Update လပျပးတ႔အခါမာ Persons table ထမာ ေအာကပါပစအတငးျဖစသြားပါတယ။

PHP MySQL Delete

DELETE statement က database table ထက records ေတြက ျဖတဖ႔အတြက အသးျပပါတယ။ FROM ဆျပးေတာ႔ DELETE ရ႕ေနာကမာပါပါတယ။

SyntaxDELETE FROM table_nameWHERE some_column = some_value

သ႔မာလညးအေရးၾကးတာက WHERE clause ပျဖစပါတယ။ WHERE clause အေနန႔ ဘယ record ေတြက ျဖတမယ မာလညးဆတာက သတမတေပးရတာျဖစပါတယ။ WHERE မပါဘ သးမယဆ records ေတြ အားလး Deleteေတြျဖစကနမာျဖစပါတယ။

PHP Guide Book Moe Myint Shein

93 Youth Dreams 93

Page 94: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

ဥပမာ

အစတနးက Persons table က ဒလရပါမယ။

ေအာကက code ေတြကသးျပးေတာ႔ LasteName က Griffin ျဖစတ႔ Persons table ထက record ေတြက ဖကပါ လ႔ ေျပာလကပါမယ။

<?php$con = mysql_connect("localhost","peter","abc123");if (!$con){die('Could not connect: ' . mysql_error());}

mysql_select_db("my_db", $con);

mysql_query("DELETE FROM Persons WHERE LastName='Griffin'");mysql_close($con);?>

Delete လပျပးတ႔အခါမာေတာ႔ ေအာကပါအတငးျဖစသြားပါမယ။

PHP Database ODBC

ODBC ဆတာကေတာ႔ Microsoft Access Database လမး Data Source တစခက ဆကသြယေပးမယ႔ ApplicationProgramming Interface (API) ျဖစပါတယ။

Create an ODBC Connection

ODBC connection တစခန႔ က ေနာတ႔အေနန႔ ODBC connection တစခ ရေနသေရြ႕ က ေနာတ႔ နတ၀က ထမာရတ႔ ဘယကြနပတာေပၚမာပျဖစျဖစရတ႔ ဘယ database န႔မဆ ခတဆတအသးျပႏငပါတယ။

ေအာကပါေဖာျပခကေတာ႔ MS Access Database တစခန႔ ODBC Connection တစခ ဘယလ ဖနတးတယဆတာပါ။

1. Open the Administrative Tools icon in your Control Panel.2. Double-click on the Data Sources (ODBC) icon inside.3. Choose the System DSN tab.

PHP Guide Book Moe Myint Shein

94 Youth Dreams 94

Page 95: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

4. Click on Add in the System DSN tab.5. Select the Microsoft Access Driver. Click Finish.6. In the next screen, click Select to locate the database.7. Give the database a Data Source Name (DSN).8. Click OK.

ဒ configuration က website ရေနတ႔ ကြနပတာေပၚမာ ျပလပရမာျဖစပါတယ။ က ေနာတ႔ရ႕ ကြနပတာမာ InternetInformation Server ဆတ႔ IIS က run ထားမယဆရင ဒ instruction ေတြက အလပလပမာျဖစပါတယ။ remote serverေပၚမာ က ေနာတ႔ရ႕ website ရတာဆရငေတာ႔ အဒ server က Physical access ရရပါမယ ဒါမမဟတ web host က ကေနာတ႔အသးျပရမယ႔ DNS တစခက set up လပေပးဖ႔ ေတာငးခရပါမယ။

Connecting to an ODBC

odbc_connect() function က ODBC data source တစခန႔ ခတဆကဖ႔အတြကသးပါတယ။ ဒ function မာ parameterေလးခရပါတယ။ data source နာမညရယ၊ username ရယ Password ရယန႔ Optical cursor type ျဖစပါတယ။SQL statement ေတြက execute လပဖ႔အတြက odbc_exec() function က အသးျပပါတယ။

ဥပမာ

ေအာကကဥပမာမာ northwind လ႔ေခၚတ႔ DNS တစခက connection ဖနတးပါတယ username န႔ password န႔ northwind မာမရပါဘး။ အဒေနာက SQL တစခကဖနတးျပးေတာ႔ execute လပပါတယ။

$conn=odbc_connect('northwind','','');$sql="SELECT * FROM customers";$rs=odbc_exec($conn,$sql);

Retrieving Records

odbc_fetch_row() ကသးျပးေတာ႔ result-set ကေန record ေတြက return ျပနပါတယ။ ဒ function က rows ေတြက return ျပနႏငတယဆရင true လ႔ေျပာျပးေတာ႔ ဒါမမဟတရငေတာ႔ false ေပးပါတယ။ဒ function ကေတာ႔ ODBC result identifier န႔ Optical row number ဆတ႔ parameter ႏစခက ယပါတယ။

odbc_fetch_row($rs)

Retrieving Fields from a Record

odbc_result() function က record တစခကေန fields ေတြကဖတဖ႔အသးျပပါတယ။ ဒ function က ODBC resultidentifier န႔ field number ဒါမမဟတ name တစခ စစေပါငး parameter ႏစခက ယပါတယ .ေအာကက code line မာေတာ႔ record ထက ပထမဆး field ရ႕ တနဖးက return ျပနေပးပါတယ။

$compname=odbc_result($rs,1);

PHP Guide Book Moe Myint Shein

95 Youth Dreams 95

Page 96: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

ေအာကက code line ကေတာ႔ CompanyName လ႔ေခၚတ႔ field တစခရ႕ တနဖးက return ျပနေပးပါတယ။

$compname=odbc_result($rs,"CompanyName");

Closing an ODBC Connection

odbc_close() function ကသးျပး ODBC connection တစခကပတရပါမယ။

odbc_close($conn);

ODBC ဥပမာ

ေအာကပါဥပမာမာ database connection တစခက အရငဆးဘယလဖနတးတယ ျပးေတာ႔ result set တစခန႔ HTMLtable တစခထမာ data ေတြကျပေပးဖ႔အတြက ေရးတာက ျပထားပါတယ။

<html><body><?php$conn=odbc_connect('northwind','','');if (!$conn){exit("Connection Failed: " . $conn);}

$sql="SELECT * FROM customers";$rs=odbc_exec($conn,$sql);if (!$rs){exit("Error in SQL");}

echo "<table><tr>";echo "<th>Companyname</th>";echo "<th>Contactname</th></tr>";while (odbc_fetch_row($rs)){$compname=odbc_result($rs,"CompanyName");$conname=odbc_result($rs,"ContactName");echo "<tr><td>$compname</td>";echo "<td>$conname</td></tr>";

}odbc_close($conn);echo "</table>";?></body></html

PHP Guide Book Moe Myint Shein

96 Youth Dreams 96

Page 97: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

Chapter 9. XML & PHP

ကေနာတ႔အတြက Web Development န႔ပတသကရင XML ဆတာ မသမျဖစပျဖစပါတယ။ XML ဆတာ ExtensibleMarkup Language ျဖစပါတယ။ XML က data သယေဆာငဖ႔န႔ သမးဆညးဖ႔ ဖနတးထားတာျဖစပါတယ။ ဒ ကတရရယမာ XML အေျခခက ေဖာျပေပးမာျဖစပါတယ။ HTML န႔ XML န႔ဘာေတြကြာျခားသလဆတာမးေတြလညးရငးသြားပါမယ။ XMLကသဖ႔အေရးၾကးပါတယ၊ သငယဖ႔လညးလြယကပါတယ။

Introduction to XML

XML က data သယေဆာငဖ႔န႔ သမးဆညးဖ႔ တထြငထားတာျဖစပါတယ။

HTML ကေတာ႔ Data ေတြက ထတျပဖ႔အတြကျဖစပါတယ။ XML က HTML လမးပအေတာေလးတတ႔ markup languageပါပ။ XML tags ေတြက ၾကတငသတမတထားတ႔ predefined ေတြမဟတပါဘး.. က ေနာတ႔ ကယ႔ဘာကယ သတမတေပးရ ပါမယ။ XML က self-descriptive ပစျဖစပါတယ။ XML က W3C ကေနလညး ေထာကခမႈေပးထားပါတယ။

XML န႔ HTML တ႔ရ႕ျခားနားခကေတြကသရပါမယ။ XML က HTML ေနရာကအစား၀ငတာမဟတပါဘး။ သတ႔ႏစခက မတညတ႔ ရညရြယခကေတြန႔ ေပၚလာၾကတာပါ။ XML က data ေတြက သယေဆာငဖ႔ န႔ သမးဆညးဖ႔ ျဖစပါတယ .. ဘာ ေဒတာ ေတြလညး ဆတ႔အေပၚမာ ဥးတညပါတယ။ HTML ကေတာ႔ ဒေဒတာေတြက ဘယလပစရလဆတ႔ အေပၚမာ မတညျပးေတာ႔ data ေတြက ျပသဖ႔အတြကပါ။ HTML ကအခကအလကေတြက ထတျပဖ႔ျဖစျပး XML ကေတာ႔ အခကအလကေတြ သယေဆာငသမးဆညးဖ႔ျဖစတယလ႔ နားလညရပါမယ။

XML Does not DO Anything

XML က ဘာကမမလပေပးႏငပါဘး။ နားလညဖ႔ခတပါမယ။ တကယေတာ႔ XML က တစစတစရာက မျပလပပါဘး။ အခကအလကေတြက ဖြ႔စညးတညေဆာက သမးဆညး ျပး သယေဆာငဖ႔ ပျဖစပါတယ။

ေအာကပါ ဥပမာကေတာ႔ XML န႔သမးဆညးထားတ႔ Jani ကေန Tove ဆကေပးတ႔ note တစချဖစပါတယ။

<note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>

က ဒႏတက ကယနားလညေအာင ကယ႔ဘာက ေရးထားတာျဖစပါတယ။ self descriptive သေဘာတရားပါ။ သ႔မာ ပ႔တ႔သန႔ လကခတ႔သရ႕ အခကအလကေတြပါပါတယ။ သ႔မာ ေခါငးစဥပါသလ စာကယလညးပါ၀ငပါတယ။ မနပါတယ ဒါေပမယ႔ ဒ XML document ေလးက တစစတစရာကအလပမလပဘးေလ.. tags ေတြန႔ ေရးထားတ႔ အခကအလက ရငးရငးေလးမသာပ ျဖစပါတယ။ တစစတစေယာကက Software တစခန႔ ခတဆကျပး ဒအခကအလကေတြက ပ႔ေပးဖ႔ လကခဖ႔ ျပသဖ႔ လပေဆာငရရမာျဖစပါတယ။

XML is Just Plain Text

PHP Guide Book Moe Myint Shein

97 Youth Dreams 97

Page 98: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

XML က ဘာမမထးျခားပါဘး။ စာေတြသကသကခညးပျဖစပါတယ။ plain text ေတြက ကငတြယႏငတ႔ Software ေတြက XML ကလညး ကငတြယႏငပါမယ။ ဒါေပမယ႔ XML အတြကဥးစားေပး Application ေတြကေတာ႔ XML tag ေတြက ေကာငးေကာငး ကငတြယႏငပါတယ။ ဒ tags ေတြရ႕ ဘာကလပေဆာငတယဆတ႔ အဓပါယကေတာ႔ application ရ႕ သေဘာသဘာ၀ေပၚမာ မခပါမယ။

XML ကအသးျပျပးေတာ႔ က ေနာတ႔က ကယၾကကတ႔ ကယပင tag ကဖနတးႏငၾကပါတယ။ အေပၚက ဥပမာက <to> တ႔ <from> တ႔လေပါ႔။ အဒါေတြဟာ ဘယ XML standard မာမ defined လပထားတာမရပါဘး။ ဒ tag ေတြက ဒ XMLdocument ကေရးသားတ႔ စာေရးဆရာ ကတထြငလကတာပါပ။ ဒါဟာ ဘာျဖစလ႔လဆေတာ႔ XML language မာ predefined tags မရလ႔ပါ။ ရငးသြားေအာင HTML န႔ယဥပါမယ။ HTML မာသးတ႔ tag ေတြကေတာ႔ Predefined ပါ။ HTMLdocuments ေတြမာ HTML standard သတမတထားတ႔ tags ေတြ ဥပမာ <p> <h1> အဒါမးေတြကပ အသးျပႏငပါတယ။

XML ဆတာ HTML ရ႕အစားထးမဟတဘ အတတကြပးေပါငးလပေဆာငၾကတာသာျဖစပါတယ။ XML က သတငးအခကအလကေတြက သယေဆာငေပးမယ႔ software န႔ hardware ေတြေပၚမာ မခမႈမရတ႔ အရာတစချဖစပါတယ။

XML ကေရးသားတ႔အခါ သစပငတစပငရ႕ ဖြ႔စညးတညေဆာကမႈအတငး အျမစကေနစျပးေတာ႔ သစရြကေတြအထ ခြျဖာသြားတာ မးျဖစပါတယ။

XML document ဥပမာ

XML ဖငတစခက self-describing ပစန႔ေရးျပးေတာ႔ ရးရငးတ႔ syntax ေတြကပအသးျပၾကပါတယ။

<?xml version="1.0" encoding="ISO-8859-1"?><note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>

ပထမဆးစာေၾကာငးကေတာ႔ ဒါ XML ဖငပါလ႔ေျပာလကတာပါ။ XML version 1.0 ပါလ႔ေဖာျပျပးေတာ႔ အသးျပထားတ႔ encoding ကေတာ႔ (ISO-8859-1 = Latin-1/West European character set) ပါလ႔ ေၾကျငာလကတာပါ။

ေနာကထပတစေၾကာငးကေတာ႔ ဒ document ေလးရ႕ root element ကေဖာျပလကတာပါ။ ဒ document ေလးက noteတစချဖစပါတယလ႔ ေျပာလကတာေပါ႔။

<note>

ေနာကထပျပးလာတ႔ စာေၾကာငးေလး ေၾကာငးကေတာ႔ to,from,heading န႔ body ဆတ႔ root ရ႕ child elements ေလးခက ေဖာျပေပးပါတယ။

<to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body>

ေနာကဆးစာေၾကာငးမာေတာ႔ root element ရ႕အဆးသတပါဆျပး ျပနပတလကတာပါ။

PHP Guide Book Moe Myint Shein

98 Youth Dreams 98

Page 99: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

</note>

ကေနာတ႔အေနန႔ ဒဥပမာကေန ဒ XML document ဟာဆရင Jani ကေန Tove ဆက note တစခပါ၀ငတယလ႔ နားလညႏငပါတယ။

XML documents ေတြမာ root element တစခပါ၀ငကပါ၀ငရပါမယ။ ဒ element က တျခား element ေတြအားလး ရ႕ မဘ parent ျဖစပါတယ။ XML တစခမာ ပါတ႔ elements ေတြက document tree တစခလပစေဆာငပါတယ။ သစပငက rootကေန စျပးေတာ႔ branches ေတြထ ခြျဖာသြားသလေပါ႔။ elements ေတြအားလးမာ ထပခြတ႔ elements (child elements)ေတြပါ၀ငႏငပါတယ။

<root><child>

<subchild>.....</subchild></child>

</root>

parent, child, sibling စတ႔ ေ၀ါဟာရေတြက elements ေတြၾကားထက ဆကဆမႈအျဖစ ေဖာျပဖ႔ သးပါတယ။ မဘေတြမာ ကေလးေတြရပါမယ ၊ level တကေလးေတြဆရင ေမာငႏမ ညအစမ ညအစက ေတြေပါ႔။ elements ေတြအားလးမာ HTMLမာလပ text content န႔ attributes ေတြပါ၀ငႏငပါတယ။

ဥပမာ

အထကပါပက စာအပတစအပက XML န႔ေအာကပါအတငးေရးတာက ကယစားျပပါတယ။

<bookstore><book category="COOKING"><title lang="en">Everyday Italian</title><author>Giada De Laurentiis</author><year>2005</year><price>30.00</price>

PHP Guide Book Moe Myint Shein

99 Youth Dreams 99

Page 100: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

</book><book category="CHILDREN"><title lang="en">Harry Potter</title><author>J K. Rowling</author><year>2005</year><price>29.99</price>

</book><book category="WEB"><title lang="en">Learning XML</title><author>Erik T. Ray</author><year>2003</year><price>39.95</price>

</book></bookstore>

ဒဥပမာထမာ root element က <bookstore> ျဖစပါတယ။ အားလးေသာ <book> elements ေတြက <bookstore>ထမာ ပါ၀ငေနပါတယ။ <book> element ထမာ ကေလး ေလးေယာကရပါတယ။ <title> <author> <year> န႔<price>တ႔ျဖစပါတယ။

XML Syntax Rules

XML Syntax Rules ေတြက ရးရငးျပး ယတတနပါတယ။ သငယဖ႔လြယကပါတယ။ အသးျပဖ႔အတြကလြယကပါတယ။

အားလးေသာ elements ေတြမာ အပတ tag ပါရပါမယ။

HTML မာဆရင အပတမပါတ႔ elelement ေတြကခဏ ခဏ ျမငရပါလမ႔မယ ေအာကကအတငးေပါ႔။

<p>This is a paragraph<p>This is another paragraph

XML မာကေတာ႔ closing tag ကထားခ႔လ႔လ႔း၀မရပါဘး။ အပတ tag ရကရရပါမယ။

<p>This is a paragraph</p><p>This is another paragraph</p>

မတခကအေနန႔ အေပၚကဥပမာမာ XML declaration စာေၾကာငးမာ closing tag မပါပါဘး။ အဒါမားတာ မဟတပါဘး..declaration က XML document ရ႕အစတအပငးတစခမဟတပါဘး ဒါေၾကာင အပတမပါပါဘး။

XML tags ေတြက Case Sensitive ျဖစပါတယ။ XMLမာ <letter> က <Letter> န႔မတပါဘး။ အပတန႔အဖြင tag ေတြက တညတ႔ case န႔ပ ေရးသားရပါမယ။

<Message>This is incorrect</message>

<message>This is correct</message>

XML elements ေတြက ေသေသခာခာ nested လပရပါမယ။ HTML မာဆရင လပခငသလ လပလ႔ရပါတယ ဥပမာ bold န႔ အရငဖြငထား ျပးမ italic လက ပတတ႔အခါ bold ကအရငပတ ျပးမ italic ကပတသလမး။

<b><i>This text is bold and italic</b></i>

PHP Guide Book Moe Myint Shein

100 Youth Dreams 100

Page 101: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

XML မာကေတာ႔ အဒလမျဖစရပါဘး ေနာကမလာတ႔ဟာအရငပတရပါမယ။ XML မာဆ ေအာကကပစမးန႔ပ nested လပက လပရပါမယ။

<b><i>This text is bold and italic</i></b>

<i> က <b> ရ႕အတြငးထေရာကမ ဖြငတာျဖစတ႔အတြက <b> ရ႕အတြငးထမာပ ပတကပတရပါမယ ဆတ႔ သေဘာပါ။

XML elements ေတြမာ HTML လမးပ တနဖး အမညနာမ အစလကပါတ႔ attributes ေတြရႏငပါတယ။

attribute value ေတြက XML မာ quote လပရပါမယ ေအာကက ပစႏစမးကၾကညပါ ပထမတစမးက မားျပးေတာ႔ ဒတယ လေရးမ မနပါတယ။

<note date=12/11/2007><to>Tove</to><from>Jani</from></note>

ဒပထမဟာမ “ “ မထညထားလ႔မားေနပါတယ။

<note date="12/11/2007"><to>Tove</to><from>Jani</from></note>

ဒဟာကမ မနပါတယ။

Entity References

တစခ႕ အကၡရာေတြမေတာ႔ XMLမာ သးတ႔အခါ ထးျခားတ႔ အဓပါယေတြရၾကပါတယ။ less than “<” character က XMLelement တစခရ႕အတြငးမာ ထားတ႔အခါ error ျဖစမာျဖစပါတယ။ XML parser က ဒအကၡရာက element တစခရ႕ အစပါလ႔ ဘာသာျပနတ႔အတြကျဖစပါတယ။ ေအာကပါစာေၾကာငးက error ျပမာျဖစပါတယ။

<message>if salary < 1000 then</message>

ဒ error ကမေပၚေအာင “<” character က entity reference တစခန႔အစားထးရပါမယ။

<message>if salary &lt; 1000 then</message>

XML မာ ၾကတင သတမတျပးသား entity references ငါးခရပါတယ။

တကယတမးမာေတာ႔ “<” န႔ “&” တ႔ကသာ XML မာ illegal ျဖစတာပါ.. greater than character ကေတာ႔သးႏငပါတယ ဒါေပမယ႔ အေကာငးဆးကေတာ႔ entity reference န႔အစားထးသးတာပါပ။

PHP Guide Book Moe Myint Shein

101 Youth Dreams 101

Page 102: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

Comments in XML

XML မာ comment ေရးတ႔ syntax ကေတာ႔ HTML မာန႔တပါတယ။

<!—This is a comment -- >

What is an XML Element?

XML document တစခမာ XML elements ေတြပါ၀ငပါတယ။ XML element ဆတာ element ရ႕ start tag ကေန elementရ႕ end tag အထကဆလပါတယ။ element တစခမာ တျခား elements ေတြ ထပပါႏငပါတယ။ ရးရးစာေတြလညးပပါႏငပါတယ။ element ေတြမာ သတ႔ရ႕ attributes ေတြလညး ပါ၀ငႏငပါတယ။

<bookstore><book category="CHILDREN"><title>Harry Potter</title><author>J K. Rowling</author><year>2005</year><price>29.99</price>

</book><book category="WEB"><title>Learning XML</title><author>Erik T. Ray</author><year>2003</year><price>39.95</price>

</book></bookstore>

ဒဥပမာမာဆ <bookstore> န႔ <book> တ႔မာ element contents ေတြျဖစၾကပါတယ သတ႔မာ တျခား elements ေတြ ထပမပါ၀ငေနပါတယ။ <author> ကေတာ႔ text content ပါ။ သ႔မာ text ေတြပပါတယ။ ဒဥပမာမာ <book> မာ attributeတစခရပါတယ။ (category=”CHILDREN”)

XML Naming Rules

XML အေနန႔ ေအာကပါအမညေပး ဥပေဒေတြက လကနာရပါမယ။

နာမညေတြမာ letter, numbers ေတြန႔ တျခား character ေတြပါ၀ငႏငပါတယ။ နာမညေတြဟာ နပါတဒါမမဟတ punctuation charcter တစခန႔ မစႏငပါဘး။ နာမညေတြဟာ xml, XML, Xml စတ႔ စကားလးေတြန႔ မစႏငပါဘး။ နာမညေတြမာ spaces ေတြ မပါ၀ငႏငပါဘး။ အဒါအျပငေတာ႔ ဘယနာမညကမဆ အသးျပႏငပါတယ။ ၾကတင ဖယရားထားတ႔ စကားလးမရပါဘး။

XML Elements are Extensible

XML elements ေတြက အခကအလကေတြမားမားပသယေဆာငႏငဖ႔အတြက ခ႔ထြငေရးႏငပါတယ။

ေအာကပါဥပမာကၾကညပါ။

PHP Guide Book Moe Myint Shein

102 Youth Dreams 102

Page 103: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

<note><to>Tove</to><from>Jani</from><body>Don't forget me this weekend!</body></note>

ကေနာတ႔အေနန႔ လခငတ႔ ေအာကက Output က XML document ရ႕ <to>, <from> န႔ <body> elements ေတြကေန ရယမယ႔ application တစခက ဖနတးတယလ႔ ယဆၾကပါစ႔..။

MESSAGE

To: ToveFrom: Jani

Don't forget me this weekend!

အဒအခါမာ ေနာကထပ အခကအလကအပေတြက က ေနာတ႔အခလ ထပထညမယလ႔ယဆမယဆရင

<note><date>2008-01-10</date><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>

ဒ application က crash ျဖစသြားမယ break ျဖစသြားႏငပါသလား။

အေျဖကေတာ႔ No ပါ။ ဒ application အေနန႔ <to>, <from> <body> စတ႔ element ေတြက ရာေတြ႔ျပးေတာ႔ တညတ႔ output ကထတေပးႏငမာပျဖစပါတယ။ XML ရ႕ေကာငးခကတစခကေတာ႔ သက application က ဖကပစျခငးမရဘန႔ extend ထပလပႏငတာျဖစပါတယ။

XML Attributes

XML elements ေတြမာ HTML လမပ သ႔ရ႕ start tag အတြငးမာ attribute ေတြရႏငပါတယ။ Attribute ေတြက elementန႔ပတသတတ႔ အခကအလကေတြကထပျပး ပ႔ပးေပးပါတယ။

HTML မာတနးက က ေနာတ႔အေနန႔ <img src=”computer.gif”> ဆတာမးက မတမၾကမာပါ။ src ဆတ႔ attribute က <img> element အတြကအခကအလကေတြက ထပျပး ပ႔ပးေပးထားတာပါ။

ဥပမာ<img src="computer.gif"><a href="demo.asp">

XML attribute ေတြက “ “ Quote အတြငးမာေရးရပါတယ။ single quote ေရာ double quote ေရာၾကကတာ သးႏငပါတယ။ လတစေယာကရ႕ sex အမးအစားကေဖာျပဖ႔အတြက person ဆတ႔ tag ကဒလေရးသားႏငပါတယ။

PHP Guide Book Moe Myint Shein

103 Youth Dreams 103

Page 104: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

<person sex="female">

ဒါမမဟတ

<person sex='female'>

attribute မာအစကတညးက double quote ပါေနတ႔ဟာမးဆ ေအာကကဥပမာလမး single quote ကအသးျပႏငပါတယ။

<gangster name='George "Shotgun" Ziegler'>

ဒလေရးႏငပါတယ ဒါမမဟတ character entity ကသးႏငပါတယ။

<gangster name="George &quot;Shotgun&quot; Ziegler">

XML Elements vs. Attributes

ေအာကပါဥပမာႏစခက ယဥၾကညပါ။

<person sex="female"><firstname>Anna</firstname><lastname>Smith</lastname>

</person><person><sex>female</sex><firstname>Anna</firstname><lastname>Smith</lastname>

</person>

ပထမတစခမာ sex က attribute ျဖစပါတယ။ ေနာကတစခမေတာ႔ sex က element ျဖစပါတယ။ ဥပမာ ႏစခစလးက တညတ႔အခကအလကေတြက ေဖာျပေပးပါတယ။ ဘယအခနမာ attribute ကသးျပး ဘယအခနမာ elementကသးရမယဆတ႔ ဥပေဒမရပါဘး။ HTML မာေတာ႔ attribute ေတြကအသးမားပါတယ။ XML မာေတာ႔ အဒါေတြမသးတာ ပေကာငးပါတယ။ element ကသာ attribute အစားပသးသငပါတယ။

My Favorite Way

ေအာကေဖာျပပါ XML documents သးခက တညတ႔အခကအလကေတြပါ၀ငပါတယ။

ပထမဥပမာမာ date attribute ကအသးျပထားပါတယ။

<note date="10/01/2008"><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>

ေအာကကဒတယဥပမာမာေတာ႔ date element ကသးပါတယ။

<note><date>10/01/2008</date>

PHP Guide Book Moe Myint Shein

104 Youth Dreams 104

Page 105: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

<to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>

date element ကမ ထပျပး ခ႔ထြငထားတ႔ ပစက ဒတတယေျမာကမာေတြ႔ရပါမယ။ ဒါက ေနာအၾကကဆး ပစပါ။

<note><date><day>10</day><month>01</month><year>2008</year>

</date><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>

XML Attributes for Metadata

တခါတရမာ elements ေတြက ID references ေတြထားေပးရပါတယ။

messages><note id="501">

<to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body>

</note><note id="502">

<to>Jani</to><from>Tove</from><heading>Re: Reminder</heading><body>I will not</body>

</note></messages>

ဒဥပမာမာ ပါတ႔ identifier ကေတာ႔ မတညတ႔ notes ေတြက ခြျခားေပးဖ႔ပျဖစပါတယ။ note ရ႕အစတအပငးတစခ မဟတပါဘး။ metadata ေတြက attributes အေနန႔ သမးသငပါတယ။ data ေတြကေတာ႔ elementsေတြအျဖစသမးသငပါတယ။

XML Validation

မနကနတ႔ syntax ေတြန႔ေရးထားမ႔ xml document တစခက well formed လ႔ေခၚတြငပါတယ။ သ႔ရ႕ syntax ruleေတြကေတာ႔ အေရ႕မာလညး ေျပာလာခ႔ပါျပ။

-XML document ေတြမာ root element တစခရရမယ။

-XML tags ေတြသည case sensitive ျဖစသည။

PHP Guide Book Moe Myint Shein

105 Youth Dreams 105

Page 106: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

-XML elements ေတြက စနစတက nested လပရမယ။

-XML attribute တနဖးေတြက quote ထမာထညေရးရမယ။

<?xml version="1.0" encoding="ISO-8859-1"?><note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>

Valid XML Documents

valid ျဖစတ႔ XML document တစခဟာ document type definitation (DND) ဥပေဒက လကနာတ႔ well form xmldocuments ကဆလပါတယ။

<?xml version="1.0" encoding="ISO-8859-1"?><!DOCTYPE note SYSTEM "Note.dtd"><note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>

အထကပါ ဥပမာမာပါတ႔ DOCTYPE ေၾကျငာခကက ျပငပ DTD file တစခက မျငမးထားတာျဖစပါတယ။ ေအာကမာ အဒ DTD file ထက content ေတြကေဖာျပထားပါတယ။

XML DTD

DTD ရရညရြယခကကေတာ႔ XML document တစခရ႕ ဖြ႔စညးတညေဆာကပက define လပေပးဖ႔ပျဖစပါတယ။ legalelements ေတြန႔ structure ကသတမတေပးပါတယ။

<!DOCTYPE note [<!ELEMENT note (to,from,heading,body)><!ELEMENT to (#PCDATA)><!ELEMENT from (#PCDATA)><!ELEMENT heading (#PCDATA)><!ELEMENT body (#PCDATA)>

]>

XML Schema

W3C က DTD အစားသးႏငတ႔ XML Schema ဆတာကလညး support လပပါေသးတယ။

<xs:element name="note"><xs:complexType><xs:sequence>

<xs:element name="to" type="xs:string"/><xs:element name="from" type="xs:string"/><xs:element name="heading" type="xs:string"/>

PHP Guide Book Moe Myint Shein

106 Youth Dreams 106

Page 107: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

<xs:element name="body" type="xs:string"/></xs:sequence>

</xs:complexType>

</xs:element

XML Validator

XML document ထမာ error ေတြရတယဆရင XML application ကရပလကမာျဖစပါတယ။ error တစခက ေတြ႔တာန႔ XML document တစခက processing လပေနရကေန အဒ Program ကရပလကမယလ႔ W3C XML specificationကေဖာျပပါတယ။ အေၾကာငးျပခကကေတာ႔ XML software က ေသးငယရမယ ျမနရမယ compatibleျဖစရမယဆတ႔အတြက ပါ။

HTML မာေတာ႔ browsers ေတြဟာ error ေတြပါတ႔ document ေတြကလညး ျပသၾကပါလမ႔မယ။ XML မာေတာ႔ errorေတြက ခြငမျပပါဘး။

ကေနာတ႔ XML ေတြရ႕ syntax ကစစေဆးႏငဖ႔အတြက XML validator တစခက ဖနတးအသးျပႏငပါတယ ခငဗာ။

Viewing XML Files

Raw XML file ေတြက ဘယ browser ေတြမာမဆ ၾကညႏငပါတယ။ HTML page ေတြလမး XML files ေတြကျပသ မာေတာ႔ မဟတပါဘး။

<?xml version="1.0" encoding="ISO-8859-1"?>- <note>

<to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body>

</note>

Displaying XML with CSS

XML document တစခက CSS သးျပး ပစခဖ႔ ျဖစႏငပါတယ။ ေအာကပါဥပမာမာ ျပထားပါတယ။

CD Catalog XML file က ဒလပစရပါမယ။

<?xml version="1.0" encoding="ISO-8859-1"?><?xml-stylesheet type="text/css" href="cd_catalog.css"?><CATALOG><CD>

<TITLE>Empire Burlesque</TITLE>

PHP Guide Book Moe Myint Shein

107 Youth Dreams 107

Page 108: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

<ARTIST>Bob Dylan</ARTIST><COUNTRY>USA</COUNTRY><COMPANY>Columbia</COMPANY><PRICE>10.90</PRICE><YEAR>1985</YEAR>

</CD><CD>

<TITLE>Hide your heart</TITLE><ARTIST>Bonnie Tyler</ARTIST><COUNTRY>UK</COUNTRY><COMPANY>CBS Records</COMPANY><PRICE>9.90</PRICE><YEAR>1988</YEAR>

</CD>....</CATALOG>

CSS file က ဒလရပါမယ။

CATALOG{background-color: #ffffff;width: 100%;}CD{display: block;margin-bottom: 30pt;margin-left: 0;}TITLE{color: #FF0000;font-size: 20pt;}ARTIST{color: #0000FF;font-size: 20pt;}COUNTRY,PRICE,YEAR,COMPANY{display: block;color: #000000;margin-left: 20pt;}

အဒါဆရင CSS န႔ format လပထားတ႔ CD catalog က ေအာကပါပစမးျဖစပါတယ။

PHP Guide Book Moe Myint Shein

108 Youth Dreams 108

Page 109: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

XML က CSS န႔ format လပတာက အရးရငးဆနညးလမးပါ။ W3C ကေတာ႔ XSLT ကသးတာက recommend လပပါတယ။ ေရ႕မာ ဆကၾကညၾကရေအာင။

Displaying XML with XSLT

XLST ကသးျပးေတာ႔ XML document တစခက HTML အျဖစက အသြငေျပာငးေပးႏငပါတယ။

XSLT က XML ရ႕ recommend ေပးခရတ႔ style sheet language ျဖစပါတယ။ XSLT(eXtensible Stylessheet LanguageTransformations) က CSS ထကပျပးေတာ႔ ဆနးျပားရႈပေထြးပါတယ။

XSLT ကအသးျပပကေတာ႔ ေအာကက ဥပမာမာျပသလ browser ေန မျပခင XML က HTML အျဖစေျပာငးတာျဖစပါတယ။

ဒါက XML fi le ပါ။

<!-- Edited by XMLSpy® -->

<breakfast_menu>

<food>

<name>Belgian Waffles</name>

<price>$5.95</price>

<description>

two of our famous Belgian Waffles with plenty of real maple syrup

</description>

<calories>650</calories>

</food>

<food>

<name>Strawberry Belgian Waffles</name>

PHP Guide Book Moe Myint Shein

109 Youth Dreams 109

Page 110: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

<price>$7.95</price>

<description>

light Belgian waffles covered with strawberries and whipped cream

</description>

<calories>900</calories>

</food>

<food>

<name>Berry-Berry Belgian Waffles</name>

<price>$8.95</price>

<description>

light Belgian waffles covered with an assortment of fresh berries and whippedcream

</description>

<calories>900</calories>

</food>

<food>

<name>French Toast</name>

<price>$4.50</price>

<description>

thick slices made from our homemade sourdough bread

</description>

<calories>600</calories>

</food>

<food>

<name>Homestyle Breakfast</name>

<price>$6.95</price>

<description>

two eggs, bacon or sausage, toast, and our ever-popular hash browns

PHP Guide Book Moe Myint Shein

110 Youth Dreams 110

Page 111: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

</description>

<calories>950</calories>

</food>

</breakfast_menu>

XSLT Style Sheet က ေအာကပါအတငးပါ။

ဒႏစခေပါငးထားတ႔ result ကေတာ႔

ျဖစပါတယ ခငဗာ။

XML file ရ႕အေပၚဆးမာ ဒလႏစေၾကာငးပါပါမယ။

<?xml version="1.0" encoding="ISO-8859-1"?><?xml-stylesheet type="text/xsl" href="simple.xsl"?>

ဒတယအေၾကာငးကေတာ႔ XML file က XSLT file န႔ ခတဆတေပးတာျဖစပါတယ။

အခအခနထ က ေနာတ႔ XML ရ႕အေျခခေတြက ေလ႔လာခ႔ျပး ျဖစပါတယ။ အခက စျပး PHP န႔ XML န႔အပငးက ဆကလကေလ႔လာပါမယ။

PHP XML Expat Parser

PHP ရ႕ Built in Expat parser က PHP ကသးျပး XML document ေတြက pricess လပဖ႔ ျဖစႏငေစပါတယ။

What is Expat?

XML document တစခက ဖတဖ႔ အသစျပျပငဖ႔ ဖနတးျပး တြကခကထတ ဖ႔အတြက XML parser တစခက လအပပါတယ။

XML parsers အေျခခအားျဖင ႏစမးရပါတယ။

PHP Guide Book Moe Myint Shein

111 Youth Dreams 111

Page 112: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

-Tree-based parser: ဒ parser ကေတာ႔ XML document တစခက tree structure အျဖစက ေျပာငးလေပးပါတယ။ သက document တစခလးက analyze လပျပးေတာ႔ tree elements ေတြဆက access လပေစပါတယ။ ဥပမာ DocumentObject Model(DOM) လမးပါ။

-Event-based parser: XML document တစခက events ေတြပါ၀ငတ႔ series တစခအျဖစ ရႈျမငပါတယ။ specific eventတစခ ျဖစပြားတ႔အခါ သ႔က ကငတြယဖ႔အတြက function တစခက ေခၚယသးပါတယ။

Expat parser ကေတာ႔ event-based parser ျဖစပါတယ။

Event-based parsers က XML document ရ႕ content ေပၚမာ မတညပါတယ Structure ေပၚမာ မမတညပါဘး။ ဒါေၾကာင event-based parser ေတြက tree-based parses ေတြထက ပျမနျမန data က access လပႏငပါတယ။

ေအာကပါအပငးေလးက ၾကညမယဆရင

<from>Jani</from>

event သးခပါတ႔ series တစခအျဖစ အထကပါ XML က event-based parser တစခက ေဖာျပပါတယ။

-Start Element:From

-Start CDATA section, တနဖးက Jani

-Close element: from

ဒ XML ဥပမာက well-formed XML ပါ၀ငပါတယ။ ဒါေပမယ႔ valid XML တစခမဟတပါဘး။ သန႔ ဆကႏြယတ႔ DTDမပါ၀ငလ႔ပါ။ Expat parser ကသးရငေတာ႔ မထးျခားပါဘး။ Expat ကလညး DTDs ေတြက လ စလရႈတ႔ non-validatingparser ပျဖစပါတယ။

event based အေနန႔ non-validating XML parser ျဖစတ႔ Expat ကလညး PHP web applications ေတြအတြက လငျမန ေသးငယ ျပး ျပညစတ႔ အတြအစပတစချဖစပါတယ။ XML document ေတြကေတာ႔ well-formed ျဖစရမာျဖစျပး မဟတရငေတာ႔ Expat က error ထတျပမာျဖစပါတယ။

Installation

XML Expat parser function ေတြက PHP core ရ႕အစတအပငးျဖစပါတယ။ ဒါေၾကာင ဒ function က သးဖ႔အတြက ဘာမ ထပျပး install လပစရာမလပါဘး။

ကေနာတ႔ရ႕ဥပမာမာ ေအာကပါ XML file ကအသးျပၾကပါမယ။

<?xml version="1.0" encoding="ISO-8859-1"?><note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>

PHP Guide Book Moe Myint Shein

112 Youth Dreams 112

Page 113: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

Initializing the XML Parser

PHP မာ XML parser က အစျပပါမယ၊ မတညတ႔ XML events ေတြအတြက handlers အခ႕က သတမတပါမယ။ ျပးတ႔ အခါ XML file က parse လပပါမယ။

ဥပမာ

<?php//Initialize the XML parser$parser=xml_parser_create();//Function to use at the start of an elementfunction start($parser,$element_name,$element_attrs){switch($element_name)

{case "NOTE":echo "-- Note --<br />";break;case "TO":echo "To: ";break;case "FROM":echo "From: ";break;case "HEADING":echo "Heading: ";break;case "BODY":echo "Message: ";}

}//Function to use at the end of an elementfunction stop($parser,$element_name){echo "<br />";}

//Function to use when finding character datafunction char($parser,$data){echo $data;}

//Specify element handlerxml_set_element_handler($parser,"start","stop");//Specify data handlerxml_set_character_data_handler($parser,"char");//Open XML file$fp=fopen("test.xml","r");//Read datawhile ($data=fread($fp,4096)){xml_parse($parser,$data,feof($fp)) ordie (sprintf("XML Error: %s at line %d",xml_error_string(xml_get_error_code($parser)),xml_get_current_line_number($parser)));}

//Free the XML parserxml_parser_free($parser);

PHP Guide Book Moe Myint Shein

113 Youth Dreams 113

Page 114: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

?>

ဒ အထကပါ code ရ႕ output ကေတာ႔ ေအာကပါအတငးပါ။

-- Note --To: ToveFrom: JaniHeading: ReminderMessage: Don't forget me this weekend!

သ႔ရ႕အလပလပပကေတာ႔

1. XML parser ကအစျပဖ႔ xml_parser_create() function ကေခၚသးျပး စလကပါတယ။2. မတညတ႔ event handlers ေတြန႔အသးျပႏငဖ႔ function ေတြက ဖနတးပါတယ။3. parser က opening န႔ closing tag ေတြကေတြ႔တ႔အခါ ဘယ function ေတြက အလပလပေစမယဆတာက

သတမတေပးဖ႔အတြက xml_set_handler() function က ေပါငးထညပါတယ။4. parser က character data က ေတြ႔တ႔အခါ ဘယ function ကတြကခကမယဆတာ သတမတဖ႔အတြက

xml_set_character_data_handler() ကေပါငးထညပါတယ။5. text.xml ဖငေလးက parse လပပါတယ။ xml_parse () function ကသးျပးေတာ႔ပါ။6. အကယလ႔ error ျဖစတယဆ xml_error_string() function ကသးျပး XML error တစခက

စာသားေဖာျပခကတစခ အျဖစ ေျပာငးေပးပါတယ။7. xml_parser_create() function န႔ memory allocate လပထားတာက ဖယထတဖ႔ xml_parser_free() function

က ေခၚလကပါတယ။

PHP XML DOM

built-in ပါျပးသား DOM parser ကသးျပး PHP ထမာ XML documents ေတြက Process လပခြငရေစပါတယ။

What is DOM?

XML DOM(Document Object Model) က XML documents ေတြက access လပတာရယ တြကခကတာရယ အတြက standard နညးလမးတစချဖစပါတယ။ DOM က XML document က tree structure န႔ပျပပါတယ element ေတြ attributeေတြန႔ nodes ေတြအျဖစ စာသားေတြပါ၀ငပါတယ။ေအာကပါပစအတငးပါ။

PHP Guide Book Moe Myint Shein

114 Youth Dreams 114

Page 115: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

XML န႔အလပလပေနတ႔ သတငးက XML DOM ကသကသရပါမယ။

PHP XML DOM ကဆကမၾကညခင DOM အေျခခေလး နညးနညးေတာ႔ ေလ႔လာပါမယ။ DOM ဆတာ W3C (World WideWeb Consortium) Standard ျဖစပါတယ။ DOM က XML န႔ HTML လ document ေတြက access လပဖ႔အတြက စတစခ ပ ျဖစပါတယ။ DOM က သးပငးခြထားပါတယ။

-Core DOM- ဘယလဖြ႔စညးထားတ႔ document မဆအတြက စ model

-XML DOM- XML documents ေတြအတြက စ model

-HTML DOM – HTML documents ေတြအတြက စ model

တ႔ျဖစပါတယ။

DOM က document elements အားလးရ႕ object န႔ properties ေတြက သတမတေပးျပးေတာ႔ သတ႔က access လပမယ႔ methods (interface) ေတြကလညး သတမတေပးပါတယ။ HTML DOM ကေတာ႔ မၾကညေတာ႔ဘ XML DOMကဆကၾကည ပါမယ။

XML DOM ကေတာ႔

-XML အတြက standard Object Model တစချဖစပါတယ။

-XML အတြက standing programming interface ျဖစပါတယ။

-Platform န႔ Language မခမႈမရပါဘး။

-W3C စသတမတခကတစခပါ။

PHP Guide Book Moe Myint Shein

115 Youth Dreams 115

Page 116: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

XML DOM ဆတာ XML elements ေတြက ဘယလ ရယဖ႔ ေျပာငးလဖ႔ ေပါငးထညဖ႔ န႔ ဖကဆးဖ႔ တ႔အတြက စ တစခ ျဖစတယလ႔ ေျပာရပါမယ။

XML DOM Nodes

DOM ထမာဆရင XML document ထမာ ရတ႔ အရာရာတငးက node တစချဖစၾကပါတယ။

DOM အေနန႔ဆရင

-document တစခလးဟာ document node တစချဖစတယ။

-XML element တငးဟာ element node တစချဖစတယ။

-XML elements ထမာရတ႔ text ေတြဟာ text node ေတြျဖစၾကတယ။

- attribute ေတြတငးဟာ attribute node တစချဖစတယ။

-Comments ေတြဟာ comment nodes ေတြျဖစၾကတယ။

DOM Example

<?xml version="1.0" encoding="ISO-8859-1"?><bookstore><book category="cooking"><title lang="en">Everyday Italian</title><author>Giada De Laurentiis</author><year>2005</year><price>30.00</price>

</book><book category="children"><title lang="en">Harry Potter</title><author>J K. Rowling</author><year>2005</year><price>29.99</price>

</book><book category="web"><title lang="en">XQuery Kick Start</title><author>James McGovern</author><author>Per Bothner</author><author>Kurt Cagle</author><author>James Linn</author><author>Vaidyanathan Nagarajan</author><year>2003</year><price>49.99</price>

</book>

PHP Guide Book Moe Myint Shein

116 Youth Dreams 116

Page 117: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

<book category="web" cover="paperback"><title lang="en">Learning XML</title><author>Erik T. Ray</author><year>2003</year><price>39.95</price>

</book></bookstore>

ဒ XML ထက root node က <bookstore> ျဖစပါတယ။ တျခားေသာ nodes ေတြအကနလးက <bookstore> ထမာ ရတာျဖစပါတယ။

root node <bookstore> ထမာ <book> nodes ေလးခရပါတယ။ ပထမ <book> node မာ <title>, <author>,<year> န႔ <price> ဆတ႔ nodes ေလးခရပါတယ .. တစခစမာ "Everyday Italian", "Giada De Laurentiis", "2005",and "30.00" ဆတ႔ text node ေတြပါ၀ငၾကပါတယ။

DOM processing မာျဖစေနၾက error ကေတာ႔ စာသားေတြ ထညဖ႔အတြက element node တစခက ေမာလငတာပါပ။ ဘယလပျဖစျဖစ element node တစခရ႕ စာသားေတြက text node တစခထမာပ သမးဆညးပါတယ။ ဒဥပမာမာဆရင <year>2005</year> မာဆရင <year> ဆတ႔ element node က 2005 ဆတ႔ တနဖးရတ႔ text node တစခက ထနးသမး ပါတယ။ 2005 ဆတာ <year> ဆတ႔ element ရ႕ တနဖးမဟတပါဘး။

XML DOM Node Tree

XML DOM က XML document တစခက node- tree တစခအေနန႔ ျမငပါတယ။ tree တစခမာရတ႔ nodes ေတြအားလး မာ တစခန႔တစခ ဆကသြယခကတစခရၾကပါတယ။

nodes ေတြအားလးက tree ကျဖတျပး access လပႏငပါတယ။ သတ႔ရ႕ content ေတြက ျပငဆငႏငပါတယ၊ ဖကႏငပါတယ၊ အသစ element ေတြ ထပဖနတးႏငပါတယ။ node tree က nodes ေတြ ရ႕အစက ျပသေပးပါတယ။ ျပးေတာ႔ သတ႔ ၾကားထက ဆကသြယခကကလညး ေဖာျပပါတယ။ tree က root node ကေန စျပးေတာ႔ tree ရ႕ အနမ႔ဆး အဆငအထ text node ေတြဆက ခြျဖာသြားပါတယ။

ဒမာ တငျပထားတ႔ ပက ေအာကပါ books.xml ဆတ႔ ဖငေလးက ကယစားျပပါတယ။

PHP Guide Book Moe Myint Shein

117 Youth Dreams 117

Page 118: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

<!-- Edited by XMLSpy® -->

<bookstore>

<book category="cooking">

<title lang="en">Everyday Italian</title>

<author>Giada De Laurentiis</author>

<year>2005</year>

<price>30.00</price>

</book>

<book category="children">

<title lang="en">Harry Potter</title>

<author>J K. Rowling</author>

<year>2005</year>

<price>29.99</price>

</book>

<book category="web">

<title lang="en">XQuery Kick Start</title>

<author>James McGovern</author>

<author>Per Bothner</author>

<author>Kurt Cagle</author>

<author>James Linn</author>

<author>Vaidyanathan Nagarajan</author>

<year>2003</year>

<price>49.99</price>

</book>

<book category="web" cover="paperback">

<title lang="en">Learning XML</title>

<author>Erik T. Ray</author>

PHP Guide Book Moe Myint Shein

118 Youth Dreams 118

Page 119: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

<year>2003</year>

<price>39.95</price>

</book>

</bookstore>

Node Parents, Children, and Siblings

node tree မာပါတ႔ nodes ေတြမာ တစခန႔တစခ မးရးဆငရာ ဆကသြယခက ရၾကပါတယ။ parent, child , subling ဆတ႔ ေ၀ါဟာရေတြက သးျပး သတ႔ရ႕ ဆကသြယခကက ေဖာျပၾကပါတယ။ parent node မာ children ေတြရပါတယ။ level တ children ေတြက siblings ေတြျဖစၾကပါတယ။

-node tree တစခမာ ထပဆး node က root လ႔ေခၚပါတယ။

-ပထမဆး root ကလြျပး က နတ႔ node တငးမာ parent node တစခပရပါတယ။

- node တစခမာ ကေလး ဘယႏစေယာကျဖစျဖစ ရႏငပါတယ။

- leaf ဆတာကေတာ႔ ကေလးမရတ႔ node ကဆလပါတယ။

-sibling ေတြက မဘတညတ႔ nodes ေတြျဖစပါတယ။

ေအာကပါ ပက node tree ရ႕အစတအပငး န႔ nodes ေတြၾကားထက ဆကသြယခကက ေဖာျပပါတယ။

First Child - Last Child

PHP Guide Book Moe Myint Shein

119 Youth Dreams 119

Page 120: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

ေအာကက XML အပငးေလးက ၾကညမယဆရင

<bookstore><book category="cooking">

<title lang="en">Everyday Italian</title><author>Giada De Laurentiis</author><year>2005</year><price>30.00</price>

</book></bookstore>

<title> element က <book> element ရ႕ ပထမဆး ကေလး ျဖစပါတယ။ <price> element ကေတာ႔ <book>element ရ႕ေနာကဆးကေလးျဖစပါတယ။ ဒါ႔အျပင <book> element က <title>, <author>, <year>, န႔ <price>elements ေတြရ႕ parent node ျဖစပါတယ။

XML Parsing in PHP

DOM အေၾကာငး နညးနညး ေလ႔လာျပးျပဆေတာ႔ PHP ဘကျပနသြားပါ႔မယ။ အထကမာ ေျပာခ႔သလပ က ေနာတ႔က XMLdocument တစခက ဖတဖ႔ ျပငဆငဖ႔ ဖနတး တြကခကဖ႔အတြက XML parserကလပါတယ။

XML parser အမးအစားႏစခရျပးေတာ႔ tree-based န႔ event-based ဆျပးျဖစပါတယ။

ခနက ေလ႔လာခ႔တ႔ Expat parser က event based ျဖစပါတယ။အခေလ႔လာမယ႔ DOM parser ကေတာ႔ tree-basedျဖစပါတယ။

ေအာကပါ XML အပငးေလးက ေလ႔လာရေအာင

<?xml version="1.0" encoding="ISO-8859-1"?><from>Jani</from>

XML DOM အေနန႔ ဒ XML ေလးက tree structure န႔ျမငပါတယ။

Level 1: XML Document Level 2: Root element: <from> Level 3: Text element: "Jani"

DOM XML parser ကလညး PHP core ရ႕အစတအပငးျဖစတ႔အတြက သ႔ရ႕ function ေတြကသးဖ႔ ထပမ install လပစရာ မလပါဘး။

ေအာကပါ XML file က က ေနာတ႔ရ႕ ဥပမာအတြက အသးျပပါမယ။

<?xml version="1.0" encoding="ISO-8859-1"?><note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>

PHP Guide Book Moe Myint Shein

120 Youth Dreams 120

Page 121: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

အခ XML parser က အစျပပါမယ ျပးရင xml က load လပျပးေတာ႔ output ျပပါမယ။

ဒါကေတာ႔ PHP code ပါ။

<?php$xmlDoc = new DOMDocument();$xmlDoc->load("note.xml");

print $xmlDoc->saveXML();?>

ဒ code ရ႕ Output က ေအာကပါအတငးျဖစပါတယ။

Tove Jani Reminder Don't forget me this weekend!

တကယလ႔ က ေနာတ႔ browser window ရ႕ view source က ၾကညမယဆရင ေအာကပါ HTML ကျမငရပါမယ။

<?xml version="1.0" encoding="ISO-8859-1"?><note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>

ဒဥပမာဟာဆရင DOM Document Object တစခက ဖနတးျပးေတာ႔ သ႔အထက note.xml ကေန XML ကလမးယျပး loadလပပါတယ။ အဒေတာ႔ saveXML() function န႔ XML document က string အျဖစေျပာငးျပး print ထတလ႔ရေစပါတယ။

Looping through XML

ဆပါစ႔ XML parser ကစမယ ျပးရင XML က load လပမယ ျပးတ႔အခါ <note> element ထမာရတ႔ elementေတြအားလးက looping ပတမယေပါ႔။

<?php$xmlDoc = new DOMDocument();$xmlDoc->load("note.xml");$x = $xmlDoc->documentElement;foreach ($x->childNodes AS $item){print $item->nodeName . " = " . $item->nodeValue . "<br />";}

?>

ဒ code ရ႕output ကေတာ႔ ဒလပါ။

#text =to = Tove#text =from = Jani#text =heading = Reminder#text =body = Don't forget me this weekend!

PHP Guide Book Moe Myint Shein

121 Youth Dreams 121

Page 122: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

#text =

ဒဥပမာမာဆရင element တစခစရ႕ၾကားမာ ဗလာ ျဖစေနတ႔ text nodes ေတြရတာကေတြ႔ရပါမယ။ XML generateလပတ႔အခါ nodes ေတြရ႕ၾကားထမာ white-space ေတြပါ၀ငလာပါတယ။ XML DOM parser က အဒါေတြက သာမန element ေတြအျဖစမတယပါတယ။ တကယလ႔ ကေနာတ႔က သတမထားမဘးဆ တခါတေလ ျပသနာေတြကျဖစေစမာပါ။

PHP SimpleXML

SimpleXML ကေတာ႔ PHP5 မာမ ပါ၀ငလာတာပါ။ သကလညး တကယလ႔က ေနာတ႔က XML document ရ႕ layoutကသရင element တစခရ႕ attribute န႔ text ေတြက ရယႏငဖ႔အတြက လြယကတ႔ နညးလမးတစခပျဖစပါတယ။

DOM တ႔ Expat parser တ႔န႔ယဥမယဆ SimpleXML က element တစခကေန text data ေတြဖတဖ႔အတြက codeနညးနညးပ လအပပါတယ။

SimpleXML က ေအာကပါအတငး XML document တစခက object တစခအျဖစေျပာငးလေပးပါတယ။

Elements- ေတြက SimpleXMLElement object ရ႕ သးျခားattribute အျဖစေျပာငးေပးပါတယ။ level တစခမာ elementတစခထကပျပးရေနရင array တစခအတြငးမာ ထားေပးပါတယ။

Attributes ေတြကေတာ႔ associative array ကအသးျပျပး access လပပါတယ။ index တစခက attribute name တစခက ဆကသြယေနပါတယ။

Element data- element ေတြက text data ေတြကေတာ႔ strings ေတြအျဖစေျပာငးေပးပါတယ။ element တစခမာ textnode တစခထကပျပးရေနရင သတ႔က ရတ႔ order အတငး အစအစဥတက ထားေပးပါတယ။

SimpleXML က ေအာကပါအေျခခလပငနးစဥေတြကေဆာငရြကတ႔အခါ လြယကျပး ျမနဆနပါတယ။

-XML file ေတြက read လပတာ

-XML string ေတြက data ေတြက extract လပတာ

-text nodes ေတြဒါမမဟတ attribute ေတြကတညးျဖတတာ။

ဘယလပျဖစျဖစျဖစ advanced XML မာအလပလပတ႔အခါ Expat parser ဒါမမဟတ XML DOM ကသးတာက ပျပး သငေလာပါတယ။

PHP 5.0 မာ SimpleXML functions ေတြက PHP core ထမာပါလာျပးေတာ႔ ဒ function ေတြက install လပစရာမလဘ သးႏငပါတယ။

Using SimpleXML

ေအာကကေတာ႔ XML file တစခပါ။

<?xml version="1.0" encoding="ISO-8859-1"?><note>

PHP Guide Book Moe Myint Shein

122 Youth Dreams 122

Page 123: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

<to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body></note>

ကေနာတ႔က အဒ file ထကေန element အမညန႔ data ေတြက Output ျပခငတယဆပါစ႔

လပရမာေတြကေတာ႔

1. XML file က Load လပမယ။2. ပထမဆး element ရ႕အမညက ရယမယ။3. child node တစခစက children()function ကသးျပးေတာ႔ loop လပမယ။4. child node တစခစရ႕ element အမညန႔ data ေတြက ထတျပမယ။

ဥပမာ

<?php$xml = simplexml_load_file("test.xml");echo $xml->getName() . "<br />";foreach($xml->children() as $child){echo $child->getName() . ": " . $child . "<br />";}

?>

ဒ code က run ရင ဒလ Output မးရပါျပ။

noteto: Tovefrom: Janiheading: Reminderbody: Don't forget me this weekend!

PHP Guide Book Moe Myint Shein

123 Youth Dreams 123

Page 124: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

Chapter 10. Exceptiong Handling & Filter in PHP

Exception ဆတာဘာလ

PHP 5 န႔အတ error ေတြန႔ပတသတျပးကငတြယဖ႔အတြက Object Oriented နညးလမးအသစတစခ ထြကေပၚလာပါတယ။

Exceptiong handling ဆတာ error အေျခအေနတစခချဖစလာျပဆရင code က execute လပေနတ႔ သ႔ရ႕ flow က ေျပာငးလ ပစလကဖ႔အသးျပတာျဖစပါတယ။ ဒအေျခအေနက exception တစခလ႔ေခၚတြငပါတယ။

ေအာကပါအေျခအေနေတြကေတာ႔ exception တစချဖစပြားသြားျပဆရင ပမနျဖစပကသြားမယ႔ ကစၥေတြေပါ႔။

- လကရ code ရ႕႔အေျခအေနက သမးဆညးထား(save) ထားေပးမယ။- code execution က (custom) ၾကတင သတမတေပးထားတ႔ exception handler function တစခ ဆက

ကးေျပာငး သြားေပးမယ။- အေျခအေနေပၚမာမတညျပးေတာ႔ handler က save လပေပးထားတ႔ code အေျခအေနကေန ဆကျပးေတာ႔ run

တာက resume လပေပးရငလပေပးမယ။ ဒါမမဟတ script execution က အဆးသတပစလကမယ။ ဒါမမဟတ code ရ႕တျခားတစေနရာကေန script ကဆကျပးေတာ႔ run သြားေပးမယ။

ေအာကပါError handling methods အမးမးက ေလ႔လာၾကပါမယ။

Basic use of Exceptions Creating a custom exception handler Multiple exceptions Re-throwing an exception Setting a top level exception handler

မတခက - Exception မားက error အေျခအေနမားတြငသာ အသးျပသငသည။ code ထတြကေနရာတစခ မ တျခားေနရာ တစခသ႔ ခနကးရနအတြက အသးမျပသငပါ။

Basic Use of Exceptions

exception တစချဖစပြားသြားျပဆရင code ေတြကဆကျပး execute လပမာမဟတေတာ႔ပါဘး။ PHP က catch ဆတ႔ blockက ရာေဖြဖ႔ ၾကးစားပါမယ။ exception တစခက caught မလပထားဘးဆရင Uncaught Exception ဆျပး errorျဖစပြားသြား မာပါ။ catch မလပဘန႔ exception တစခက ဖနတးၾကရေအာင။

<?php//create function with an exceptionfunction checkNum($number){if($number>1){throw new Exception("Value must be 1 or below");}

return true;}

PHP Guide Book Moe Myint Shein

124 Youth Dreams 124

Page 125: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

//trigger exceptioncheckNum(2);?>

ကေနာ႔ဆမာ အခလ error ျပပါတယ။

Fatal error: Uncaught exception 'Exception' with message 'Value must be 1 or below' inC:\xampp\htdocs\php\1.php:7 Stack trace: #0 C:\xampp\htdocs\php\1.php(13): checkNum() #1 {main}thrown in C:\xampp\htdocs\php\1.php on line 7

Try, throw and catch

Try, throw န႔ catch တကေတာ႔ exception ေတြမာ သးတ႔ ေ၀ါဟာရေတြပါ။ အထကလမး error ေတြမျဖစရေအာင exception တစခက ကငတြယေျဖရငးဖ႔အတြက code ေသခာဖနတးဖ႔လပါတယ။

Proper exception code ေပါ႔ စနစတက ရတ႔ ကငတြယႏငမယ code ေတြမာ ေအာကပါအခကေတြ ပါ၀ငသငပါတယ။

1.Try- exception တစခ ကအသးျပမယ႔ function က try block တစခ ထမာရေနသငပါတယ။ exception တစခ မျဖစပြားသြားဘးဆရင ဒ code ေတြက ပမနအတငးပ ဆက run သြားမာပါ။ မဟတဘ exception တစခ ျဖစပြားသြားမယ ဆမသာ exception တစခ thrown လပေပးမာေပါ႔။

2.Throw- ဒါကေတာ႔ exception တစခက က ေနာတ႔ ဘယလ ေျဖရငးမယဆတာကေပါ႔။ throw တငးမာ အနညးဆးေတာ႔ catch တစခရရပါမယ။

3. Catch- catch block တစခက exception တစခက လကခရယတယ ျပးတ႔ေနာက exception information ေတြပါ၀ငတ႔ object တစခက ဖနတးေပးပါတယ။

valid code ေတြ exception တစခက ေျဖရငးၾကညရေအာင

<?php//create function with an exceptionfunction checkNum($number){if($number>1){throw new Exception("Value must be 1 or below");}

return true;}

//trigger exception in a "try" blocktry{checkNum(2);//If the exception is thrown, this text will not be shownecho 'If you see this, the number is 1 or below';}

//catch exceptioncatch(Exception $e)

PHP Guide Book Moe Myint Shein

125 Youth Dreams 125

Page 126: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

{echo 'Message: ' .$e->getMessage();}?>

ဒ ေဖာျပပါ code က ေအာကပါအတငး error ကေကာငးေကာငး မြနမြန output ထတေပးပါတယ။

Message: Value must be 1 or below

ဒေဖာျပပါ ဥပမာက exception တစခက throw ျဖစေစပါတယ ျပးတ႔ေနာကမာ ျပနျပး catch လပလကပါတယ။

1. checkNum() function ကဖနတးပါတယ။ သက နပါတတစခက တစထက ၾကးလား စစတာက လပပါတယ။ တကယလ႔ ၾကးတယဆရင exception တစခက ျဖစေစပါတယ။

2. try block ထမာ checkNum() function ကေခၚလကပါတယ။3. checkNum() function ထမာ ရတ႔ exception ၾကးျဖစပြား(thrown) သြားပါတယ။4. catch block က exception ကရယျပးေတာ႔ ($e) object တစခကဖနတးပါတယ။ ဒ object မာ exception

information ေတြပါ၀ငပါတယ။5. exception အတြက error message က $e->getMessage() ဆျပးေတာ႔ exception object ဆကေန

လမးေခၚျပး echo န႔ထတျပပါတယ။

Creating a Custom Exception Class

ကယ႔ဘာသာကယ custom exception class တစခဖနတးတာကလညး ရးရငးပါတယ။ PHP မာျဖစပြားမယ႔ exceptionတစခ ေပၚေပါကလာရင ေခၚသးလ႔ရမယ႔ function ေတြပါမယ႔ class တစခက ဖနတးရပါ။ ဒ class ကေတာ႔ exceptionclass ရ႕ extension ျဖစရပါမယ။

ကယ႔ဘာကဖနတးမယ႔ custom exception class က PHP ရ႕ excepton class ဆက ဂဏ သတေတြက မးရးဆကခရမာ ျဖစျပးေတာ႔ custom function ေတြက ထပေပါငးထညေပးႏငပါတယ။

exception class တစခက ဖနတးၾကရေအာင

<?phpclass customException extends Exception{public function errorMessage(){//error message$errorMsg = 'Error on line '.$this->getLine().' in '.$this->getFile().': <b>'.$this->getMessage().'</b> is not a valid E-Mail address';return $errorMsg;}

}$email = "[email protected]";try{//check ifif(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE){//throw exception if email is not validthrow new customException($email);

PHP Guide Book Moe Myint Shein

126 Youth Dreams 126

Page 127: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

}}catch (customException $e){//display custom messageecho $e->errorMessage();}?>

class အသစက old exception class ရ႕ ေကာပ တစချဖစပါတယ။ သ႔မာ အပအေနန႔ errorMessage() ဆတ႔ functionတစခ ပါလာပါတယ။ old class ရ႕ copy ျဖစတ႔အတြက properties န႔ method ေတြက old class ကေန မးရးလကပါတယ။ က ေနာတ႔အေနန႔ getLine(), getFile() န႔ getMessage() လမး exception class methods ေတြက အသးျပႏငပါတယ။

အထကကဥပမာက ရငးရရင

ဒ ဥပမာမာ exception တစခက ျဖစပြားေစျပးေတာ႔ custom exception class တစခန႔ ျပနဖမးထားပါတယ။

1. customException() class တစခက Old exception class ရ႕ extension တစခအေနန႔ ဖနတးပါတယ။ ဒါေၾကာင old exception class ကေန methods န႔ properties ေတြအားလးက မးရးဆကခပါတယ။

2. errorMessage() ကဖနတးပါတယ။ email address က invalid ျဖစေနမယဆရင ဒ function က errormessage တစခက ထတေပးတာပါ။

3. $email variable က valid မျဖစတ႔ email လပစာတစခအျဖစ ထားေပးလကတ႔အခါ။4. try block က execute လပလကေတာ႔email လပစာက valid မျဖစတ႔အတြက exception တစခ thrown

သြားပါတယ။5. catch block က exception က ဖမးထားလကျပးေတာ႔ error message ကထတျပေပးပါတယ။

Multiple Exceptions

အေျခအေနေတြ အမားၾကးက စစေဆးဖ႔အတြက exception ေတြအမားၾကးက အသးျပဖ႔ script တစခတညးက သးႏငပါတယ။ if else block ေတြ switch တ႔ nested multiple exception ေတြအမားၾကးက အသးျပလ႔ရပါတယ။ ဒ exception ေတြကေန မတညတ႔ exception class ေတြကအသးျပျပးေတာ႔ မတညတ႔ error message ေတြက ထတေပးပါလမ႔မယ။

<?phpclass customException extends Exception{public function errorMessage(){//error message$errorMsg = 'Error on line '.$this->getLine().' in '.$this->getFile().': <b>'.$this->getMessage().'</b> is not a valid E-Mail address';return $errorMsg;}}

$email = "[email protected]";

try{

PHP Guide Book Moe Myint Shein

127 Youth Dreams 127

Page 128: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

//check ifif(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE){//throw exception if email is not validthrow new customException($email);}

//check for "example" in mail addressif(strpos($email, "example") !== FALSE){throw new Exception("$email is an example e-mail");}

}

catch (customException $e){echo $e->errorMessage();}catch(Exception $e){echo $e->getMessage();}?>

run လကမယ ဆရင [email protected] is an example e-mail ဆျပးေတာ႔ ရလာပါလမ႔မယ။

code ေတြကျပနရငးရရငေတာ႔

1. customException() class တစခက Old exception class ရ႕ extension တစခအေနန႔ ဖနတးပါတယ။ ဒါေၾကာင old exception class ကေန methods န႔ properties ေတြအားလးက မးရးဆကခပါတယ။

2. errorMessage() ကဖနတးပါတယ။ email address က invalid ျဖစေနမယဆရင ဒ function က error messageတစခက ထတေပးတာပါ။

3. $email variable က valid ျဖစတ႔ email လပစာတစခအျဖစ ထားေပးထားပါတယ။ ဒါေပမယ႔ example ဆတ႔ string တစခပါေနပါတယ။

4. try block က execute လပပါတယတယ ပထမ အေျခအေနမာ exception တစခမျဖစပြားပါဘး။5. ဒတယအေျခအေနၾကမာသာ example ဆတ႔ စာသားေၾကာင exception တစချဖစပြားသြားပါတယ။6. catch ဘေလာ႔က exception ကဖမးလကပါတယ ျပးေတာ႔ မနကနတ႔ error message ကထတျပေပးပါတယ။

Re-throwing Exceptions

တစခါတေလ exception တစခက thrown လပျပးတ႔ေနာက ပမနနညးလမးန႔မဟတဘ တျခားနညးန႔ ကငတြယ ခငပါလမ႔မယ။ ဒတယအၾကမ catch block တစခန႔ exception တစခက throw လပလ႔ရပါတယ။

script တစခရ႕ဂဏ သတေကာငး တစခအေနန႔ user ေတြက system error ေတြမျမငရေအာင လပေဆာငေပးႏငရပါမယ။ System error ေတြက coder ေတြအတြကအေရးၾကးပါတယ ဒါေပမယ႔ user ေတြအတြက ဘာမ စတ၀ငစားစရာ မရဘးေလ။ user ေတြအတြက အရာရာ လြယကေနေစဖ႔ user friendly message တစခန႔ exception ေတြက re-throwလပႏငပါတယ။

<?phpclass customException extends Exception{

PHP Guide Book Moe Myint Shein

128 Youth Dreams 128

Page 129: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

public function errorMessage(){//error message$errorMsg = $this->getMessage().' is not a valid E-Mail address.';return $errorMsg;}

}$email = "[email protected]";try{try{//check for "example" in mail addressif(strpos($email, "example") !== FALSE){//throw exception if email is not validthrow new Exception($email);}

}catch(Exception $e){//re-throw exceptionthrow new customException($email);}

}catch (customException $e){//display custom messageecho $e->errorMessage();}?>

Output ကေတာ႔

[email protected] is not a valid E-Mail address. ျဖစပါတယ။

1. customException() class တစခက Old exception class ရ႕ extension တစခအေနန႔ ဖနတးပါတယ။ ဒါေၾကာင old exception class ကေန methods န႔ properties ေတြအားလးက မးရးဆကခပါတယ။

2. errorMessage() ကဖနတးပါတယ။ email address က invalid ျဖစေနမယဆရင ဒ function က error messageတစခက ထတေပးတာပါ။

3. $email variable က valid ျဖစတ႔ email လပစာတစခအျဖစ ထားေပးထားပါတယ။ ဒါေပမယ႔ example ဆတ႔ string တစခပါေနပါတယ။

4. try block ထမာ exception က re-throw လပႏငေစဖ႔အတြက ေနာကထပ try block တစခရေနပါတယ။5. email မာ example ဆတ႔ string ပါေနလ႔ exception ျဖစပြားသြားပါတယ။6. catch block က exception ကဖမးျပးေတာ႔ customException တစခက re-throws လပလကပါတယ။7. customException က ဖမးလကျပးေတာ႔ error message တစခထတျပေပးပါတယ။

Set a Top Level Exception Handler

set_exception_hadnler() function ကေန မဖမးလကရတ႔ exception ေတြအားလးက ကငတြယဖ႔အတြက user-definedfunction တစခက set လပေပးပါတယ။

PHP Guide Book Moe Myint Shein

129 Youth Dreams 129

Page 130: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

<?phpfunction myException($exception){echo "<b>Exception:</b> " , $exception->getMessage();}set_exception_handler('myException');throw new Exception('Uncaught Exception occurred');?>

output ကေတာ႔

Exception: Uncaught Exception occurred

ဒ code မာ catch block မပါ၀ငပါဘး အဒအစား top level exception handler ကသးပါတယ။ ဒ function က uncaughtexception ေတြအတြက အသးျပသငပါတယ။

Exception အတြက ဥပေဒမား

1. Code ေတြက try block ထမာ ထားသငပါတယ။2. try block ဒါမမဟတ throw တငးမာ အနညးဆးေတာ႔ သတဆငတ႔ catch block တစခရရပါမယ။3. မတညတ႔ exception class ေတြအတြက catch block ေတြအမားၾကး အသးျပႏငပါတယ။4. try block တစခအတြငးမာ ရတ႔ catch block တစခအတြငးမာ exception ေတြက thrown (or rethrown)

လပႏငပါတယ။5. ေနာကထပ ရငးလငးတ႔ ဥပမာကေတာ႔ တစခခက ပစထတ (throw) လကျပဆရင အဒါက ျပနဖမး (catch)

ရပါမယ။

PHP Filter

PHP Filter ဆတာဘာလ ?

PHP filter က စတမခရတ႔ ေနရာေတြကေနလာမ႔ ဥပမာ user ေတြက သြငးမယ႔ input လဟာမးက လာမယ႔ ေဒတာ ေတြက valid ျဖစမျဖစ န႔ filter လပေပးဖ႔အတြက အသးျပရပါတယ။

user imputer ေတြက စစေဆးမယ validate လပမယ filter လပမယ ဒါေတြက ဘယ web application မာမဆ အေရးၾကးတ႔ အစတအပငးပျဖစပါတယ။

PHP filter က လြယလြယကက ျမနျမနဆနဆန filter(စစခ) လပႏငဖ႔အတြက ဖနတးထားျပးသားပါ။

ဘာလ႔ Filter ကသးရတာလ?

web application ေတြ အားလးနးပါးဟာ ျပငပကလာတ႔ Input ေတြေပၚမာ မခေနၾကပါတယ။ အမားအားျဖင အဒါေတြက user တစေယာကေယာကဆက ဒါမမဟတ တျခား web service လမး application တစခဆကေနျပးေတာ႔ပါ။ filter က သးလကမယဆရင.. က ေနာတ႔ရ႕ application က မနကနတ႔ input အမးအစားေတြက ရရမယလ႔ ေသခာေစမာ ျဖစပါတယ။

PHP Guide Book Moe Myint Shein

130 Youth Dreams 130

Page 131: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

က ေနာတ႔ အျပငကလာတ႔ data မနသမက အျမတမး filter လပသငပါတယ။ input filtering က application ေတြရ႕ security န႔ပတသကျပးေတာ႔ အေရးအၾကးဆးအခကတစခကပျဖစပါတယ။

external data ဆတာဘာေတြလညးဆေတာ႔

- form တစခကေန ထညေပးၾကတ႔ Input data ေတြ- Cookies ေတြ- Web services ေတြကေနလာတ႔ data ေတြ- server variable ေတြ န႔- Database query result ေတြျဖစပါတယ။

Functions and Filters

variable တစခက filter လပဖ႔အတြက ေအာကပါ filter function ေတြထက တစခက အသးျပပါတယ။

- filter_var()- တက ေသခာတ႔ filter တစခကသးျပးေတာ႔ variable တစလးက filter လပတာ- filter_var_array()- တညတ႔ ဒါမမဟတ မတညတ႔ filter ေတြကသးျပးေတာ႔ variable ေတြအမားၾကးက filter

လပတာ။- filter_input- variable input တစခတညးက ရယျပးေတာ႔ သ႔ကပ filter လပတာန႔- filter_input_array- input variable ေတြအမားၾကးက ရယထားျပးေတာ႔ တညတ႔ ဒါမမဟတ မတညတ႔ filter

ေတြန႔ filter လပတာပ ျဖစပါတယ။

ေအာကပါ ဥပမာမာ filter_var() function ကအသးျပျပးေတာ႔ integer တစလးက valid ျဖစမျဖစ စစပါတယ။

<?php$int = 123;if(!filter_var($int, FILTER_VALIDATE_INT)){echo("Integer is not valid");}else{echo("Integer is valid");}?>

ဒအထကပါcode က variable ကfilter လပဖ႔အတြက “FILTER_VALIDATE_INT” filter ကအသးျပထားပါတယ။Integer က valid ျဖစတ႔အတြက output ကေတာ႔“ Interger is valid”. လ႔ရပါလမ႔မယ။

integer မဟတတ႔variable ဥပမာ123abc လ႔စမးၾကညမယဆရင“Integer is not valid” ဆျပးေတာ႔ထတေပးမာျဖစပါတယ။

Validating and Sanitizing

PHP Guide Book Moe Myint Shein

131 Youth Dreams 131

Page 132: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

filter ႏစမးရပါတယ။Validating filters န႔Sanitizing filters တ႕ပါ။

Validating filters ေတြက- user input ေတြကvalidate လပဖ႔- format rule ေတြကတတက က သတမတေပးဖ႔(ဥပမယ URL န႔EMAIL)- ေအာငျမငတ႔အခါ ေမာလငထားတ႔အမးအစားကထတေပးျပးေတာ႔validate မျဖစတ႔အခါFALSE က

ျပနထတေပးဖ႔အသးျပပါတယ။

Sanitizing filters ေတြကေတာ႔- string တစခထမာရတ႔character တစခခေတြကခြငျပမလားခြငမျပဘးလားဆတာအတြကသးပါတယ။- data format rules မရပါဘး- အျမတမးString ကသာreturn ျပနေပးပါတယ။

Options and Flags

options န႔flags တ႔က ေရြးခယထားတ႔filters ေတြအတြက အပfilter options ေတြထပေပါငးထညဖ႔အသးျပပါတယ။မတညတ႔filterေတြမာမတညတ႔Options န႔flags ေတြရၾကပါတယ။ေအာကပါဥပမာမာfilter_var() function ရယ min_range န႔max_range option ေတြကသးျပးinteger တစခကကေနာတ႔validate လပပါတယ။

<?php$var=300;$int_options = array("options"=>array("min_range"=>0,"max_range"=>256));if(!filter_var($var, FILTER_VALIDATE_INT, $int_options)){echo("Integer is not valid");}else{echo("Integer is valid");}?>

ဒcode မာoptions ေတြကoption လ႔အမညေပးျပးေတာ႔associative array တစခထမာထညထားရပါတယ။flagကအသးျပတ႔အခါမာေတာ႔သက array ထမာရေနဖ႔မလအပပါဘး။integer က 300 ျဖစတ႔အတြက သက သတမတထားတ႔အတငးအတာrange ထမာမရေနပါဘး။ဒါေၾကာငဒcode ကrun ရင“Integer is not valid” လ႔ျပနပါမယ။Validate Input

form တစခက input form တစခကvalidate လပဖ႔ၾကးစားၾကရေအာင။

PHP Guide Book Moe Myint Shein

132 Youth Dreams 132

Page 133: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

ပထမဆးလပဖ႔လအပတာက က ေနာတ႔ရာေဖြေနတ႔input data ရဖ႔ ေသခာေစဖ႔ျဖစပါတယ။ ျပးတ႔အခါfilter_input ()function ကသးျပးေတာ႔input data ကfilter လပပါမယ။ေအာကပါဥပမာမာinput variable “email” ကPHP page ကပ႔ေပးပါတယ။

<?phpif(!filter_has_var(INPUT_GET, "email")){echo("Input type does not exist");}else{if (!filter_input(INPUT_GET, "email", FILTER_VALIDATE_EMAIL)){echo "E-Mail is not valid";}

else{echo "E-Mail is valid";}

}?>

ဒဥပမာမာinput ျဖစတ႔(URL) က“POST” Method သးျပးေတာ႔ပ႔ေပးထားပါတယ။1. POST type ရ႕url input က ရမရစစပါတယ။2. ရတယဆရငအဒါကsanitize(valid မျဖစတ႔character) ေတြကဖယထတျပးေတာ႔$url variable ထမာ

သမးေပးပါတယ။

တကယလ႔Input string က http://www.moååemyintsøhein.com/ ျဖစေနမယဆရငsanitizing လပျပးတ႔အခါမာေတာ႔http://www.moemyintshein.com လ႔ျဖစသြားမာပါ။

Filter Multiple Inputs

Form တစခအေနန႔အျမတမးinput fileld တစခထကမက ရပါတယ။filter_var ဒါမမဟတ filter_input function ေတြကတခါျပးတစခါ ေခၚရတာမးမျဖစေစဖ႔က ေနာတ႔က filter_var_array ဒါမမဟတ filter_input_array စတ႔function ေတြကအသးျပၾကပါတယ။ဒဥပမာမာက ေနာတ႔က GET variable သးခကfilter လပဖ႔အတြက filter_input_array() function ကအသးျပပါတယ။လကခရရတ႔GET variable က နာမညတစခအသကတစခန႔အးေမးလပစာတစခတ႔ျဖစပါတယ။

<?php$filters = array("name" => array(

PHP Guide Book Moe Myint Shein

133 Youth Dreams 133

Page 134: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

"filter"=>FILTER_SANITIZE_STRING),

"age" => array("filter"=>FILTER_VALIDATE_INT,"options"=>array("min_range"=>1,"max_range"=>120)

),"email"=> FILTER_VALIDATE_EMAIL,);$result = filter_input_array(INPUT_GET, $filters);if (!$result["age"]){echo("Age must be a number between 1 and 120.<br />");}elseif(!$result["email"]){echo("E-Mail is not valid.<br />");}else{echo("User input is valid");}?>

ဒဥပမာမာ(name,age,email) ဆတ႔Input သးခရပါတယ ျပးေတာ႔GET method န႔ပ႔ထားပါတယ။

1. input variable ေတြရ႕အမညပါ၀ငတ႔array တစခကသတမတပါတယ ျပးေတာ႔input variables ေတြအတြကfilter ေတြကလညးသတမတပါတယ။

2. filter_input_array() function ကGET input variable ေတြန႔က ေနာတ႔ဖနတးထားတ႔array တ႔န႔ေခၚလကပါတယ။

3. valid input ေတြရဖ႔အတြက $result variable အတြငးမာage, email variable ေတြကစစပါတယ။ဒinputေတြထက တစခခinvalid ျဖစတယဆရငfilter_input_array() function ျပးတ႔အခါမာinput variable က FALSEျဖစမာျဖစပါတယ။

filter_input_array() function ရ႕ဒတယ parameter က array လညးျဖစႏငသလsingle filter ID တစခလညးျဖစႏငပါတယ။တကယလ႔parameter က single filter ID ဆရငinput array ရ႕တနဖးေတြအားလးက သတမတထားတ႔filter အားျဖငfilter လပသြားမာျဖစပါတယ။

parameter က array ျဖစတယဆရင ေအာကပါဥပေဒေတြကလကနာရပါမယ။- (age input variable လမး) array key တစခအေနန႔input variable တစခပါ၀ငတ႔associative array တစခ

ျဖစရပါမယ။- array တနဖးက filter ID ဒါမမဟတ filter, flag န႔option ေတြကသတမတေပးထားတ႔array တစချဖစရပါမယ။

PHP Guide Book Moe Myint Shein

134 Youth Dreams 134

Page 135: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

Using Filter Callback

user defined function တစခက ေခၚသးျပးေတာ႔FILTER_CALLBACK filter ကသးျပးfilter လပဖ႔ ျဖစႏငပါတယ။ဒနညးလမးန႔က ေနာတ႔data filtering ကအျပညအ၀ထနးခပႏငပါတယ။ကေနာတ႔အေနန႔ကယပငUser defined function ကဖနတးႏငသလရျပးသားPHP function တစခကသးႏငပါတယ။ေအာကပါဥပမာမာက ေနာတ႔က “_” ေတြကwhitespaces ေတြအျဖစေျပာငးေပးတ႔user created function တစခကအသးျပပါတယ။

<?phpfunction convertSpace($string){return str_replace("_", " ", $string);}

$string = "Peter_is_a_great_guy!";

echo filter_var($string, FILTER_CALLBACK,array("options"=>"convertSpace"));?>

output ကေတာ႔

Peter is a great guy!

ျဖစပါတယ။

အလပလပပကေတာ႔

1. “_” ေတြကwhitespaces ေတြအျဖစေျပာငးေပးမယ႔fuction တစခကဖနတးပါတယ။2. FILTER_CALLBACK filter ရယ က ေနာတ႔function ပါတ႔array တစခရယန႔အတ filter_var() function

ကေခၚသးပါတယ။

PHP Guide Book Moe Myint Shein

135 Youth Dreams 135

Page 136: PHP GUIDE BOOK - Ningapi.ning.com/files/ozSMl9PATp16UpSJDdEktgfZ3nzZb0N7TE2... · 2017-05-28 · Contents at a Glance Chapter 1 Introducing PHP 5 Chpater 2 The Building Blocks of

Conclusionကေနာ႔ရ႕စာအပက ဖတျပး ေလ႔လာၾကသအားလးကေက းဇးတငပါတယခငဗာ။ က ေနာ အတတႏငဆး ၾကးစားထားပါတယ။ လအပတာမားရရင ေ၀ဖနအၾကျပႏငၾကပါတယ။ ဒေနရာမာ ရပမထားဘဆကလကေလ႔လာသြားႏငဖ႔အတြက English လေရးထားတ႔PHP စာအပေတြကကေနာ႔ဘေလာ႔မာတငေပးထားပါမယ။ ေဒါငးလပဆြျပးဆကလကေလ႔လာသြားၾကပါ။အားလးကယျဖစခငတာေတြက ေအာငျမငေအာငလပေဆာငႏငၾကပါေစ။

Moe Myint SheinEmail: [email protected]: www.moemyintshein.com

PHP Guide Book Moe Myint Shein

136 Youth Dreams 136