Update
PHP 7 adds the null coalescing operator to handle assignment depending on whether the right hand side is set.
The null coalescing operator (??) has been added as syntactic sugar
for the common case of needing to use a ternary in conjunction with
isset(). It returns its first operand if it exists and is not NULL;
otherwise it returns its second operand.
<?php
// Fetches the value of $_GET['user'] and returns 'nobody'
// if it does not exist.
$username = $_GET['user'] ?? 'nobody';
// This is equivalent to:
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
// Coalescing can be chained: this will return the first
// defined value out of $_GET['user'], $_POST['user'], and
// 'nobody'.
$username = $_GET['user'] ?? $_POST['user'] ?? 'nobody';
?>
Additionally, PHP 7.4 adds the null coalescing assignment operator, which handles the opposite case -- assigning a value depending on whether the left hand side is set:
<?php
$array['key'] ??= computeDefault();
// is roughly equivalent to
if (!isset($array['key'])) {
$array['key'] = computeDefault();
}
?>
Original Answer
I ended up just creating a function to solve the problem:
public function assignIfNotEmpty(&$item, $default)
{
return (!empty($item)) ? $item : $default;
}
Note that $item is passed by reference to the function.
Usage example:
$variable = assignIfNotEmpty($item, $default);
$variablewhen$itemis empty.