PHP ၏အခြေခံ Features များ
1. Variables
PHP တွင် variable များကို $ သင်္ကေတဖြင့်စတင်ပါတယ်။
<?php
$name = "John Doe";
$age = 25;
$price = 9.99;
$is_active = true;
?>
2. Data Types
PHP တွင် data type အမျိုးအစားများ:
- String
- Integer
- Float (double)
- Boolean
- Array
- Object
- NULL
- Resource
3. Operators
- Arithmetic operators (
+,-,*,/,%) - Assignment operators (
=,+=,-=, etc.) - Comparison operators (
==,===,!=,>,<, etc.) - Logical operators (
&&,||,!)
4. Conditional Statements
<?php
$age = 18;
if ($age >= 18) {
echo "You are an adult.";
} elseif ($age >= 13) {
echo "You are a teenager.";
} else {
echo "You are a child.";
}
?>
5. Loops
<?php
// For loop
for ($i = 0; $i < 5; $i++) {
echo $i . "<br>";
}
// While loop
$j = 0;
while ($j < 5) {
echo $j . "<br>";
$j++;
}
// Foreach loop (for arrays)
$colors = array("red", "green", "blue");
foreach ($colors as $color) {
echo $color . "<br>";
}
?>
6. Functions
<?php
function greet($name) {
return "Hello, " . $name . "!";
}
echo greet("Alice"); // Output: Hello, Alice!
?>
Database (MySQL) နှင့်ချိတ်ဆက်ခြင်း
1. MySQLi အသုံးပြုခြင်း
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
// Perform query
$sql = "SELECT id, name FROM users";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>
2. PDO အသုံးပြုခြင်း
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
// Perform query
$stmt = $conn->prepare("SELECT id, name FROM users");
$stmt->execute();
// set the resulting array to associative
$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);
foreach($stmt->fetchAll() as $row) {
echo "id: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
}
$conn = null;
?>
PHP File Handling
1. File ဖတ်ခြင်း
<?php
$file = fopen("welcome.txt", "r") or die("Unable to open file!");
echo fread($file, filesize("welcome.txt"));
fclose($file);
?>
2. File ရေးခြင်း
<?php
$file = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "Hello World\n";
fwrite($file, $txt);
fclose($file);
?>
Form Handling
HTML Form
<form action="welcome.php" method="post">
Name: <input type="text" name="name"><br>
Email: <input type="text" name="email"><br>
<input type="submit">
</form>
PHP Form Processing (welcome.php)
<?php
$name = $_POST['name'];
$email = $_POST['email'];
echo "Welcome " . $name . "<br>";
echo "Your email is: " . $email;
?>
Security Best Practices
- User input များကို အမြဲတမ်း validate နှင့် sanitize လုပ်ပါ
- Database queries များအတွက် prepared statements သုံးပါ
- Error messages များကို production environment တွင်မပြပါနှင့်
- Password များကို hash လုပ်ပါ (password_hash() သုံးပါ)
- HTTPS ကိုအသုံးပြုပါ
PHP Frameworks များ
- Laravel
- Symfony
- CodeIgniter
- CakePHP
- Yii
PHP သည် web development အတွက်အလွန်ကောင်းမွန်သော language တစ်ခုဖြစ်ပြီး၊ လွယ်ကူစွာလေ့လာနိုင်ပါသည်။ ဤအခြေခံများကိုသိပြီးပါက PHP ၏ advanced topics များကိုဆက်လက်လေ့လာနိုင်ပါသည်။
