0Pricing
PHP Academy · Lesson

if, elseif, and else

Make decisions in PHP with conditional branching.

if, elseif, and else is a free PHP Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the PHP Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Conditional Logic in PHP

Conditional statements let your program make decisions. PHP provides if, elseif, and else for branching logic.

The condition inside if () is evaluated as a boolean — truthy proceeds into the block, falsy skips it.

Basic if Statement

The simplest conditional — run code only when a condition is true:

<?php
$age = 20;

if ($age >= 18) {
    echo 'Adult';
}
// Output: Adult

if / else

Add an else branch for the case when the condition is false:

<?php
$score = 45;

if ($score >= 50) {
    echo 'Pass';
} else {
    echo 'Fail';  // runs
}

elseif Chains

Test multiple conditions in sequence with elseif:

<?php
$grade = 78;

if ($grade >= 90) {
    echo 'A';
} elseif ($grade >= 80) {
    echo 'B';
} elseif ($grade >= 70) {
    echo 'C';  // runs
} else {
    echo 'F';
}

Comparison Operators

Operators used in conditions:

  • == equal (loose)
  • === identical (strict)
  • != not equal
  • < > less/greater than
  • <= >= less/greater or equal

Prefer === to avoid type-juggling surprises.

Logical Operators

Combine conditions with logical operators:

<?php
$age  = 25;
$member = true;

if ($age >= 18 && $member) {
    echo 'Access granted';  // both true
}

if ($age < 18 || $member) {
    echo 'Also granted';    // one is true
}

if (!$member) {
    echo 'Not a member';
}

Ternary Operator

The ternary operator is a compact one-liner for simple if/else:

<?php
$age  = 20;
$status = ($age >= 18) ? 'adult' : 'minor';
echo $status;  // adult

// Null coalescing operator ?? (PHP 7+)
$name = $_GET['name'] ?? 'Guest';
echo $name; // 'Guest' if name not in URL

Null Coalescing Assignment

PHP 7.4 added ??= to assign a default only when the variable is null or unset:

<?php
$config = [];

$config['debug'] ??= false;
$config['timeout'] ??= 30;

var_dump($config);
// ['debug' => false, 'timeout' => 30]

Alternative if Syntax

PHP supports an alternative colon syntax for use inside HTML templates:

<?php $loggedIn = true; ?>

<?php if ($loggedIn): ?>
    <p>Welcome back!</p>
<?php else: ?>
    <p>Please log in.</p>
<?php endif; ?>

Spaceship Operator

The spaceship operator <=> compares two values and returns -1, 0, or 1 — perfect for sorting:

<?php
echo 1 <=> 2;  // -1 (left is less)
echo 2 <=> 2;  //  0 (equal)
echo 3 <=> 2;  //  1 (left is greater)

$nums = [3, 1, 4, 1, 5];
usort($nums, fn($a, $b) => $a <=> $b);
print_r($nums); // [1, 1, 3, 4, 5]

match vs if

PHP 8 match is a stricter alternative to long if/elseif chains — you'll learn it in the next lesson. Key difference from if: match uses strict comparison and has no type juggling.

Quick Check

What does the ?? operator do?

Recap: if/elseif/else

Key points:

  • Use if, elseif, else to branch logic
  • Prefer === over == to avoid juggling
  • Ternary ?: for compact single-expression branches
  • ?? for null coalescing defaults
  • Alternative colon syntax for HTML templates

Next: switch and match statements.

Frequently asked questions

Is the “if, elseif, and else” lesson free?

Yes — the full text of “if, elseif, and else” is free to read here on the web, and the PHP Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the PHP Academy course, upgrade to CoddyKit PRO.

What will I learn in “if, elseif, and else”?

Make decisions in PHP with conditional branching. You practise PHP Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start PHP Academy?

No prior experience is required. PHP Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “if, elseif, and else” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this PHP Academy lesson?

Yes. Every PHP Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. if, elseif, and else
  2. Switch and Match Statements
  3. for and while Loops
  4. foreach and Loop Control
← Back to PHP Academy