Thursday, May 27, 2021

Crash Course in the PHP Ternary Operator With Examples

In this article, we’ll discuss the ternary operator in PHP. Along with the syntax, we’ll go through a couple of real world examples to understand how it works.

What Is the Ternary Operator?

The ternary operator (? and :) is a conditional operator which allows you to execute a condition, and based on the result of the condition, it allows you to return different results. In most cases, you could use it as an alternative to the if-else statement, specifically when you want to assign variables based on the outcome of the specific condition.

Syntax

Let’s go through the syntax of the ternary operator.

In most cases, the ternary operator takes the following form:

First, expression1 is executed, and if it evaluates to TRUE, expression2 is evaluated and returned, otherwise expression3 is evaluated and its result is returned.

It’s important to note that the ternary operator is an expression. So you can’t use it to return a value in a function which returns the value by reference.

So that’s about the syntax of the ternary operator. In the next section, we’ll see a couple of real-world examples to understand how it works.

Examples

In this section, we’ll go through a couple of examples to understand how the ternary operator works.

First of all, let’s go through the following if-else example.

It’s a very simple if-else statement which checks if the $_GET['limit'] variable is set, and if it’s set, initializes the $limit variable with the value of the $_GET['limit'] variable. Otherwise it sets the $limit variable to 10.

Now, let’s see how the above snippet looks like with the ternary operator.

As you can see, when we use the ternary operator, it’s just a single line of code instead of five lines of the if-else statement code. Apart from the fact that it has reduced overall code length, the code looks more readable too.

You can also stack multiple ternary expressions as shown in the following example.

In the above example, if the $_GET['limit'] variable is set, it would call the another ternary expression, which checks if the value of the $_GET['limit'] variable is greater than 100, it would return 10, otherwise it would return the actual value of the $_GET['limit'] variable. If the value of the $_GET['limit'] variable is not set, it would default to 10.

Although, you may be tempted to stack multiple ternary expressions in order to reduce code length, it’s not recommended since it can make things difficult to understand, and the ternary operator would lose its original purpose of making things simpler.

What Is the Benefit Of the Ternary Operator?

So now you’re aware of the basics of the ternary operator. In this section, we’ll see a couple of benefits of using the ternary operator.

The main benefit of the ternary operator is that it reduces code length. Let’s have a look at the following example to understand it.

Now, when you use the ternary operator, the above code looks like this.

Isn’t it looking much more compact now?

Apart from reducing code length, it improves code readability. When you’re using the ternary operator, you can quickly figure out how the expression is going to be evaluated.

Finally, If your code is compact, it’s easier to maintain and debug it over the period of time.

What Is the Ternary Shorthand Operator?

In this section, we’ll discuss how exactly the ternary shorthand operator, a shorthand version of the ternary operator, works. You would want to use the ternary shorthand operator, if you want to return the result of the test statement if the test is true—this avoids duplication.

Syntax

Let’s go through the syntax of the ternary shorthand operator.

First, expression1 is executed, and if it evaluates to TRUE, it returns expression1, otherwise it returns expression2. As you can see, we’re avoiding duplication by skipping the expression between the ? and : separators.

Examples

Let’s quickly go through the following example to see how the ternary shorthand operator works.

In the above example, if the $x variable evaluates to TRUE, the value of the $x variable is assigned to the $output variable, otherwise $output will be set to 0. I’ve also demonstrated how it would look like with the regular ternary operator.

Conclusion

Today, we discussed the basics of the ternary operator in PHP along with a couple of real-world examples. I hope you find the ternary operator useful in your coding. I know I do!

No comments:

Post a Comment