An identical operator doesn't exist as of PHP 5.6, but you can make a function that behaves similarly.
/**
* Returns the first entry that passes an isset() test.
*
* Each entry can either be a single value: $value, or an array-key pair:
* $array, $key. If all entries fail isset(), or no entries are passed,
* then first() will return null.
*
* $array must be an array that passes isset() on its own, or it will be
* treated as a standalone $value. $key must be a valid array key, or
* both $array and $key will be treated as standalone $value entries. To
* be considered a valid key, $key must pass:
*
* is_null($key) || is_string($key) || is_int($key) || is_float($key)
* || is_bool($key)
*
* If $value is an array, it must be the last entry, the following entry
* must be a valid array-key pair, or the following entry's $value must
* not be a valid $key. Otherwise, $value and the immediately following
* $value will be treated as an array-key pair's $array and $key,
* respectfully. See above for $key validity tests.
*/
function first(/* [(array $array, $key) | $value]... */)
{
$count = func_num_args();
for ($i = 0; $i < $count - 1; $i++)
{
$arg = func_get_arg($i);
if (!isset($arg))
{
continue;
}
if (is_array($arg))
{
$key = func_get_arg($i + 1);
if (is_null($key) || is_string($key) || is_int($key) || is_float($key) || is_bool($key))
{
if (isset($arg[$key]))
{
return $arg[$key];
}
$i++;
continue;
}
}
return $arg;
}
if ($i < $count)
{
return func_get_arg($i);
}
return null;
}
Usage:
$option = first($option_override, $_REQUEST, 'option', $_SESSION, 'option', false);
This would try each variable until it finds one that satisfies isset()
:
$option_override
$_REQUEST['option']
$_SESSION['option']
false
If 4 weren't there, it would default to null
.
Note: There's a simpler implementation that uses references, but it has the side effect of setting the tested item to null if it doesn't already exist. This can be problematic when the size or truthiness of an array matters.