PHP if else for conditional logic and branching

PHP if else evaluates a boolean expression and runs one of two or more code blocks depending on the result. Use elseif to chain additional conditions when outcomes are mutually exclusive and order-dependent. Only the first matching branch executes, so place the most restrictive condition first. This makes if/elseif/else the default control structure for any branching logic that depends on range checks, compound expressions, or mixed types.

PHP If Else Example For Conditional Branching

Output:

Output will appear here...

Output:

Grade: B

How This Example Works

  1. $score holds an integer value of 82.
  2. The if condition $score >= 90 evaluates to false, so PHP skips that block.
  3. The elseif condition $score >= 80 evaluates to true, so PHP runs the second block and prints Grade: B.
  4. The else block never runs because a previous branch already matched. PHP stops evaluating the chain after the first truthy condition.

What Is elseif in PHP?

PHP elseif adds a new condition to an existing if block. PHP evaluates each elseif in order and executes the first one that is true, then skips the rest of the chain. You can stack as many elseif branches as needed, though more than three or four usually signals that match or a lookup array would be cleaner. In brace syntax, else if (two words) and elseif (one word) behave identically, but in alternative syntax (if: ... endif;) only the single-word elseif is valid.

Common Mistakes With PHP If Else

Using = instead of == or === in conditions

Wrong:

if ($role = "admin") { // assigns "admin" to $role, always truthy
    echo "Access granted";
}

Right:

if ($role === "admin") {
    echo "Access granted";
}

Assignment inside if changes the variable and returns the assigned value, which can make the branch run unconditionally.

Relying on indentation instead of braces (dangling else)

Wrong:

if ($isAdmin)
    if ($isActive)
        echo "ok";
    else
        echo "not admin"; // attaches to the inner if

Right:

if (!$isAdmin) {
    echo "not admin";
} elseif ($isActive) {
    echo "ok";
}

Without braces, else binds to the nearest unmatched if. Use braces or restructure the logic into one if/elseif/else chain.

PHP If/Elseif vs Switch vs Match

Featureif/elseifswitchmatch (PHP 8+)
Comparison typeAny expressionLoose (==)Strict (===)
Returns a valueNoNoYes
Fall-throughN/AYes, unless breakNo
Best forRange checks, compound conditionsMany discrete values with shared logicStrict value mapping to a result

Use if/elseif when conditions involve ranges, compound logic, or function calls. Switch to match when mapping discrete values to results with strict comparison. Use switch when you need fall-through or statement blocks per case.

More Examples

Guard clause to reduce nesting:

function process($input) {
    if (!is_string($input)) {
        return "invalid type";
    }
    if ($input === "") {
        return "empty string";
    }
    return strtoupper($input);
}

Early returns replace nested if/else trees, keeping the main logic at the top indentation level.

Ternary shorthand for simple two-branch assignments:

$label = ($count > 0) ? "has items" : "empty";

When the branch produces a single value, the ternary operator ? : replaces a full if/else block in one expression.

FAQ

What is the difference between elseif and else if in PHP?

In brace syntax ({ }), both are equivalent — PHP treats else if as an else followed by a nested if. In alternative syntax (if: ... endif;), only the single-word elseif compiles. Stick with elseif everywhere to avoid syntax errors when refactoring between styles.

Should PHP conditions use == or ===?

== performs type juggling: 0 == "foo" is true in PHP 7 (changed to false in PHP 8 for non-numeric strings). === compares both value and type with no coercion. Prefer === for predictable results, especially with user input or mixed-type data.

Can you have multiple elseif branches in PHP?

Yes. PHP allows unlimited elseif branches in a single chain. Each condition is checked in order, and only the first truthy branch runs. If none match and an else exists, it acts as the default. When the list grows beyond four or five branches, match or a lookup array is more readable.