Skip to content
Home Tutorials Roadmaps Courses
Log in Join free
Tutorials PHP Conditionals
PHP Intermediate FREE

Conditionals

Lesson 1 of 17 Intermediate Interactive

Conditional statements allow your PHP code to make decisions based on conditions. PHP supports several ways to control program flow.

if / elseif / else

The most common way to execute code based on a condition.

switch Statement

Used when comparing a single variable against many possible values.

match Expression (PHP 8+)

A newer, cleaner alternative to switch that uses strict comparison and returns a value.

Ternary Operator

A shorthand for simple if/else: $result = condition ? "yes" : "no";

Null Coalescing Operator (??)

Returns the left operand if it exists and is not null: $name = $name ?? "Guest";

Syntax

PHP
<?php
// if / elseif / else

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

// switch

switch ($day) {
    case "Monday":
        echo "Start of week";
        break;
    default:
        echo "Another day";
}

// match (PHP 8+)

$status = match($code) {
    200 => "OK",
    404 => "Not Found",
    500 => "Server Error",
    default => "Unknown",
};

// Ternary

echo $age >= 18 ? "Adult" : "Minor";

// Null coalescing

echo $name ?? "Guest";
?>
PHP Conditionals
PHP
<?php
$score = 85;

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

// Match expression (PHP 8)
$result = match(true) {
    $score >= 90 => "Excellent",
    $score >= 70 => "Good",
    default => "Needs work",
};
echo "Result: $result
";
?>

Practice

1
Exercise

Write a conditional that checks if a user is logged in ($isLoggedIn) and displays different messages.

Answer
<?php
$isLoggedIn = true;
if ($isLoggedIn) {
    echo "Welcome back, Sukhwinder!";
} else {
    echo "Please log in to continue.";
}
?>
2
Exercise

Use the null coalescing operator to set a default value for a potentially undefined variable.

Answer
<?php
$color = null;
$theme = $color ?? "light";
echo "Theme: $theme"; // Theme: light
?>

Quick Quiz

1

What is the null coalescing operator in PHP?

The ?? operator returns the left operand if it exists and is not null, otherwise it returns the right operand.

2

How is match() different from switch?

match uses strict (===) comparison, always returns a value, and does not need break statements.

3

What does the ternary operator do?

The ternary operator condition ? "yes" : "no" is a shorthand for simple if/else statements.

Interview Questions

Use match() when working with PHP 8+ because it provides strict comparison (===), returns a value directly, doesn't need break statements, and is more concise. switch() still works but allows loose comparison and requires explicit break to avoid fall-through.

== performs loose comparison with type juggling (e.g., 0 == "hello" is true). === performs strict comparison checking both value and type (e.g., 0 === "hello" is false). Always prefer === to avoid unexpected type coercion bugs.