PHP: Include & Require Functions

Include Function

PHP အကြောင်း အများကြီး အသေးစိတ် မသိသေး ပေမယ့်လဲ code ရေးရာမှာ အချိန်ကုန် သက်သာဖို့ include, require function တွေကို လေ့လာ ကြည့်ရအောင်။ include function က သူ့ထဲမှာ ထည့်ပေးလိုက်တဲ့ file ထဲမှာ ပါတဲ့ code တွေကို သူ့ကို ခေါ်သုံးထားတဲ့ script file ထဲကို ထည့်ပေး လိုက်တာပါပဲ။

အဲဒါ ဘာလို့ ကောင်းလဲ စဉ်းစား ကြည့်ရအောင်ဗျာ။ Site တစ်ခုမှာ Page တစ်ရာပါတယ် ဆိုပါစို့။ အဲဆိုက်အတွက် Header တွေ Navigation Menu တွေ အတွက် code တွေကို Page တစ်ရာ လုံးမှာ လိုက်ရေးရမယ်ဗျာ။ Copy & Paste လုပ်လို့ ရတယ်ထားဦး၊ code line များရင် အချိန်တော့ ကုန်မှာပဲ။ Menu link တွေ ပြောင်းသွားရင် အကုန် လိုက်ပြင်ရမယ်။ အဲမှာ Header နဲ့ Menu အတွက် code ကို ဖိုင် တစ်ခုထဲမှာ သိမ်းထား၊ အဲဒီဖိုင်ကို Page တိုင်းမှာ include function နဲ့ ပြန်ခေါ်သုံး။ တစ်ကြောင်း ထဲနဲ့ ပီးသွားမယ်။ ကြည့်ရတာလဲ ရှင်းသွားမယ်။ အဲ. Page အသစ်တွေ ထပ်ထည့်မယ်၊ Menu system ပြောင်းမယ်ဆိုလဲ Menu အတွက် ရေးထားတဲ့ code file မှာ သွားပြင်လိုက်ရုံပဲ။ ကဲ. ဘယ်လောက် ကောင်းလဲဗျာ။

Include Example

အခု ကျနော်တို့တွေ Page တိုင်းမှာ သုံးဖို့အတွက် common menu file တစ်ခု ဆောက်ကြမယ်။ include function မှာ ခေါ်သုံးဖို့ ဖိုင်တွေကို နာမည်ပေးရာမှာ “.php” extension ပေးဖို့လဲ အလေ့အကျင့် လုပ်ထားပါ။ ကျနော်တို့ menu လုပ်မှာ ဖြစ်လို့ “menu.php” လို့ ပေးလိုက်ပါမယ်။

menu.php Code:

<html>
<body>
<a href="http://www.example.com/index.php">Home</a> -
<a href="http://www.example.com/about.php">About Us</a> -
<a href="http://www.example.com/links.php">Links</a> -
<a href="http://www.example.com/contact.php">Contact Us</a><br />

menu.php လို့ save လုပ်လိုက်ပါ။ နောက် index.php ကို menu.php ရှိတဲ့ directory/folder ထဲမှာပဲ ဆောက်ပါမယ်။

index.php Code:

<?php include("menu.php"); ?>
<p>This is my home page that uses a common menu to save me time when I add
new pages to my website!</p>
</body>
</html>

Common Menu

Common Menu

ဒီနည်းလမ်း အတိုင်းပဲ “about.php”, “links.php” နဲ့ “contact.php” ဖိုင်တွေ ဆောက်လို့ရပါပီ။ အသစ် ထပ်တိုးရင်လဲ menu.php ဖိုင်ကို သွားပြင် လိုက်ရုံပါပဲ။ အခုလို ထပ်ခါထပ်ခါ လုပ်ရမယ့် အလုပ်တွေအတွက် include function ကို သတိရလိုက်ပါ။ include function သုံးဖို့ လိုမယ့် နေရာတွေကိုလဲ မြင်တတ်အောင် လေ့ကျင့်ထားပါ။

What do Visitors See?

ကျနော်တို့တွေ page တိုင်းမှာ include function သုံးထားတယ်နော်။Visitor တွေက view source လုပ်ကြည့်ရင် ဘယ်လို မင်ရမယ် ထင်လဲ? include function က copy paste လုပ်တာနဲ့ တူတူပါပဲ။ အဲဒါကြောင့် index.php ကို view source လုပ်ကြည့်ရင် အောက်မှာလို မြင်ရပါမယ်။

<html>
<body>
<a href="index.php">Home</a> -
<a href="about.php">About Us</a> -
<a href="links.php">Links</a> -
<a href="contact.php">Contact Us</a> <br />
<p>This is my home page that uses a common menu to save me time when I add
new pages to my website!</p>
</body>
</html>

Require Function

Require Function ကိုလဲ Include Function သုံးတဲ့ အတိုင်း သုံးနိုင်ပါတယ်။ ဒါပေမယ့် သူတို့ နှစ်ခုမှာ ကြီးမားတဲ့ ကွဲပြားချက် တစ်ခုရှိပါတယ်။ အဲဒီ ကွဲပြားချက်ကို ကြည့်ရအောင်။

ကျနော်တို့တွေ ဖိုင်တစ်ခုကို include လုပ်မယ်။ နာမည် မှားသည်ဖြစ်စေ၊ အဲဖိုင် မရှိရင် ဖြစ်စေ၊ လမ်းကြောင်း မှားသည်ဖြစ်စေ အောက်ကလို Error message ပေးပါလိမ့်မယ်။

<?php
include("none.php");
echo "Hello World!";
?>

Include function error

Include function error

အခုလို Error တက်ပေမယ့် echo statement ကို execute လုပ်သွားတာကို သတိထားကြည့်ပါ။ ဘာလို့လဲ ဆိုတော့ Warning error message တွေက PHP script ကို ဆက် မ run ဖို့ တားမပေးပါဖူး။ ဒီပုံစံအတိုင်းပဲ require function ကို သုံးကြည့်ပါမယ်။

<?php
require("none.php");
echo "Hello World!";
?>

Require function error

Require function error

ဒီမှာတော့ echo statement ကို ဆက်ပီး execute မလုပ်တော့ပါဖူး။ require function မှာ ခေါ်ထားတဲ့ file မရှိတဲ့ အတွက် fatal error တက်သွားပါတယ်။ မပါမဖြစ် လိုအပ်တဲ့ဖိုင် မရှိခဲ့ရင် (သို့) နာမည်မှား ပေးခဲ့တာမျိုး ဖြစ်ခဲ့ရင် နောက်က code တွေ ဆက် မ run စေဖို့  include အစား require ကို သုံးပါ။

<html>
<body>
<a href="http://www.example.com/index.php">Home</a> -
<a href="http://www.example.com/about.php">About Us</a> -
<a href="http://www.example.com/links.php">Links</a> -
<a href="http://www.example.com/contact.php">Contact Us</a><br /><p>This is my home page that uses a common menu to save me time when I add
new pages to my website!</p>
</body>
</html>

Facebook comments:

Leave a comment


*