PHP: Control Structures Part I

PHP: Control Structures

LEVEL : Intermediate

Knowledge Required: HTML, PHP Basic

Control Structures ဆိုတာကတော့ ဘယ် Programming Language မှာမဆို ရှိပါတယ်။ Control Structure တွေထဲမှာ Conditional statements တွေ Loops တွေနဲ့ အခြား Control structures တွေ ပါဝင်ပါတယ်။ အားလုံးကတော့ အချက်အလက်ကို ထိန်းချုပ် ကွပ်ကဲတဲ့ နေရာမှာ သုံးတာတွေပါပဲ။

PHP က အထောက်အပံ့ပေးနိုင်တဲ့ Control Structure တွေကတော့

  • if
  • else
  • elseif/else if
  • while
  • do-while
  • for
  • foreach
  • break
  • continue
  • switch
  • return
  • require
  • include
  • require_once
  • include_once
  • goto

if, else, and else if

if statement က အခြေခံအကျဆုံး statement ဖြစ်ပါတယ်။ Condition တွေကို စစ်တဲ့ Control Structure တစ်ခုလည်း ဖြစ်ပါတယ်။ အခြား Comparison Operators တွေနဲ့ တွဲစပ်သုံးရပါတယ်။

<?php
	$foo = 5;

	if($foo < 10) {
		echo "The condition was met. <br />";
	}

?>

ဒီ Program မှာ If Condition နဲ့ မကိုက်ရင် ဘာ ရလဒ်မှ ထွက်လာမှမဟုတ်ပါဘူး။ အဲဒီလို အခြေအနေမျိုး မှာဆိုရင် ဒီလိုမဟုတ်ရင် ဒီလိုဆိုတဲ့ အခြေအနေ တစ်ခု ထပ်လိုလာမှာ ဖြစ်ပါတယ်။ အဲဒီအတွက် else ကို သုံးဖို့ လိုလာပါတယ်။

<?php
	$foo = 15;

	if($foo < 10) {
		echo "The condition was met. <br />";
	} else {
		echo "The condition was not met. <br />";
	}

?>

ပထမဦးဆုံး if မှာ စစ်လို့ အဆင်မပြေတာကို ထပ်ပြီး စစ်ချင်သေးတယ်၊ ပြီးမှာ else နဲ့ ထပ်သုံးမယ် ဆိုရင်

<?php
	$age = 20;

	if($age < 18) {
		echo "Not old enough to vote or drink! <br />";
	} else if($age < 21) {
		echo "Old enough to vote, but not to drink. <br />";
	} else {
		echo "Old enough to vote and drink! <br />";
	}
?>

အဲဒီလို စစ်ထားရင် ဒုတိယ Condition နဲ့ ကိုက်တာကြောင့် Old enough to vote, but not to drink ဆိုပြီး Output ထွက်လာမှာ ဖြစ်ပါတယ်။

while and do-while

while loop ကတော့ Code block တစ်ခုကို အခြေအနေ တစ်ခုမရောက်ခင် အထိ ပတ်ခိုင်းထားလို့ ရပါတယ်။ Condition တစ်ခု TRUE ဖြစ်မှ အပြင်ကို ထွက်မှာ ဖြစ်ပါတယ်။ နမူနာ ရေးကြည့်မယ် ဆိုရင်

<?php
	$i = 0;

	while ($i < 3) {
		echo "Count is at $i. <br />";
		++$i;
	}
?>

အဲဒီ Program ကို Run ကြည့်မယ်ဆိုရင် $i ရဲ့ တန်ဖိုးက 3 ထက်ငယ်နေသ၍ ပတ်မယ် ဆိုတဲ့ အဓိပ္ပါယ်ရပါတယ်။ $i ရဲ့ တန်ဖိုးကို ပေါင်းပေါင်းသွားတာ 3 ရောက်သွားတဲ့ အချိန်မှာ Condition နဲ့ မကိုက်တော့ပါဘူး။ အဲဒါဆို While Loop က ထွက်သွားပါလိမ့်မယ်။ Output ကို ကြည့်မယ် ဆိုရင်

