Lesson 2 of 8

Types, Conditions & Loops

Learn PHP's basic types, conditions and . Pay attention to the difference between == and , which is a common source of .

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",
};
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
Finished reading?Mark it done to track your progress.

Tip: use the ← and → keys to move between lessons.