<?php $score = 75; if ($score >= 80) { echo "A grade"; } elseif ($score >= 70) { echo "B grade"; // ← ဒါ run မယ် } elseif ($score >= 60) { echo "C grade"; } else { echo "Fail"; } // Comparison operators // == loose equal (type convert) // === strict equal (type + value) // != not equal // !== strict not equal // > < >= <= ?>
elseif ကို else if (space ပါ) လည်း ရေးနိုင်တယ် — နှစ်မျိုးလုံး OK
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | Equal (loose) | 5 == "5" | true |
=== | Identical (strict) | 5 === "5" | false |
!= | Not equal | 5 != 6 | true |
!== | Not identical | 5 !== "5" | true |
> | Greater | 5 > 3 | true |
< | Less | 3 < 5 | true |
>= | Greater or equal | 5 >= 5 | true |
<= | Less or equal | 4 <= 5 | true |
<=> | Spaceship (PHP 7+) | 5 <=> 3 | 1 |
<?php $age = 20; // Ternary $status = $age >= 18 ? "Adult" : "Minor"; echo $status; // Adult // Null Coalescing ?? — for null/undefined check $username = $_GET['user'] ?? "Guest"; // if $_GET['user'] exists → use it // if not → use "Guest" // Elvis operator ?: (short ternary) $name = $_POST['name'] ?: "Unknown"; // if truthy → use left, else → use right // Null coalescing assignment ??= (PHP 7.4+) $config ??= []; // assign only if null ?>
<?php $day = "Monday"; switch ($day) { case "Monday": echo "တနင်္လာ"; break; case "Saturday": case "Sunday": echo "Weekend!"; break; default: echo "Weekday"; } ?>
<?php // match — PHP 8+ (strict, no type coercion) $status = 2; $text = match($status) { 1 => "Active", 2 => "Inactive", // ← match 3 => "Banned", default => "Unknown" }; echo $text; // Inactive ?>
<?php // && (AND) — short-circuit if ($age >= 18 && $hasID) { echo "Access granted"; } // || (OR) if ($isMember || $isPremium) { echo "Can access"; } // ! (NOT) if (!$isLoggedIn) { echo "Please login"; } // PHP also has word operators: and, or, not // but && || have higher precedence — use those ?>
<?php // Falsy values in PHP: false // bool false 0 // integer 0 0.0 // float 0.0 "" // empty string "0" // string "0" ← PHP gotcha! [] // empty array null // null // isset() — is variable set AND not null? isset($var) // true if defined and not null // empty() — is variable empty/falsy? empty($var) // true if "", 0, [], null, false empty($_POST['name']) // safe check for form input ?>
if (isset($_POST['submit']) && !empty($_POST['name'])) { /* process */ }
<?php $errors = []; $name = $_POST['name'] ?? ''; $email = $_POST['email'] ?? ''; $age = (int)($_POST['age'] ?? 0); if (empty($name)) { $errors[] = "Name is required"; } elseif (strlen($name) < 2) { $errors[] = "Name too short"; } if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $errors[] = "Invalid email"; } if ($age < 18 || $age > 100) { $errors[] = "Age must be 18-100"; } if (empty($errors)) { echo "✅ Registration successful!"; } else { foreach ($errors as $error) { echo "❌ $error<br>"; } } ?>
← PHP 01 | Next: PHP Lesson 03 → Loops →