php

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
  1. User input များကို အမြဲတမ်း validate နှင့် sanitize လုပ်ပါ
  2. Database queries များအတွက် prepared statements သုံးပါ
  3. Error messages များကို production environment တွင်မပြပါနှင့်
  4. Password များကို hash လုပ်ပါ (password_hash() သုံးပါ)
  5. HTTPS ကိုအသုံးပြုပါ
PHP Frameworks များ
  1. Laravel
  2. Symfony
  3. CodeIgniter
  4. CakePHP
  5. Yii

PHP သည် web development အတွက်အလွန်ကောင်းမွန်သော language တစ်ခုဖြစ်ပြီး၊ လွယ်ကူစွာလေ့လာနိုင်ပါသည်။ ဤအခြေခံများကိုသိပြီးပါက PHP ၏ advanced topics များကိုဆက်လက်လေ့လာနိုင်ပါသည်။

Similar Posts

  • PHP ဆိုတာဘာလဲ?

    PHP (Hypertext Preprocessor) သည် server-side scripting language တစ်ခုဖြစ်ပြီး dynamic website များနှင့် web applications များကိုတည်ဆောက်ရန်အတွက်အသုံးပြုပါသည်။ PHP သည် open-source language တစ်ခုဖြစ်ပြီး၊ MySQL database နှင့်အတူအလွန်ကောင်းစွာအလုပ်လုပ်နိုင်ပါသည်။ PHP အတွက်လိုအပ်သော Server Requirements 1. Local Development အတွက် 2. Web Hosting အတွက် PHP Syntax အခြေခံများ PHP script များကို <?php နှင့် ?> tag များကြားတွင်ရေးသားပါသည်။ PHP တွင် Comment ရေးသားနည်း 1. Basic Hello World 2. HTML…