← Back to PHP Course | Chapter 4: Control Flow | Lesson 2 of 14

PHP if-else Statement

if-else is a fork in the road with exactly two paths: do this if the question is true, or do that if it is not. One of the two always happens.
Syntax
php
if (condition) {
    // runs when condition is true
} else {
    // runs otherwise
}

Standard If-Else

if/else guarantees exactly one of two blocks runs: the if block when the condition is true, the else block when it's false.

There's no scenario where neither runs, and none where both do — the two branches are mutually exclusive by construction.

Example: Standard If-Else

php
<?php
// Declare `$age`, set to `15`
$age = 15;
// Check whether `$age >= 18`
if ($age >= 18) {
    // Print "Adult" to the output
    echo "Adult";
// Otherwise, run this branch
} else {
    // Print "Minor" to the output
    echo "Minor";
}
?>

Conditional Assignment

Using if/else purely to set a variable's value based on a condition is common, but for the simplest cases — assigning one of two values — the ternary operator ($x = $cond ? $a : $b;) expresses the identical logic in a single line.

Example: Conditional Assignment

php
<?php
// Declare `$cond`, set to `true`
$cond = true;
// Declare `$x`, set to `$cond ? "yes" : "no"`
$x = $cond ? "yes" : "no";
// Print `$x` to the output
echo $x;
?>

Boolean Toggles

Boolean variables pair naturally with if/else to make a condition read like plain English: if ($isPremium) { ... } else { ... } is immediately clear about what's being branched on, compared to a condition buried in a more complex expression.

Example: Boolean Toggles

php
<?php
// Declare `$isPremium`, set to `false`
$isPremium = false;
// Check whether `$isPremium`
if ($isPremium) {
    // Print "Premium features unlocked" to the output
    echo "Premium features unlocked";
// Otherwise, run this branch
} else {
    // Print "Upgrade to unlock features" to the output
    echo "Upgrade to unlock features";
}
?>

Alternative Syntax

PHP's alternative syntax replaces the curly braces with a colon after the condition and an endif; to close the block.

It's rarely used in plain PHP files, but it reads far more cleanly than brace-heavy code when it's interleaved directly inside an HTML template.

Example: Alternative Syntax

php
<?php $isAdmin = true; ?>
<?php // Branch which HTML block gets output, based on $isAdmin ?>
<?php if ($isAdmin): ?>
  <p>Admin panel</p>
<?php else: ?>
  <p>Access denied</p>
<?php endif; ?>

Array Checks

Calling a function like array_key_exists() or isset() inside your if condition — before you try to read a value from the array — avoids the warning PHP throws when you access a key that isn't actually there.

Example: Array Checks

php
<?php
// Declare `$user` as an array: `["name" => "Alice"]`
$user = ["name" => "Alice"];
// Check whether `isset($user['email'])`
if (isset($user['email'])) {
    // Print `$user['email']` to the output
    echo $user['email'];
// Otherwise, run this branch
} else {
    // Print "No email set." to the output
    echo "No email set.";
}
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Placing else on the wrong brace, or writing else if with colon syntax, which causes a parse error (use elseif there).
  2. Using if/else to return a boolean, as in if ($x > 5) { return true; } else { return false; }, when return $x > 5; is enough.
  3. Forgetting braces around multi-statement else bodies, so only the first line belongs to the else.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.