I currently use this code pattern fairly frequently
if(array_key_exists('possible_key', $array)) {
$my_variable = $array['possible_key'];
} else {
$my_variable = 'my default value';
}
Is there a better way to be writing this?
One quick change you can do is making use of ternary operator instead of writing if else block.
$my_variable = array_key_exists('possible_key', $array)
? $array['possible_key']
: 'my default value';
array_key_exists('possible_key', $array)
and isset($array['possible_key'])
?
\$\endgroup\$
Commented
Oct 31, 2012 at 1:44
$array['possible_key'] = null;
isset($array['possible_key']); /* false */ array_key_exists('possible_key', $array); /* true */
\$\endgroup\$
A modern (PHP7+) solution in case you want a default value when either the element is not set or the value is null.
$my_variable = $array['possible_key'] ?? 'my default value';
I would prefer to keep it simple and not to use if else. Just assign it the default value, and change only when the condition meets.
$my_variable = 'my default value';
if(array_key_exists('possible_key', $array)) $my_variable = $array['possible_key'];
Is there an atomic way to check if the value exists by a key AND return the value, so there will be just one query to the array? I think that this was the original idea of the author of the question. In a very large array, all examples here have shown essentially too lookup: for example array_key_exists('possible_key', $array) ? $array['possible_key']
etc...
A better way would be a single operation with the array that would not give a PHP run time warning in the key does not exist.
Unfortunately, PHP does not have an atomic check-and-lookup. So, the answer to the author's question is "NO, this is not possible in PHP to check and lookup in one operation".
However, on the question of "is there a better way to be writing this", the answer is "YES, you can avoid repeating string literals and use ternary operator, but there still be two operations with the array". So here is an improved version of the author's code:
$my_key = 'possible_key'; $my_variable = (array_key_exists($my_key, $array)) ? $array[$my_key] : 'my default value';
idx()
with this behavior. docs.hhvm.com/hack/reference/function/HH.idx
\$\endgroup\$
Commented
Mar 26, 2018 at 17:12
Untested code here as I don't have access to my dev machine, but taking Sky's ternary suggestion above and simplifying it (one empty() call is faster than an array_key_exists (see https://stackoverflow.com/questions/6884609/array-key-existskey-array-vs-emptyarraykey)
$var = empty($array['possible_key']) ? 'default value' : $array['possible_key'];
$array['possible_key']
is set to ''
or 0
, this'll return 'default value'
.
\$\endgroup\$
Commented
Oct 31, 2012 at 1:45
'0'
, etc, this is shorter and a tiny bit faster. (isset
would probably be a better fit here than empty
by the way. isset
has fewer false negatives in this context.)
\$\endgroup\$
I prefer to use use isset and make little refactor and wrap that code to function/method
function defaultize( $array_var ,$defaul_val ){
if(isset($array_var)) {
$ret = $array_var;
} else {
$ret = 'my default value';
}
return $ret;
}
and then use in code
$my_variable = defaultize($array['possible_key'],'my default_value' );
$my_variable2 = defaultize($array['possible_key2'],'my default_value2' );
$array
seem uninitialized inside the function.
\$\endgroup\$
Commented
Oct 30, 2012 at 18:52