else if vs elseif in PHP — What's the Difference and Which Should You Use?

If you've been writing PHP for a while, you've probably used both else if (two words) and elseif (one word) without thinking much about it. They look almost identical, they behave almost identically, and PHP accepts both without complaint. So what's actually going on under the hood?

Let's break it down.


The Basics First

Here's the classic conditional structure most developers learn on day one:

$score = 75;

if ($score >= 90) {
    echo "Grade: A";
} elseif ($score >= 75) {
    echo "Grade: B";
} elseif ($score >= 60) {
    echo "Grade: C";
} else {
    echo "Grade: F";
}

Clean, readable, and works perfectly. Now swap every elseif with else if — it still works. So why does PHP have two ways to write the same thing?


They Are NOT Always the Same

Here's the critical part most tutorials skip.

With Curly Braces {} — Both Work Identically

if ($x > 10) {
    echo "big";
} else if ($x > 5) {
    echo "medium";
} else {
    echo "small";
}
if ($x > 10) {
    echo "big";
} elseif ($x > 5) {
    echo "medium";
} else {
    echo "small";
}

These two blocks compile to the same bytecode. Zero difference.

With Colon Syntax : — Only elseif is Valid

PHP supports an alternative syntax for control structures, commonly used inside HTML templates:

<?php if ($isLoggedIn): ?>
    <p>Welcome back!</p>
<?php elseif ($isGuest): ?>
    <p>Hello, guest!</p>
<?php else: ?>
    <p>Please log in.</p>
<?php endif; ?>

Now try using else if here:

<?php if ($isLoggedIn): ?>
    <p>Welcome back!</p>
<?php else if ($isGuest): ?>   <!-- Parse error! -->
    <p>Hello, guest!</p>
<?php endif; ?>

PHP throws a parse error. This is because else if in the colon syntax creates a nested if inside else, breaking the endif pairing. The parser no longer knows which if the endif closes.

This is the one real, concrete difference between the two.


What's Happening Internally

When you write else if (two words) with curly braces, PHP actually parses it as a nested structure:

// What you write
} else if ($condition) {

// What PHP actually sees
} else {
    if ($condition) {
    }
}

It's else followed by a new if block nested inside it. PHP is smart enough to flatten this in the compiled output when you're using curly braces, so performance is identical. But with colon syntax, that nesting breaks the template structure — which is why else if is forbidden there.

elseif (one word), on the other hand, is a dedicated keyword. PHP treats it as a single, flat continuation of the conditional chain — no hidden nesting involved.


Performance — Is There Any Difference?

Short answer: No.

With curly braces, both forms produce identical opcodes. PHP's compiler optimizes else if into the same flat structure as elseif. You will never notice a performance difference in any real application.


So Which One Should You Use?

Follow this simple rule:

Situation Use
Standard curly brace blocks Either works — pick one and be consistent
Alternative colon/template syntax Always use elseif
Team or framework codebase Follow the existing style guide

PSR-12 and Community Convention

The PHP-FIG's PSR-12 coding standard — the most widely adopted PHP style guide — explicitly recommends elseif over else if:

"The keyword elseif SHOULD be used instead of else if so that all control keywords look like single words."

Laravel, Symfony, and most major PHP frameworks follow PSR-12. If you're writing professional PHP, elseif is the right default.


Quick Reference

// Recommended — works everywhere
if ($a) {
    // ...
} elseif ($b) {
    // ...
} else {
    // ...
}

// Also valid — but only with curly braces
if ($a) {
    // ...
} else if ($b) {
    // ...
} else {
    // ...
}

// Template syntax — elseif ONLY, else if will throw a parse error
<?php if ($a): ?>
    ...
<?php elseif ($b): ?>
    ...
<?php else: ?>
    ...
<?php endif; ?>

The Bottom Line

elseif and else if are functionally identical in curly brace contexts, but elseif is the only valid option in PHP's alternative colon syntax. Beyond correctness, elseif is what PSR-12 recommends, what major frameworks enforce, and what makes your code look intentional and professional.

Pick elseif. Use it everywhere. Move on to harder problems.

No comments:

Post a Comment

Running PHP in the Terminal — No Browser Required

Most developers discover PHP through XAMPP, a local server, or a tutorial that immediately sets up Apache and a browser preview. So it's...