Output

ဒီနေရာမှာ တစ်ခု သတိထားရမှာက Condition အနေနဲ့ အစကတည်းက မကိုက်ရင် While Loop က အလုပ်လုပ်မှာ မဟုတ်ပါဘူး။ ဥပမာ $i ရဲ့ တန်ဖိိုးကို 4 လို့ သတ်မှတ်ထားရင်ပေါ့။

ဒီထက်ပိုကောင်းတဲ့ ဥပမာ တစ်ခုရေးကြည့်ရအောင်

<?php
	$bands = array("Gun and Roses", "Extreme", "Pearl Jam", "Bon Jovi", "Metallica");

	$i = 0;
	$n = count($bands); // Stores the number of values in the array
	while($i < $n) {
		echo $bands[$i], "<br />";
		++$i;
	}
?>

Output ကတော့

While Output

While အနေနဲ့ Condition နဲ့ မကိုက်တဲ့အခါ လုံးဝမလုပ်ဘူးလို့ ဆိုထားပါတယ်။ အကယ်၍ ကိုယ့်အနေနဲ့ တစ်ကြိမ်လောက်တော့ အနည်းဆုံး Run စေချင်တယ်ဆိုရင် Do .. While ဆိုတာကို သုံးရမှာ ဖြစ်ပါတယ်။

<?php

	$i = 10;

	do {
		echo "The count is at $i.\n";

		++$i;
	}while($i<5);

	// Outputs "The count is at 10."
	// even though $i doesn't meet the condition

?>

FOR

for နဲ့ စတဲ့ statement ကတော့ နေရာတကာ သုံးနိုင်တဲ့ Control Statement တစ်ခုဖြစ်ပါတယ်။ သူကလည်း Loop ပဲ ဖြစ်ပြီး expression အနေနဲ့ သုံးခု လက်ခံပါတယ်။

  • expression one အနေနဲ့ ပထမဦးဆုံး Loop အစမှာ စတင် သတ်မှတ်လုပ်ဆောင်ပါတယ်။
  • expression tow အနေနဲ့ ပထမဦးဆုံး Loop မှာ စတင်စစ်ဆေးပြီး အခြေအနေ မှန်ကန်နေတဲ့ အထိ ရှေ့ဆက်သွားပါတယ်။
  • expression three ကတော့ Loop တစ်ခုရဲ့ နောက်ဆုံးမှာ လုပ်ဆောင်ပါတယ်။

ဒါတွေက ရေးပြနေရင် နားလည်ရ ခက်ပါတယ်။ ဒါကြောင့် Program တစ်ခု ရေးကြည့်လိုက်ရအောင်

<?php

	for ($i = 0; $i < 3; $i++) {
		echo "The count is at $i.\n";
	}

	// Outout:
	// The count is at 0.
	// The count is at 1.
	// The count is at 2.

?>

ပထမ တစ်ပတ်မှာ $i ရဲ့ တန်ဖိုးက 0 ပဲ ရှိသေးတယ်။ ဒါပေမယ့် $i က 3 ထက်ငယ်လားလို့တော့ တစ်ခါစစ်ပါတယ်။ အဲဒီမှာကတည်းက မကိုက်ရင် အလုပ်မလုပ် တော့ပါဘူး။ နောက်ဆုံး Condition ဖြစ်တဲ့ $i++ အတွက် ပထမ Loop မှာ အလုပ် လုပ်မှာ မဟုတ်ပါဘူး။ ဒုတိယ တစ်ပတ်ကြမှ ပေါင်းပါလိမ့်မယ်။ နောက်တစ်မျိုး ရေးကြည့်ရအောင်

<?php

	$bands = array("Guns and Roses", "Metallica",
			"Extreme", "God Smack", "Sum41");

	for($i=0, $n=count($bands); $i<$n; ++$i) {
		echo $bands[$i], "<br />";
	}

	// Outputs:
	// Guns and Roses
	// Metallica
	// Extreme
	// God Smack
	// Sum41

