This is a very simple but useful function that returns the first variable from a given list of variables that has a value.
$one = 1; $two = array("", null, 4); $three= array();
print_r(either($three,$two, $one));
function either($arr) {
if(!(func_num_args() == 1 && is_array($arr)))
$arr = func_get_args();
foreach($arr as $v) {
if(is_array($v)) {
if(count($v) != 0) return either($v);
}else if($v) {
return $v;
}
}
}
An example where this could be used is $_POST/$_GET variables - if the $_POST variable is not available the alternative could be to use a $_GET variable, failing that a default value could be applied, e.g.
$query = either($_POST[query], $_GET[query], "test")