PhPHP · Lesson 2 of 8

Types, Conditions & Loops

PHP is dynamically typed but has grown a real type system. Learn the comparison gotcha (== vs ===) early — it's the classic PHP interview question for a reason.

PHP
<?php
$int    = 42;
$float  = 3.14;
$string = "hello";
$bool   = true;
$null   = null;

var_dump($int);     // int(42) — var_dump shows type + value
gettype($float);    // "double"

// Conditions:
$grade = 87;
if ($grade >= 90) {
    echo "A";
} elseif ($grade >= 80) {
    echo "B";
} else {
    echo "C";
}

// match (PHP 8) — like switch but an expression, no fallthrough:
$label = match(true) {
    $grade >= 90 => "excellent",
    $grade >= 80 => "good",
    default      => "keep going",
};
⚠ Warning
== compares after type juggling: 0 == 'a' was true in old PHP, '1' == '01' is still true. === compares value AND type with no conversions. Use === and !== always; reserve == for the rare case you truly want coercion.
PHP
<?php
for ($i = 0; $i < 5; $i++) {
    echo $i;
}

$count = 0;
while ($count < 3) {
    $count++;
}

// foreach is the loop you'll use most (arrays, next lesson):
foreach ([1, 2, 3] as $n) {
    echo $n * 2;      // 2 4 6
}

// Ternary and null coalescing:
$status = $age >= 18 ? "adult" : "minor";
$name   = $input ?? "anonymous";   // default if null/unset