The Elvis Operator
June 6, 2014


The magic that is the Elvis Operator was only recently introduced to me. The Elvis Operator (which is a modified, shortended version of the ternary operator) allows you to clean up your code vastly by reducing the size of simple if ( ... ){ ... } else { ... } statements. Let’s take a look a simple example of using the basic ternary operator. We can then expand on our knowledge of the ternary operator and look into the Elvis operator:

First, let’s say you have something like the following (I would argue this is a pretty common thing amongst web developers):

We can actually turn this 4 line statement into a single line. To do this we can use the ternary operator as follows:

As you can see this is just shorthand for an if { ... } else { ... } statement; where the first argument represents the if ( ... ) condition, the second argument after the ? is returned value if the statement is true, and finally the third argument located after the : is returned if the statement is false.

This is already a pretty awesome shortcut, but we can actually reduce the size of this code even further using the Elvis operator. The following definition was taken from Wikipedia:

In computer programming, the binary operator ?: colloquially referred to as the Elvis operator due to its resemblance to an emoticon, is a null-coalescing variant of the ternary conditional operator in languages such as PHP. It is used by several other languages that use the ? : operator, which is the most common ternary operator.

Binary ?:, used as an operator with two arguments around it and no characters in between, is used to obtain a value that is specified in the second argument if the first argument evaluates to null or false, and to return the value of the first argument otherwise.

An Elvis operation can be understood to represent a ?: ternary operation with the second argument omitted and assumed to equal the first. – Wikipedia

Basically the Elvis Operator is used to cut out the first assignment of the variable to itself if it is indeed set. Take a look at the following example using the ternary operator:

We can use the Elvis operator to cut out the first assignment of the variable to itself by using:

This can basically be read as, “Amount is equal to post amount if post amount is set, otherwise it is equal to zero”. Using the Elvis operator not only cleans up your code, but I personally think it makes it more readable. I much prefer the Elvis operator to the ternary operator when reading.

That’s all there is to using the Elvis operator! It’s important to remember that the Elvis operator was only implement in PHP 5.3, so make sure you have at least that installed on your machine before you try it out. As always thank you for reading! Please feel free to put any questions, suggestions, or ideas in the comments section below regarding what kind of small tips/tricks posts you would want to see, since my tutorials will be a little shorter for a while since I’ve been playing so much Wildstar! If you haven’t already, check out the game and try it for yourself – its amazing!