Is there a way to shorten a set of conditionals like this? I have a long set of IF statements that basically set a null object Value to 0, I feel that because i am doing the same action each time their has to be a simple way to make this allot shorter. It just looks like something I wouldn't normally see in code.

My code looks like this:

if ($data-&gt;sl1 === NULL){ $data-&gt;sl1 = "0";}
if ($data-&gt;sl2 === NULL){ $data-&gt;sl2 = "0";}
if ($data-&gt;sl3 === NULL){ $data-&gt;sl3 = "0";}
if ($data-&gt;sl4 === NULL){ $data-&gt;sl4 = "0";}
if ($data-&gt;sl5 === NULL){ $data-&gt;sl5 = "0";}
if ($data-&gt;sl6 === NULL){ $data-&gt;sl6 = "0";}
if ($data-&gt;sl7 === NULL){ $data-&gt;sl7 = "0";}
if ($data-&gt;sl8 === NULL){ $data-&gt;sl8 = "0";}
if ($data-&gt;sl9 === NULL){ $data-&gt;sl9 = "0";}
if ($data-&gt;sl10 === NULL){ $data-&gt;sl10 = "0";}
if ($data-&gt;sl11 === NULL){ $data-&gt;sl11 = "0";}
if ($data-&gt;sl12 === NULL){ $data-&gt;sl12 = "0";}

if ($data-&gt;sn1 === NULL){ $data-&gt;sn1 = "0";}
if ($data-&gt;sn2 === NULL){ $data-&gt;sn2 = "0";}
if ($data-&gt;sn3 === NULL){ $data-&gt;sn3 = "0";}
if ($data-&gt;sn4 === NULL){ $data-&gt;sn4 = "0";}
if ($data-&gt;sn5 === NULL){ $data-&gt;sn5 = "0";}
if ($data-&gt;sn6 === NULL){ $data-&gt;sn6 = "0";}
if ($data-&gt;sn7 === NULL){ $data-&gt;sn7 = "0";}
if ($data-&gt;sn8 === NULL){ $data-&gt;sn8 = "0";}
if ($data-&gt;sn9 === NULL){ $data-&gt;sn9 = "0";}
if ($data-&gt;sn10 === NULL){ $data-&gt;sn10 = "0";}
if ($data-&gt;sn11 === NULL){ $data-&gt;sn11 = "0";}
if ($data-&gt;sn12 === NULL){ $data-&gt;sn12 = "0";}

if ($data-&gt;sr1 === NULL){ $data-&gt;sr1 = "0";}
if ($data-&gt;sr2 === NULL){ $data-&gt;sr2 = "0";}
if ($data-&gt;sr3 === NULL){ $data-&gt;sr3 = "0";}
if ($data-&gt;sr4 === NULL){ $data-&gt;sr4 = "0";}
if ($data-&gt;sr5 === NULL){ $data-&gt;sr5 = "0";}
if ($data-&gt;sr6 === NULL){ $data-&gt;sr6 = "0";}
if ($data-&gt;sr7 === NULL){ $data-&gt;sr7 = "0";}
if ($data-&gt;sr8 === NULL){ $data-&gt;sr8 = "0";}
if ($data-&gt;sr9 === NULL){ $data-&gt;sr9 = "0";}
if ($data-&gt;sr10 === NULL){ $data-&gt;sr10 = "0";}
if ($data-&gt;sr11 === NULL){ $data-&gt;sr11 = "0";}
if ($data-&gt;sr12 === NULL){ $data-&gt;sr12 = "0";}


One way I know could work would simply be to iterate through the entire object with a simple foreach like this:

foreach ($data as $item) {
    if ($item === NULL){ $item = "0";}
}


But let's say I don't want to change every single null object to 0.  Do I have any other option?

I am using PHP in my example, but I am curious to hear answers for other languages as well.
In short, the CSV parser is using the wrong data types. Whenever you have contiguous variable names, it screams "use an array!" If you at all, possibly can, use an array. That allows you to operate on it much, much more naturally, it's more flexible, and it's way less prone to human error.



If you used an array, this would be much, much simpler:

foreach ($this-&gt;sl as $k =&gt; $sl) {
    $this-&gt;sl = ($sl === null) ? "0" : $sl;
}




Since you seemingly can't use an array, your best bet is probably to either process it into an array, or use some nasty variable-variable hackiness to pretend it's an array.

$sl = array();
for ($i = 1; isset($this-&gt;{'sl' . $i}; ++$i) {
    $sl[] = $this-&gt;{'sl' . $i};
}




Or if (for some reason) you don't want to use an array:

for ($i = 1; isset($this-&gt;{'sl' . $i}; ++$i) {
    $this-&gt;{'sl' . $i} = ($this-&gt;{'sl' . $i} === null) ? "0" : $this-&gt;{'sl' . $i};
}