ELSEIF
Your brief EB
183 stories from 71 feeds 32 clusters Refreshed 8 minutes ago next pull 13:20

PHP 8 · Beginner

PHP if, elseif, and else: syntax that stays readable

See how PHP selects a conditional branch, whether elseif and else if differ, and how to avoid loose-comparison and template syntax traps.

PHP uses if, elseif, and else to express one ordered decision. It evaluates conditions from top to bottom and runs the first matching branch.

<?php

$statusCode = 404;

if ($statusCode >= 500) {
    $message = 'Server error';
} elseif ($statusCode >= 400) {
    $message = 'Request error';
} else {
    $message = 'Request completed';
}

echo $message; // Request error

Once $statusCode >= 400 matches, PHP skips the else. A chain produces at most one branch.

Is it elseif or else if in PHP?

With curly braces, both spellings work:

if ($role === 'admin') {
    grantAdminAccess();
} elseif ($role === 'editor') {
    grantEditorAccess();
}
if ($role === 'admin') {
    grantAdminAccess();
} else if ($role === 'editor') {
    grantEditorAccess();
}

The first form is a single elseif token. The second is technically an else containing another if. They behave the same in this braced example.

Pick one style and use it consistently. elseif is conventional in PHP and has one important advantage in template-style syntax.

Alternative syntax requires elseif

PHP offers colon syntax for templates:

<?php if ($user->isAdmin()): ?>
    <a href="/admin">Admin</a>
<?php elseif ($user->canEdit()): ?>
    <a href="/edit">Edit</a>
<?php else: ?>
    <span>Read only</span>
<?php endif; ?>

In this form, write elseif as one word. else if (...): does not fit the alternative syntax grammar and triggers a parse error.

Framework templates such as Blade provide their own directives, but the underlying decision model is the same:

@if ($user->isAdmin())
    <a href="/admin">Admin</a>
@elseif ($user->canEdit())
    <a href="/edit">Edit</a>
@else
    <span>Read only</span>
@endif

Order specific cases first

Overlapping conditions make order meaningful:

function ticketPrice(int $age): int
{
    if ($age >= 18) {
        return 25;
    } elseif ($age >= 65) {
        return 18;
    }

    return 15;
}

The senior branch never runs. Everyone aged 65 or older already matched $age >= 18.

function ticketPrice(int $age): int
{
    if ($age >= 65) {
        return 18;
    } elseif ($age >= 18) {
        return 25;
    }

    return 15;
}

This version tests the narrower group first.

Prefer strict comparisons

PHP’s loose equality operator converts types. That can make a branch accept values its author did not intend:

$submitted = '0';

if ($submitted == 0) {
    // true after type juggling
}

Use === or !== when type is part of the contract:

if ($submitted === 0) {
    // only the integer zero
} elseif ($submitted === '0') {
    // only the string "0"
}

For ordering comparisons such as $score >= 80, typed function parameters and validated input keep the comparison predictable.

empty() can erase meaningful values

The convenience function empty() treats several values as empty, including 0 and the string '0':

if (empty($quantity)) {
    echo 'No quantity supplied';
}

That message appears for a legitimate zero quantity. Test the actual missing states your application expects:

if ($quantity === null) {
    echo 'No quantity supplied';
} elseif ($quantity === 0) {
    echo 'Quantity is zero';
}

Broad convenience checks are safe only when all the values they group together mean the same thing to your program.

Use early returns for validation

When every branch exits, else adds indentation without adding information:

function updateProfile(?User $user, array $data): bool
{
    if ($user === null) {
        return false;
    }

    if (! $user->canUpdateProfile()) {
        return false;
    }

    return $user->update($data);
}

The successful path is visible at the base indentation level. This is usually easier to scan than nesting the update inside two positive checks.

When to use match

PHP 8’s match expression is a good alternative when one value maps to one result. It compares strictly, returns a value, and does not fall through:

$label = match ($status) {
    'draft' => 'Keep working',
    'review' => 'Waiting for review',
    'published' => 'Live',
    default => 'Unknown status',
};

Keep if / elseif for ranges, compound boolean expressions, and conditions involving different values.

See the PHP manual entry for elseif for the formal syntax and alternative-style warning.

How this was checked

Examples were executed with PHP 8.4.4. The alternative-syntax distinction was checked against the PHP manual's elseif control-structure documentation.