The Ternary Operator
As the Ternary Operator pattern can exist in both JS/TS and PHP 🐘, I decided to break this example out onto its own page.
This page’s code blocks are written in PHP, and the concept applies to both languages
Example 1: A great use case for the ternary operator
Before:
$biskinik_cat_name = '';
$biskinik_term = get_the_terms( get_the_ID(), 'biskinik_categories' );
if ( $biskinik_term ) {
$biskinik_cat_name = $biskinik_term[0]->slug;
}
Since the purpose of this if
statement is to conditionally assign a variable’s contents, a ternary operator allows the purpose of the condition check to come first, thus the line could be refactored as follows:
Ternary Operator:
$biskinik_term = get_the_terms( get_the_ID(), 'biskinik_categories');
$biskinik_cat_name = ( $biskinik_term ) ? $biskinik_term[0]->slug : '';
Here, we see that $biskinik_cat_name
is being set conditionally to either the first term’s slug or an empty string.
In this case, prefer either the first block or the ternary operator block, as the middle block obscures and confuses the code’s intention.
Example 2: A poor use-case for the ternary operator
if ( get_post_meta( get_the_ID(), 'archive_content', true ) ) {
$content = get_post_meta( get_the_ID(), 'archive_content', true );
} else {
$content = get_post_meta( get_the_ID(), 'content', true );
}
At first glance, this code block could benefit from the ternary operator, however, here is an argument against using it.
Since this is an if/else
statement, one could only use a single line statement for the else
block, but the arguments are quite close and the difference could easily be lost on a reader (the only difference is the second argument of get_post_meta
being ‘content’ vs ‘archive_content’).
As this statement sets the value of $content
based on a condition, the ternary operator would would like this:
$content = ( get_post_meta( get_the_ID(), 'archive_content', true ) ) ? ( get_post_meta( get_the_ID(), 'archive_content', true ) ) : ( get_post_meta( get_the_ID(), 'content', true ) );
However, this ternary operator breaks the single-line rule (by wrapping to a second line). Furthermore, it obscures the tiny argument difference by forcing the reader to read an almost identical block of code three times.
A better option would be to refactor this code to make it more readable overall.
The Best Option 🏆
$archive_content = get_post_meta( get_the_ID(), 'archive_content', true );
$content = ( ! empty( $archive_content ) ) ? $archive_content : get_post_meta( get_the_ID(), 'content', true );
Here, instead of relying on an empty string to be interpreted as a falsy
value, we make use of php’s empty()
function to check if $archive_content
is an empty string or false
(both are possible return
values of get_post_meta()
) and set $content
accordingly.