Skip to main content


From a line committed in 2014 by a former contractor at my job:
if (isset($live) && !is_null($live) && !empty($live)) {
Can't be too careful. #PHP #Overkill #Programming
I do love null safe, null coalescing, and Elvis operators in languages like Kotlin, Dart, and C#...
PHP timeline for the introduction of these operators:

  • at most 2004: ?:
  • 2015: ??
  • 2020: Null-safe operator



Of course there can be several years between introduction and adoption.
That's cool it has it now though! Meanwhile I don't think Java has any of that still.
Hah, never heard of the https://en.wikipedia.org/wiki/Elvis_operator before. So IIUC it's a logical, short-circuiting or like || in some languages, but it's an Elvis when it's spelled ?:?
This entry was edited (2 years ago)
I didn't realize it had a more general context outside null coalescing but yeah I guess so. I thought it was just the name the Kotlin people came up with for it.
@Hank G β˜‘οΈ@Hypolite Petovan I don't know Kotlin, but null coalescing and short-circuiting logical or are slightly different.

In JS ?? is or-if-nullish and || is or:

false || 23 == 23
false ?? 23 == false
null ?? 23 == 23
undefined ?? 23 == 23

Is Elvis an or-if-null or an or?
In PHP Elvis is a or-if-falsy
null ?: 23 => 23
false ?: 23 => 23
0 ?: 23 => 23
"0" ?: 23 => 23
[] ?: 23 => 23
-0.0 ?: 23 => 23
Java has only optional, but no special operator
@Christoph S Yeah pretty weak tea. I love optional type things. I like result monads even more. Which is why I wrote a library for them for Dart (link at end). But they aren't a substitute for language level null safety convenience methods. https://www.nequalsonelifestyle.com/2021/11/29/result-monads-in-dart-and-flutter/
just add yet in the condition:

echo $live ?? 'no value'; //in case it would be null...
More simply, since there is a !empty($live) in the condition, it covers both isset($live) and !is_null($live) and both could be safely removed from the condition.
⇧