🏠 Home / Hub

🐘 PHP Lesson 01 — Syntax & Variables

← Back to PHP Menu

1. PHP ဆိုတာ ဘာလဲ?

PHP = PHP: Hypertext Preprocessor (recursive!)

Server-side scripting language — web server (Apache, Nginx) မှာ run ပြီး HTML output ထုတ်ပေးတယ်

✅ Free & Open Source
✅ WordPress, Laravel, Drupal မှာ သုံးတယ်
✅ XAMPP, Laragon, WAMP မှာ local server run နိုင်တယ်
⚠️ Browser မှာ တိုက်ရိုက် open မရ — server မှ run ရတယ်
📂 File တွေကို: C:/xampp/htdocs/myfolder/ ထဲ save ပြီး
🌐 Browser မှာ: localhost/myfolder/ ကနေ ဝင်ကြည့်ပါ

2. PHP Tags

<!-- ဒါ HTML -->
<h1>Hello</h1>

<?php
  // ဒါ PHP code
  echo "This is PHP";
?>

<!-- Short echo tag -->
<p><?= $name ?></p>
<!-- same as: <?php echo $name; ?> -->
PHP code ကို <?php နဲ့ ဖွင့်ပြီး ?> နဲ့ ပိတ်ရတယ်
PHP file only (HTML မပါ): closing tag ?> မထည့်ရ (best practice)

3. Variables

<?php

// Variable names always start with $
$name = "Ko Min";      // string
$age = 25;           // integer
$price = 9.99;       // float
$isLoggedIn = true; // boolean
$nothing = null;    // null

// PHP is case-sensitive for variables!
$Name$name

// Variable naming rules
$myVar         // ✅ camelCase
$my_var        // ✅ snake_case (PHP convention)
$_myVar        // ✅ underscore start OK
// $1var = ""   // ❌ cannot start with number

?>

4. echo & print

<?php

$name = "Ko Min";
$age = 25;

// echo — most common way to output
echo "Hello World";
echo $name;
echo "Age: " . $age;       // . = concatenation
echo "Hi, I'm $name";       // Variable inside double quotes
echo "Hi, I'm {$name}!";    // With curly braces

// print — same but returns 1
print "Hello";

// var_dump — debug output (type + value)
var_dump($age);       // int(25)
var_dump($name);      // string(6) "Ko Min"
var_dump(true);        // bool(true)

// print_r — readable output (for arrays)
print_r([1, 2, 3]);

?>
// Example output: Hello World Ko Min Age: 25 Hi, I'm Ko Min Hi, I'm Ko Min! int(25) string(6) "Ko Min" bool(true)

5. PHP Data Types

TypeExampleDescription
string"Hello"Text
integer42Whole number
float3.14Decimal number
booleantrue/falseTrue or false
array[1,2,3]List / map
nullnullNo value
objectnew MyClass()Class instance
<?php
// Type checking
is_string($x)   // true/false
is_int($x)      // true/false
is_float($x)    // true/false
is_bool($x)     // true/false
is_array($x)    // true/false
is_null($x)     // true/false
gettype($x)     // "string", "integer", etc.

// Type casting
(int)"42"       // 42
(string)42      // "42"
(float)"3.14"  // 3.14
(bool)0          // false
?>

6. Constants

<?php

// define() — old way
define("MAX_SIZE", 100);
define("SITE_NAME", "My Website");

// const — modern way (inside class too)
const PI = 3.14159;
const DB_HOST = "localhost";

// Usage — no $ sign!
echo MAX_SIZE;       // 100
echo SITE_NAME;      // My Website
echo PI;             // 3.14159

// PHP Built-in Constants
echo PHP_VERSION;    // "8.2.0" etc
echo PHP_EOL;        // Line break
echo __FILE__;       // Current file path
echo __LINE__;       // Current line number
?>

7. Comments

<?php

// Single line comment
// This is a comment

// # also works
# Another comment

/*
 * Multi-line comment
 * Use for longer explanations
 */

/**
 * PHPDoc comment
 * @param string $name User's name
 * @return string Greeting message
 */
function greet(string $name): string {
    return "Hello, $name!";
}
?>

8. ကိုယ်ပိုင် PHP File ဆောက်ပါ

📁 C:/xampp/htdocs/learn/ folder ထဲမှာ hello.php file ဆောက်ပြီး ဒါ ရိုက်ပါ:
<?php
$name = "ကလေး";
$year = date("Y");  // current year
?>
<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>My PHP Page</title>
</head>
<body>
  <h1>မင်္ဂလာပါ, <?= $name ?>!</h1>
  <p>ဒီနှစ်က <?= $year ?> ဖြစ်တယ်</p>
  <p><?php echo "PHP Version: " . PHP_VERSION; ?></p>
</body>
</html>
Browser output: မင်္ဂလာပါ, ကလေး! ဒီနှစ်က 2026 ဖြစ်တယ် PHP Version: 8.2.x

Next: PHP Lesson 02 → Conditions →

📌 Study Checklist