github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/docs/parser/elvis.md (about) 1 # `?:` Elvis Operator 2 3 > Returns the right operand if the left operand is falsy (expression) 4 5 ## Description 6 7 The Elvis Operator is a little like a conditional where the result of the 8 operation is the first non-falsy value from left to right. 9 10 A falsy value is any of the following: 11 12 * an unset / undefined variable 13 * any value with a `null` data type 14 * a `str` or generic with the value `false`, `null`, `0`, `no`, `off`, `fail`, 15 `failed`, or `disabled` 16 * a number (`num`, `float` or `int`) with the value `0` 17 * an empty object or zero length array 18 * and, of course, a boolean with the value `false` 19 20 21 22 ## Examples 23 24 **Assign a variable with a default value:** 25 26 ``` 27 » $foo = $bar ?: "baz" 28 ``` 29 30 If `$bar` is falsy, then the value of `$foo` will be **"baz"**. 31 32 **Multiple elvis operators:** 33 34 ``` 35 » $unset_variable ?: null ?: false ?: "foobar" 36 foobar 37 ``` 38 39 ## Detail 40 41 ### Whats in a name? 42 43 [Wikipedia](https://en.wikipedia.org/wiki/Elvis_operator) explains this best 44 where it says: 45 46 > The name "Elvis operator" refers to the fact that when its common notation, 47 > `?:`, is viewed sideways, it resembles an emoticon of Elvis Presley with his 48 > signature hairstyle. 49 50 ## See Also 51 52 * [Pipeline](../user-guide/pipeline.md): 53 Overview of what a "pipeline" is 54 * [Schedulers](../user-guide/schedulers.md): 55 Overview of the different schedulers (or 'run modes') in Murex 56 * [`&&` And Logical Operator](../parser/logical-and.md): 57 Continues next operation if previous operation passes 58 * [`??` Null Coalescing Operator](../parser/null-coalescing.md): 59 Returns the right operand if the left operand is empty / undefined (expression) 60 * [`?` STDERR Pipe](../parser/pipe-err.md): 61 Pipes STDERR from the left hand command to STDIN of the right hand command (DEPRECATED) 62 * [`err`](../commands/err.md): 63 Print a line to the STDERR 64 * [`expr`](../commands/expr.md): 65 Expressions: mathematical, string comparisons, logical operators 66 * [`out`](../commands/out.md): 67 Print a string to the STDOUT with a trailing new line character 68 * [`try`](../commands/try.md): 69 Handles non-zero exits inside a block of code 70 * [`trypipe`](../commands/trypipe.md): 71 Checks for non-zero exits of each function in a pipeline 72 * [`||` Or Logical Operator](../parser/logical-or.md): 73 Continues next operation only if previous operation fails 74 * [null](../commands/devnull.md): 75 null function. Similar to /dev/null 76 77 <hr/> 78 79 This document was generated from [gen/expr/elvis_op_doc.yaml](https://github.com/lmorg/murex/blob/master/gen/expr/elvis_op_doc.yaml).