Ternary operator php without else

Can you use the Ternary Operator in PHP without the closing 'else' statement? I've tried it and it's returning errors. Google search isn't yielding anything, so I think the answer is probably no. I just wanted to double check here. For instance:

if ( isset($testing) {
  $new_variable = $testing;
}

Will only set $new_variable if $testing exists. Now I can do

$new_variable = (isset($testing) ? $testing : "");

but that returns an empty variable for $new_variable if $testing isn't set. I don't want an empty variable if it's not set, I want the $new_variable to not be created.

I tried

$new_variable = (isset($testing) ? $testing);

and it returned errors. I also tried

$new_variable = (isset($testing) ? $testing : );

and it also returned errors. Is there a way to use the Ternary Operator without the attached else statement, or am I stuck writing it out longhand?

EDIT: Following Rizier123's advice, I tried setting the 'else' part of the equation to NULL, but it still ends up appending a key to an array. The value isn't there, but the key is, which messes up my plans. Please allow me to explain further.

The code is going to take a bunch of $_POST variables from a form and use them for parameters in a stdClass which is then used for API method calls. Some of form variables will not exist, as they all get applied to the same variable for the API call, but the user can only select one. As an example, maybe you can select 3 items, whichever item you select gets passed to the stdClass and the other 2 don't exist.

I tried this:

$yes_this_test = "IDK";
$setforsure = "for sure";
$list = new stdClass;
$list->DefinitelySet = $setforsure;
$list->MaybeSet = (isset($yes_this_test) ? $yes_this_test : NULL);
$list->MaybeSet = (isset($testing) ? $testing : NULL);
print_r($list);

But obviously MaybeSet gets set to NULL because (isset($testing) comes after (isset($yes_this_test) and it returns

stdClass Object ( [DefinitelySet] => for sure [MaybeSet] => )

I won't know what order the $_POST variables are coming in, so I can't really structure it in such a way to make sure the list gets processed in the correct order.

Now I know I can do something like

if ( isset($yes_this_test ) {
  $list->MaybeSet = $yes_this_test;
}
elseif ( isset($testing) ) {
  $list->MaybeSet = $testing;
}

But I was hoping there was a shorthand for this type of logic, as I have to write dozens of these. Is there an operator similar to the Ternary Operator used for if/elseif statements?

Scout APM helps PHP developers pinpoint N+1 queries, memory leaks & more so you can troubleshoot fast & get back to coding faster. Start your free 14-day trial today.

You probably already know some comparison operators in PHP. Things like the ternary ?:, the null coalescing ?? and the spaceship <=> operators. But do you really know how they work? Understanding these operators makes you use them more, resulting in a cleaner codebase.

Before looking at each operator in depth, here's a summary of what each of them does:

  • The ternary operator is used to shorten if/else structures
  • The null coalescing operator is used to provide default values instead of null
  • The spaceship operator is used to compare two values

# Ternary operator

The ternary operator is a shorthand for the if {} else {} structure. Instead of writing this:

if ($condition) {
    $result = 'foo' 
} else {
    $result = 'bar'
}

You can write this:

$result = $condition ? 'foo' : 'bar';

If this $condition evaluates to true, the lefthand operand will be assigned to $result. If the condition evaluates to false, the righthand will be used.

Interesting fact: the name ternary operator actually means "an operator which acts on three operands". An operand is the term used to denote the parts needed by an expression. The ternary operator is the only operator in PHP which requires three operands: the condition, the true and the false result. Similarly, there are also binary and unary operators. You can read more about it here.

Back to ternary operators: do you know which expressions evaluate to true, and which don't? Take a look at the boolean column of this table.

The ternary operator will use its lefthand operand when the condition evaluates to true. This could be a string, an integer, a boolean etc. The righthand operand will be used for so called "falsy values".

Examples would be 0 or '0', an empty array or string, null, an undefined or unassigned variable, and of course false itself. All these values will make the ternary operator use its righthand operand.

Noticed a tpyo? You can submit a PR to fix it. If you want to stay up to date about what's happening on this blog, you can follow me on Twitter or subscribe to my newsletter:

# Shorthand ternary operator

Since PHP 5.3, it's possible to leave out the lefthand operand, allowing for even shorter expressions:

$result = $initial ?: 'default';

In this case, the value of $result will be the value of $initial, unless $initial evaluates to false, in which case the string 'default' is used.

You could write this expression the same way using the normal ternary operator:

$result = $condition ? $condition : 'default';

Ironically, by leaving out the second operand of the ternary operator, it actually becomes a binary operator.

# Chaining ternary operators

The following, even though it seems logical; doesn't work in PHP:

$result = $firstCondition
    ? 'truth'
    : $elseCondition
        ? 'elseTrue'
        : 'elseFalse';

The reason because is that the ternary operator in PHP is left-associative, and thus parsed in a very strange way. The above example would always evaluate the $elseCondition part first, so even when $firstCondition would be true, you'd never see its output.

I believe the right thing to do is to avoid nested ternary operators alltogether. You can read more about this strange behaviour in this Stack Overflow answer.

Furthermore, as PHP 7.4, the use of chained ternaries without brackets is deprecated.

# Null coalescing operator

Did you take a look at the types comparison table earlier? The null coalescing operator is available since PHP 7.0. It similar to the ternary operator, but will behave like isset on the lefthand operand instead of just using its boolean value. This makes this operator especially useful for arrays and assigning defaults when a variable is not set.

$undefined ?? 'fallback'; // 'fallback'

$unassigned;
$unassigned ?? 'fallback'; // 'fallback'

$assigned = 'foo';
$assigned ?? 'fallback'; // 'foo'

'' ?? 'fallback'; // ''
'foo' ?? 'fallback'; // 'foo'
'0' ?? 'fallback'; // '0'
0 ?? 'fallback'; // 0
false ?? 'fallback'; // false

The null coalescing operator takes two operands, making it a binary operator. "Coalescing" by the way, means "coming together to form one mass or whole". It will take two operands, and decide which of those to use based on the value of the lefthand operand.

# Null coalescing on arrays

This operator is especially useful in combination with arrays, because of its acts like isset. This means you can quickly check for the existance of keys, even nested keys, without writing verbose expressions.

$input = [
    'key' => 'key',
    'nested' => [
        'key' => true
    ]
];

$input['key'] ?? 'fallback'; // 'key'
$input['nested']['key'] ?? 'fallback'; // true
$input['undefined'] ?? 'fallback'; // 'fallback'
$input['nested']['undefined'] ?? 'fallback'; // 'fallback'

null ?? 'fallback'; // 'fallback'

The first example could also be written using a ternary operator:

$output = isset($input['key']) ? $input['key'] : 'fallback';

Note that it's impossible to use the shorthand ternary operator when checking the existance of array keys. It will either trigger an error or return a boolean, instead of the real lefthand operand's value.

// Returns `true` instead of the value of `$input['key']`
$output = isset($input['key']) ?: 'fallback' 

// The following will trigger an 'undefined index' notice 
// when $input is no array or has no 'key'.
//
// It will trigger an 'undefined variable' notice 
// when $input doesn't exist.
$output = $input['key'] ?: 'fallback';

# Null coalesce chaining

The null coalescing operator can easily be chained:

$input = [
    'key' => 'key',
];

$input['undefined'] ?? $input['key'] ?? 'fallback'; // 'key'

# Nested coalescing

It's possible to use the null coalescing operator on nested object properties, even when a property in the chain is null.

$a = (object) [
    'prop' => null,
];

var_dump($a->prop->b ?? 'empty');

// 'empty'

# Null coalescing assignment operator

In PHP 7,4, we can expect an even shorter syntax called the "null coalescing assignment operator".

// This operator will be available in PHP 7.4

function (array $parameters = []) {
    $parameters['property'] ??= 'default';
}

In this example, $parameters['property'] will be set to 'default', unless it is set in the array passed to the function. This would be equivalent to the following, using the current null coalescing operator:

function (array $parameters = []) {
    $parameters['property'] = $parameters['property'] ?? 'default';
}

# Spaceship operator

The spaceship operator, while having quite a peculiar name, can be very useful. It's an operator used for comparison. It will always return one of three values: 0, -1 or 1.

0 will be returned when both operands are equals, 1 when the left operand is larger, and -1 when the right operand is larger. Let's take a look at a simple example:

1 <=> 2; // Will return -1, as 2 is larger than 1.

This simple example isn't all that exiting, right? However, the spaceship operator can compare a lot more than simple values!

// It can compare strings,
'a' <=> 'z'; // -1

// and arrays,
[2, 1] <=> [2, 1]; // 0

// nested arrays,
[[1, 2], [2, 2]] <=> [[1, 2], [1, 2]]; // 1

// and even casing.
'Z' <=> 'z'; // -1

Strangely enough, when comparing letter casing, the lowercase letter is considered the highest. There's a simple explanation though. String comparison is done by comparing character per character. As soon as a character differs, their ASCII value is compared. Because lowercase letters come after uppercase ones in the ASCII table, they have a higher value.

# Comparing objects

The spaceship operator can almost compare anything, even objects. The way objects are compared is based on the kind of object. Built-in PHP classes can define their own comparison, while userland objects are compared based on their attributes and values.

When would you want to compare objects you ask? Well, there's actually a very obvious example: dates.

$dateA = DateTime::createFromFormat('Y-m-d', '2000-02-01');

$dateB = DateTime::createFromFormat('Y-m-d', '2000-01-01');

$dateA <=> $dateB; // Returns 1

Of course, comparing dates is just one example, but a very useful one nevertheless.

# Sort functions

One great use for this operator, is to sort arrays. There are quite a few ways to sort an array in PHP, and some of these methods allow a user defined sort function. This function has to compare two elements, and return 1, 0, or -1 based on their position.

An excellent use case for the spaceship operator!

$array = [5, 1, 6, 3];

usort($array, function ($a, $b) {
    return $a <=> $b;
});

// $array = [1, 3, 5, 6];

To sort descending, you can simply invert the comparison result:

usort($array, function ($a, $b) {
    return -($a <=> $b);
});

// $array = [6, 5, 3, 1];

Hi there, thanks for reading! I hope this blog post helped you! If you'd like to contact me, you can do so on Twitter or via e-mail. I always love to chat!

Footnotes

A summary about array + array in PHP

Array destructuring in PHP

Attributes in PHP 8 — A close look at attributes, also known as annotations

Array merge vs. +

Can we use if without else in PHP?

An if statement by its self is absolutely fine. If you have a series of conditions where only one will be true it's better to put the most likely first, then use else if on the subsequent ones so that PHP doesn't have to keep evaluating each condition.

Does PHP have a ternary operator?

The term "ternary operator" refers to an operator that operates on three operands. An operand is a concept that refers to the parts of an expression that it needs. The ternary operator in PHP is the only one that needs three operands: a condition, a true result, and a false result.

Does ternary need else?

A ternary operation is called ternary because it takes 3 arguments, if it takes 2 it is a binary operation. It's an expression returning a value. If you omit the else you would have an undefined situation where the expression would not return a value. You can use an if statement.

How use ternary operator in if condition in PHP?

It is called a ternary operator because it takes three operands- a condition, a result statement for true, and a result statement for false. The syntax for the ternary operator is as follows. Syntax: (Condition) ? (Statement1) : (Statement2);