include Vs require
Last Updated on Saturday, 15 November 2008 16:50 Written by Zack Saturday, 01 November 2008 17:52
တကယ္ေတာ့ include နဲ႔ require ကသေဘာတရားျခင္း အတူတူပါပဲ။
ဘာလို႔ သုံးလဲဆိုေတာ့ ကြၽန္ေတာ္တို႔ program ေရးရင္ ထပ္ခါထပ္ခါျပန္သုံးရမယ့္ function ေတြရွိတတ္ပါတယ္။ဒါမ်ိဳးေတြကို တျခား php
file တခုအေနနဲ႔ ထုတ္ျပီး ေရးရင္ပိုျပီး ေကာင္းပါတယ္။ ျပီးေတာ့မွ အဲဒီ file ကို include (or) require တို႔နဲ႔ ျပန္ေခၚျပီး အထဲမွာ ေရးထားသမ်
function ေတြအားလုံးကို
ျပန္သုံးလို႔ရပါတယ္။
ဒါကေတာ့ ဘာေၾကာင့္သုံးရလဲဆိုတာျဖစ္ပါတယ္။ ဘယ္လိုကြာျခားတယ္ဆိုတာဆက္ေျပာပါမယ္။
ဥပမာ
myfunction.php ကို ဒီလိုမ်ိဳးျပန္ေခၚျပီး သုံးလို႔ရပါတယ္။
ဒါကေတာ့ include ကိုသုံးျပီး ျပန္ေခၚထားတာျဖစ္ပါတယ္။
<html>
<body>
<?php
include("myfunction.php");
echo "Hello World!";
?>
</body>
</html>
myfunction.php က path မွား ေနလို႔ပဲ ျဖစ္ျဖစ္၊ တျခား error တခုခုရွိရင္ပဲျဖစျ္ဖစ္ ..... error ကို warning ျပျပီး code ကိုဆက္ျပီး execute လုပ္သြားပါလိမ့္မယ္။ ေအာက္ကျပထားသလိုမ်ိဳးပါ။
failed to open stream: No such file or directory in C:\home\website\test.php on line 5Warning: include() [function.include]: Failed opening 'myfunction.php' for inclusion (include_path='.;C:\php5\pear') in C:\home\website\test.php on line 5Hello World!
ဒါကေတာ့ require သုံးထားတဲ့ code ေလးပါ။
<html> <body> <?php require("myfunction.php"); echo "Hello World!"; ?> </body> </html>
myfunction.php က path မွား ေနလို႔ပဲ ျဖစ္ျဖစ္၊ တျခား error တခုခုရွိရင္ပဲျဖစျ္ဖစ္ ဆက္ျပီး execute မလုပ္ပဲ ဒီလိုမ်ိဳး warning နဲ႔ fatal error ကိုျပပါလိမ့္မယ္။
Warning: require(myfunction.php) [function.require]: failed to open stream: No such file or directory in C:\home\website\test.php on line 5Fatal error: require() [function.require]: Failed opening required 'myfunction.php' (include_path='.;C:\php5\pear') in C:\home\website\test.php on line 5
အဓိကေတာ့ error ရွိလည္း ဆက္ Run ျခင္တယ္ဆိုရင္ include ကိုသုံးပါ။မဟုတ္ရင္ေတာ့ require ကိုပဲသုံးေပါ့။ ဒါကေတာ့ include နဲ႔ require ရဲ႕ ကြာျခားခ်က္ပဲျဖစ္ပါတယ္။
တျခား PHP Post ေတြအားလုံး ကို ႏွစ္သက္ရာ ေရြးဖတ္ ခ်င္တယ္ဆိုရင္ ဒီ Link ေလးကို Click လိုက္ပါ။
| < Prev | Next > |
|---|
Login Form
Latest Post
Categories Table View
- Reader's Conner (133)
- PHP (48)
- Joomla CMS (46)
- Codeigniter (18)
- jQuery (12)
- iDhamma (11)
- Mobile Development (10)
- PHP & AJAX (4)
- Apache (3)
- For Mac (3)
- mySQL (2)
- DhammaDroid (1)
ေက်းဇူးပါ) 17;ဲ အကိုေရ
အခုလိုေ၀မွ ်ေပးတဲ့အတြ က္
ေနာက္လည္း ဒီထက္ပို ေ၀မွ်ႏုိင္ ပါေစ