PHP If...Else Statements: Conditional Logic, Branching & Ternary Operator

In computer programming, conditional statements allow your web scripts to perform different actions based on different conditions or user inputs. Without conditional logic, a program would simply execute blindly from top to bottom. The if...else statement is the most foundational decision-making construct in PHP, enabling dynamic user authentication, form validation, access permission checks, and grade calculation.

Basic Syntax of if and if...else

An if block evaluates an expression inside parentheses. If the expression resolves to true, the enclosed block of code executes:

<?php
    $userAge = 19;

    // Simple if statement
    if ($userAge >= 18) {
        echo "Eligibility Confirmed: You are eligible to vote.<br>";
    }

    // if...else statement
    $marks = 45;
    if ($marks >= 50) {
        echo "Congratulations! You passed the semester examination.";
    } else {
        echo "Status: Supplementary examination required.";
    }
?>

Nested if...else Statements

You can place an if block inside another if block to verify multiple hierarchical conditions:

<?php
    $hasAccount = true;
    $isEmailVerified = true;
    $isSubscriptionActive = false;

    if ($hasAccount) {
        if ($isEmailVerified) {
            if ($isSubscriptionActive) {
                echo "Access Granted: Welcome to Premium Video Lessons!";
            } else {
                echo "Notice: Please renew your active subscription to proceed.";
            }
        } else {
            echo "Security Alert: Please verify your registered email address.";
        }
    } else {
        echo "User Not Found: Please register a new student account.";
    }
?>

The Ternary Operator: Shorthand if...else

For concise inline decisions (such as displaying user status in HTML tables), PHP provides the Ternary Operator (?:):

<?php
    // Syntax: (condition) ? value_if_true : value_if_false;
    $isLoggedIn = true;
    $dashboardTitle = $isLoggedIn ? "Welcome back, Student!" : "Please Sign In";

    echo "<h3>" . $dashboardTitle . "</h3>";
?>

Alternative Syntax for Templating: if ... endif;

When embedding PHP conditional blocks directly inside complex HTML files, PHP offers an alternative colon-based syntax that improves visual readability by removing confusing closing curly braces:

<?php $isAdmin = true; ?>

<?php if ($isAdmin): ?>
    <div class="admin-panel" style="background:#e0f2fe; padding:10px;">
        <h4>Administrator Controls</h4>
        <button>Manage Database</button>
    </div>
<?php else: ?>
    <p>Standard student profile view.</p>
<?php endif; ?>

BCA / CS University Exam Corner

High-Frequency Exam Question:

Q: What will be the output of if ("0") { echo "A"; } else { echo "B"; } in PHP?
A: Output will be B. In PHP's type casting rules, the string "0" is considered a falsy value. Therefore, the condition evaluates to false and the else branch is executed.

Interactive Student Practice Session

Exercise 12.1: Write a script that accepts a year (e.g. $year = 2024) and uses an if-else condition to determine whether it is a Leap Year.

Exercise 12.2: Write a script that checks a student's marks and attendance percentage. The student can sit for the exam only if attendance is >= 75% AND fees are cleared.

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Stumbleupon Share on Tumblr Share on Reddit Share on Diggit

You may also like this!