So lets say I have this line in my php script:array_push($arr,array($a,$b,$c));
Lets say sometimes I have a $b1
variable to put in this array I would like to not have duplicate for the sake of maintenance how can I avoid doing this: ```
if(!isset($b1)){
array_push($arr,array($a,$b,$c,$d));
}
else{
array_push($arr,array($a,$b,$b1,$c,$d));
}
```
This is the solution I have came up with
I was hoping for some kind of syntax trick I did not know about.
I believe this works the same as Marko Mackic's solution but without using @
to suppress errors, however if there was a actual need to have null values in the array this is not a solution.
function skipNull($in){
$out=array();
foreach($in as $val){
if($val!==null){
array_push($out,$val);
}
}
return $out;
}
//skip part where variable are defined
array_push($arr,skipNull(array(
$a,$b,
isset($b1)?$b1:null,
$c,$d
)));