Is there any more efficient,cleaner and shorter form for following:
if($x==null or $y==null or $z==null or $w==null) return true;
In general how can I also get name of null variables?
function any_null(array $a){
foreach($a as $v) if ($v==null) return //variable name;
return false;
}
var_dump(any_null(array($x,$y,$z,$w)));
isset can take multiple arguments.
Since this is the opposite of what you want a simple negation is needed:
An additional bonus is that
issetis not a regular function and will not raise any notices on undefined variables.It’s harder to get the name from the variables. You will need to check every one of them in some way. I think the best and most scalable solution is to use an associative array with the name of the variable as key:
Note the strict comparison (
===). Otherwisefalse,"",0andarray()would be counted as “null” as well.Another solution is to only pass the name of your variables and make use of the $GLOBALS array. Here I’ve coupled it with a call to func_get_args to get a little less verbose calling convention:
This last solution have a lot of shortcomings though. You can only test variables in the global scope, and you can only test standard variables (not elements in an array, not objects and so on). The syntax for those would be very clunky.