🏠 Home / Hub

🐘 PHP Lesson 02 — Conditions

← Back to PHP Menu

1. if / elseif / else

<?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
// >  <  >=  <=

?>
Output: B grade
⚠️ PHP မှာ elseif ကို else if (space ပါ) လည်း ရေးနိုင်တယ် — နှစ်မျိုးလုံး OK

2. Comparison Operators

OperatorMeaningExampleResult
==Equal (loose)5 == "5"true
===Identical (strict)5 === "5"false
!=Not equal5 != 6true
!==Not identical5 !== "5"true
>Greater5 > 3true
<Less3 < 5true
>=Greater or equal5 >= 5true
<=Less or equal4 <= 5true
<=>Spaceship (PHP 7+)5 <=> 31

3. Ternary & Null Coalescing

<?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

?>

4. switch / case

<?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

?>

5. Logical Operators

<?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

?>

6. Truthy & Falsy in PHP

<?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

?>
Form validation pattern:
if (isset($_POST['submit']) && !empty($_POST['name'])) { /* process */ }

7. Real-world: Registration Validation

<?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 →

📌 Study Checklist