?>

foreach

foreach ကတော့ Array တွေအတွက် အထူးလို့ ပြောရမှာ ဖြစ်ပါတယ်။ အပေါ်မှာ ရေးသလို ရေးစရာ မလိုတော့ပဲ လွယ်လွယ်နဲ့ ရေးလို့ ရသွားစေပါတယ်။ နမူနာ ရေးကြည့်ရအောင်

<?php

	$bands = array("Guns and Roses", "Metallica",
			"Extreme", "God Smack", "Sum41");

	foreach($bands as $band){
		echo $band, "<br />";
	}
	// Outputs:
	// Guns and Roses
	// Metallica
	// Extreme
	// God Smack
	// Sum41

?>

အဲဒီလို ရေးလိုက်ခြင်းအားဖြင့် Array တွေကို ကိုင်တွယ်ရတာ အများကြီး သက်သာသွားပါတယ်။ အကယ်၍ Array တွေမှာ ကိုယ်က $key တွေ ထားပြီး ပေးမယ် ဆိုရင်လည်း ရပါတယ်။ ဥပမာ ရေးကြည့်မယ် ဆိုရင်

<?php

	$person = array(
		'name' => 'Jackson',
		'age' => 23,
		'passion' => 'craft beer'
	);

	foreach ($person as $key => $value) {
		echo "His $key is $value. <br />";
	}

?>

Output အနေနဲ့ အောက်မှာ ပြထားတဲ့အတိုင်း ထွက်လာပါလိမ့်မယ်။

Output

အကယ်၍ ကိုယ့်အနေနဲ့ Mulidimensional Arrays တွေအတွက်ဆိုရင် foreach ကို Nested လုပ်ပြီး သုံးနိုင်ပါတယ်။

<?php

	$people = array(
		'Jackson' => array(
			'gender' => 'male',
			'hair' => 'brown'
		),
		'Carly' => array(
			'gender' => 'female',
			'hair' => 'blonde'
		)
	);

	foreach($people as $name => $person) {
		foreach ($person as $key => $value) {
			echo "$name's $key is $value. <br />";
		}
	}

?>

Output အနေနဲ့ ထွက်လာမှာကတော့

Output

ဆက်ပါဦးမယ် …

Facebook comments:

5 Responses

  1. beegran says:

    php ebook လေးများမရှိမှလားမသိဘူး ရှိရင် ကျွန်တော်ကိုလည်းရှယ်ပါဦးနော် beginner level ပါ

    • phpcrazy says:

      အခြေခံအဆင့်အတွက် ကျွန်တော်အကြိုက်ဆုံးကတော့ PHP for absolute beginner ပဲဗျ။ ကျွန်တော်ရေးတော့လည်း အဲဒီအထဲက ကြည့်ရေးတာပဲ

      • Goo Goo says:

        ဖတ်ဖူးသမျှထဲတော့ PHP for absolute beginner က အကောင်းဆုံးပဲ.. update လဲဖြစ်တယ်.. database access ကို PDO သုံးပြထားတယ်.. :) programming knowledge ရှိပီးသား၊ တခြား language ကနေ ပြောင်းလေ့လာမယ့် သူဆို ပိုအဆင်ပြေလောက်တယ်.. programming ကို အခုမှ စမယ့်သူဆိုရင်တော့ နည်းနည်း အခက်အခဲ ရှိလိမ့်မယ်ထင်တယ်ဗျ..

  2. [...] ဖြစ်ပါတယ်။ ပထမပိုင်းကတော့ ဒီမှာ ပါ။ Control Structure တွေကို သေသေချာချာ [...]

  3. သိချင်တာလေးရှိလို ့ပါ။ ဘာsoftware ကိုသုံးတာလဲ ။ Wampserver လား။ text editor က ဘာသုံးတာ လဲသိ၀ူး။ notepad++ အပြင်ဘာတွေ ရှိသေးသလဲ။ browser မှာပြထားတဲ့ Software လေးကို သဘောကျလို့ပါ.

Leave a comment


*