Shorthand Conditionals in PHP You Should Know

The if...else statement is one of the most common logic patterns we write as programmers. It executes one block of code if a condition is true, and another if the condition is false. It’s so fundamental that I honestly can’t remember a day I didn’t write some form of if condition. As a quick refresher, here’s what the basic syntax looks like: if (condition) { // code to execute if condition is true } else { // code to execute if condition is false } Imagine you’re building an e-commerce backend. When a user makes a payment, you want to handle scenarios where the payment is successful or fails: if ($payment->isSuccessful()) { // Mark order as paid // Generate invoice // Update user’s purchase history // Send confirmation email } else { // Log the failure // Notify the user } In the example above, both the success and failure paths include multiple steps. This is where the traditional if...else syntax shines; it keeps your logic clean, organised, and easy to maintain. But sometimes, our logic isn’t that complex. For instance, what if we simply want to check if a user's role is "admin"? If it is, they can access a page; if not, they can’t. Writing the traditional if...else block for this might seem too much. Luckily, PHP provides shorthand techniques for conditional logic that can keep your code concise and readable. In this article, we’ll explore some of these shorthand methods and when best to use them. 1. Ternary Operator This is probably the most well-known shorthand for conditionals, and chances are, you've already seen it in the wild. $value = (condition) ? 'return this if true' : 'return this if false'; This would normally be a 4–5 line traditional if...else block, but now it is just a single line. It’s great for simple decisions where both outcomes are straightforward. $score = 70; $result = ($score >= 70) ? 'pass' : 'fail'; Nesting Ternary Operators You can nest ternary operators, but here’s a big disclaimer: I don’t recommend it. It quickly becomes unreadable and error-prone. It’s usually better to stick to the traditional if...else block. $value = ($user->isAdmin()) ? 'Show admin panel' : ($user->isEditor() ? 'Show editor dashboard' : 'Show viewer homepage'); Yes, it works—but imagine debugging this later

Apr 30, 2025 - 09:26
 0
Shorthand Conditionals in PHP You Should Know

The if...else statement is one of the most common logic patterns we write as programmers. It executes one block of code if a condition is true, and another if the condition is false. It’s so fundamental that I honestly can’t remember a day I didn’t write some form of if condition.

As a quick refresher, here’s what the basic syntax looks like:

if (condition) {
    // code to execute if condition is true
} else {
    // code to execute if condition is false
}

Imagine you’re building an e-commerce backend. When a user makes a payment, you want to handle scenarios where the payment is successful or fails:

if ($payment->isSuccessful()) {
    // Mark order as paid
    // Generate invoice
    // Update user’s purchase history
    // Send confirmation email
} else {
    // Log the failure
    // Notify the user
}

In the example above, both the success and failure paths include multiple steps. This is where the traditional if...else syntax shines; it keeps your logic clean, organised, and easy to maintain.

But sometimes, our logic isn’t that complex. For instance, what if we simply want to check if a user's role is "admin"? If it is, they can access a page; if not, they can’t. Writing the traditional if...else block for this might seem too much.

Luckily, PHP provides shorthand techniques for conditional logic that can keep your code concise and readable.

In this article, we’ll explore some of these shorthand methods and when best to use them.

1. Ternary Operator

This is probably the most well-known shorthand for conditionals, and chances are, you've already seen it in the wild.

$value = (condition) ? 'return this if true' : 'return this if false';

This would normally be a 4–5 line traditional if...else block, but now it is just a single line. It’s great for simple decisions where both outcomes are straightforward.

$score = 70;
$result = ($score >= 70) ? 'pass' : 'fail';

Nesting Ternary Operators

You can nest ternary operators, but here’s a big disclaimer: I don’t recommend it. It quickly becomes unreadable and error-prone. It’s usually better to stick to the traditional if...else block.

$value = ($user->isAdmin())
    ? 'Show admin panel'
    : ($user->isEditor() ? 'Show editor dashboard' : 'Show viewer homepage');

Yes, it works—but imagine debugging this later