W3docs

else

The "else" keyword is a control structure in PHP that is used in conjunction with the "if" keyword to execute a block of code when the "if" condition is false.

The PHP else Keyword

else is the companion to PHP's if statement. On its own, if only decides whether to run a block of code; pairing it with else lets you provide a fallback that runs whenever the if condition evaluates to false. Together they express a single, clear branch: "do this — otherwise, do that."

This page covers the exact syntax, the difference between else, elseif, and a chain of ifs, the alternative colon syntax used in HTML templates, and the gotchas that trip people up (such as confusing = with ==).

Syntax

An else block can only appear immediately after an if (or after an elseif). It takes no condition of its own:

<?php

if (condition) {
  // runs when condition is truthy
} else {
  // runs when condition is falsy
}

Exactly one of the two blocks runs, never both and never neither. The else branch catches everything the if condition did not — so you don't need to spell out the opposite condition by hand.

A basic example

<?php

$myNumber = 5;

if ($myNumber == 4) {
  echo "Number is 4";
} else {
  echo "Number is not 4";
}

// Output: Number is not 4

Because $myNumber is 5, the condition $myNumber == 4 is false, so control falls through to the else block.

Try it Yourself isn't available for this example.

What counts as "false"?

The else block runs whenever the if condition is falsy — not only when it is literally false. PHP treats the following as falsy: false, 0, 0.0, "0", "" (empty string), null, and the empty array []. Everything else is truthy.

<?php

$cart = [];

if ($cart) {
  echo "You have items in your cart.";
} else {
  echo "Your cart is empty."; // an empty array is falsy
}

// Output: Your cart is empty.

This is handy for "is there anything here?" checks, but be deliberate: "0" and 0 are falsy, so a value you do care about can slip into the else branch. When you need an exact test, compare explicitly (if ($x === 0)) rather than relying on truthiness.

else vs elseif vs separate ifs

A plain if/else has only two outcomes. When you need more than two, reach for elseif instead of stacking independent if statements:

<?php

$score = 72;

if ($score >= 90) {
  echo "Grade: A";
} elseif ($score >= 70) {
  echo "Grade: B";
} else {
  echo "Grade: C or lower";
}

// Output: Grade: B

The chain stops at the first branch that matches, so order matters — put the most specific or highest conditions first. Using two separate if blocks instead would evaluate both conditions independently and could print two grades. See PHP if, else, elseif for a fuller comparison.

Alternative (colon) syntax

When you are mixing PHP with HTML in a template, the curly-brace style can be hard to read. PHP offers an alternative syntax that replaces { with : and closes the block with endif:

<?php $loggedIn = true; ?>

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

Both styles behave identically; the colon form simply keeps templates tidier.

Nesting

An if/else can live inside another else branch. Each else always binds to the nearest unmatched if:

<?php

$age = 17;
$hasGuardian = true;

if ($age >= 18) {
  echo "Admitted.";
} else {
  if ($hasGuardian) {
    echo "Admitted with a guardian.";
  } else {
    echo "Not admitted.";
  }
}

// Output: Admitted with a guardian.

If you find yourself nesting more than a level or two, elseif or a switch statement usually reads better.

Common gotcha: = vs ==

A frequent bug is writing = (assignment) where you meant == (comparison). The line below assigns 4 to $x, and since 4 is truthy, the if branch always runs — the else becomes unreachable:

<?php

$x = 1;

if ($x = 4) {        // assignment, not comparison!
  echo "Always runs";
} else {
  echo "Never runs";
}

// Output: Always runs

Use == for loose comparison or === for strict (type-aware) comparison. The comparison operators page lists them all.

Ternary: a shorthand for simple cases

When both branches just produce a value, the ternary operator ?: is a compact alternative to a full if/else:

<?php

$age = 20;
$status = $age >= 18 ? "adult" : "minor";

echo $status;

// Output: adult

Reserve this for short, single-value decisions — multi-statement logic stays clearer as a real if/else.

Summary

  • else runs only when the preceding if (or elseif) condition is falsy; exactly one branch executes.
  • "Falsy" includes false, 0, "", null, and [] — not just the literal false.
  • Use elseif for more than two outcomes, and the colon/endif syntax inside HTML templates.
  • Watch for = vs ==, and reach for the ternary ?: when you only need to pick a value.

Practice

Practice
What does the 'else' statement indicate in PHP?
What does the 'else' statement indicate in PHP?
Was this page helpful?