1. 19
    Refactor a PHP if else statement into a ternary
    3m 33s

Refactor a PHP if else statement into a ternary

Mark Shust
InstructorMark Shust

Share this video with your friends

Send Tweet
Published 11 months ago
Updated 6 months ago

An if statement that depends upon the logic of a boolean can be simplified into something called a "ternary", which can lead to more concise & readable code.

Instructor: [0:00] Because checking for a truthy result is so common, PHP supplies a simplified if-else shorthand syntax called a ternary. This is useful when you want to execute a very simple statement based on the results of a condition.

[0:15] Rather than using if-else here, we will use a question mark and a colon. We're going to first remove this num posts logic of exactly three posts since we won't be dealing with it anymore. We will refactor this if-has post condition to a ternary.

[0:36] Let's go ahead and remove this, and we will create a new PHP tag. In this logic, since we are just echoing out two lines, we can write our echo tag first, followed by the condition. Our condition is has-posts.

[0:54] You can think of this as the part in between the parentheses of an if-statement. We can even add parentheses around it if we wish, but that's not really necessary at the moment.

[1:06] Next comes a question mark, which is the equivalent of open and closed brackets of an if-statement. Then since we are echoing something out, next will be the value that we want returned to this echo statement when this condition is true. This will be posts exist.

[1:25] Immediately after this string is the else condition, which is represented with a colon. This follows the same format as the question mark. The result of executing this else condition will come next. This will output there are no posts.

[1:44] Let me expand this window just a little bit so we can actually see what's going on within our code. You'll notice we'll also need an ending PHP tag. If has-post is true, we'll echo out post exist. If not, it will echo out there are no posts.

[2:05] If we save and refresh this page, we can see that post exist still shows if it's 10. If this is zero, we will get the output of there are no posts. The vigilant watchers of this lesson may have noticed that this PHP tag is followed with an echo statement.

[2:26] You'll notice that this can be refactored into a short echo statement. This can give us the highest level of terseness or the most concise code possible for this block of code. I greatly prefer ternaries when they are used in this format with short echo tags, and with conditions that are assigned two well named variables.

[2:47] They're just so simple and easy to read once you get used to this format. Keep in mind that you only really ever want to use a ternary when you're dealing with Boolean logic, that is, mainly just a single true or false condition.

[3:02] This is why I assign conditional logic two variables such as has-posts because it makes your code so much easier to read. I also use this method a lot in backend PHP code when evaluating and returning values from Boolean checks.

[3:19] Whenever conditions get more complex or seem hard to follow along with ternaries, you'll want to use the longer if-else format as it will be easier to understand for more complex conditional statements.