Limited Offer
60% OFF on all plans!

Conditionals in PHP

Learn how to control program flow using if/else/elseif, switch statements, and the modern match expression introduced in PHP 8.

if / else

The `if` statement is the most fundamental conditional in PHP. It runs a block of code only when a condition is `true`. Combine it with `else` to handle the false case, or chain multiple conditions with `elseif`.

1. Basic if

Run a block of code only when a condition is truthy.

2. if / else

Use `else` to handle the case when the condition is false.

3. elseif Chain

Test multiple conditions in sequence with `elseif`.

4. Alternative Syntax (Templates)

PHP provides an alternative syntax using `:` and `endif` — common in HTML templates.

Truthy & Falsy Values

PHP automatically converts values to boolean when they are used in a conditional expression. Knowing which values are falsy (treated as `false`) and which are truthy (treated as `true`) is essential for writing correct conditional logic.

1. Falsy Values in PHP

These values evaluate to `false` when used in a boolean context.

2. Truthy Values in PHP

Everything that is not falsy evaluates to `true`.

3. Practical Use in Conditions

Rely on truthy/falsy to write concise, idiomatic PHP.

Ternary & Null Coalescing Shorthand

For simple if/else decisions, PHP offers the ternary operator `?:` and the null coalescing operator `??` (PHP 7.0+) to express conditions in a single line.

1. Ternary Operator

`condition ? valueIfTrue : valueIfFalse`

2. Elvis Operator (Short Ternary)

`$a ?: $b` — returns `$a` if truthy, otherwise `$b`.

3. Null Coalescing (??) — PHP 7.0+

Returns left side if it exists and is not null; otherwise returns right side.

4. Null Coalescing Assignment (??=) — PHP 7.4+

Assign a default value to a variable only if it is currently null or unset.

switch Statement

The `switch` statement compares a single expression against multiple `case` values using loose comparison (`==`). It is useful when you have many possible values to test against.

1. Basic switch

Match a variable against multiple cases.

2. switch Inside a Function

Use `return` instead of `break` when inside a function.

3. switch Uses Loose Comparison

switch uses `==`, not `===` — be careful with type juggling.

match Expression — PHP 8.0+

Introduced in PHP 8.0, `match` is a modern alternative to `switch`. It uses strict comparison (`===`), returns a value directly, and throws an `UnhandledMatchError` if no arm matches.

1. Basic match

`match` returns a value and uses strict comparison.

2. match with default

Provide a fallback with `default`.

3. Multiple Conditions per Arm

Comma-separate multiple values in one match arm.

4. match vs switch: Strict Comparison

`match` uses `===`, avoiding the type-juggling traps of `switch`.

5. match with no-match Conditions (PHP 8.0+)

Use `true` as the subject to match on arbitrary expressions.

Nested Conditionals & Best Practices

Deep nesting makes code hard to read and maintain. Use early returns, guard clauses, and the right conditional tool to keep your logic flat and clear.

1. Nested if (Avoid Deep Nesting)

Deeply nested conditionals are hard to follow.

2. Guard Clauses (Early Return)

Return early for failure cases to flatten nesting.

3. Choosing the Right Conditional

Pick the right tool based on what you are testing.