There’s at least 3 ways to code if conditionals and else statements in WordPress using PHP.
In the first 2 examples below, we’ll use the most common methods using if and else.
In the 3rd method, we’ll use the ternary operator question mark ? and colon :
In programming language, a ternary operator is commonly referred to as a conditional operator which excepts 3 arguments.
Is the use of a ternary operator a more effecient way to code if conditionals and else statements using PHP? Test the code yourself and find out.
Here’s 3 working examples you can paste into your child themes functions file and test at the same time or individually: