patternphpMinor
Is there a way to shorten a set of conditionals like this?
Viewed 0 times
thisconditionalswayshortenlikethereset
Problem
I have a long set of IF statements that basically set a null object Value to
My code looks like this:
One way I know could work would simply be to iterate through the e
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->sl1 === NULL){ $data->sl1 = "0";}
if ($data->sl2 === NULL){ $data->sl2 = "0";}
if ($data->sl3 === NULL){ $data->sl3 = "0";}
if ($data->sl4 === NULL){ $data->sl4 = "0";}
if ($data->sl5 === NULL){ $data->sl5 = "0";}
if ($data->sl6 === NULL){ $data->sl6 = "0";}
if ($data->sl7 === NULL){ $data->sl7 = "0";}
if ($data->sl8 === NULL){ $data->sl8 = "0";}
if ($data->sl9 === NULL){ $data->sl9 = "0";}
if ($data->sl10 === NULL){ $data->sl10 = "0";}
if ($data->sl11 === NULL){ $data->sl11 = "0";}
if ($data->sl12 === NULL){ $data->sl12 = "0";}
if ($data->sn1 === NULL){ $data->sn1 = "0";}
if ($data->sn2 === NULL){ $data->sn2 = "0";}
if ($data->sn3 === NULL){ $data->sn3 = "0";}
if ($data->sn4 === NULL){ $data->sn4 = "0";}
if ($data->sn5 === NULL){ $data->sn5 = "0";}
if ($data->sn6 === NULL){ $data->sn6 = "0";}
if ($data->sn7 === NULL){ $data->sn7 = "0";}
if ($data->sn8 === NULL){ $data->sn8 = "0";}
if ($data->sn9 === NULL){ $data->sn9 = "0";}
if ($data->sn10 === NULL){ $data->sn10 = "0";}
if ($data->sn11 === NULL){ $data->sn11 = "0";}
if ($data->sn12 === NULL){ $data->sn12 = "0";}
if ($data->sr1 === NULL){ $data->sr1 = "0";}
if ($data->sr2 === NULL){ $data->sr2 = "0";}
if ($data->sr3 === NULL){ $data->sr3 = "0";}
if ($data->sr4 === NULL){ $data->sr4 = "0";}
if ($data->sr5 === NULL){ $data->sr5 = "0";}
if ($data->sr6 === NULL){ $data->sr6 = "0";}
if ($data->sr7 === NULL){ $data->sr7 = "0";}
if ($data->sr8 === NULL){ $data->sr8 = "0";}
if ($data->sr9 === NULL){ $data->sr9 = "0";}
if ($data->sr10 === NULL){ $data->sr10 = "0";}
if ($data->sr11 === NULL){ $data->sr11 = "0";}
if ($data->sr12 === NULL){ $data->sr12 = "0";}One way I know could work would simply be to iterate through the e
Solution
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:
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.
Or if (for some reason) you don't want to use an array:
If you used an array, this would be much, much simpler:
foreach ($this->sl as $k => $sl) {
$this->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->{'sl' . $i}; ++$i) {
$sl[] = $this->{'sl' . $i};
}Or if (for some reason) you don't want to use an array:
for ($i = 1; isset($this->{'sl' . $i}; ++$i) {
$this->{'sl' . $i} = ($this->{'sl' . $i} === null) ? "0" : $this->{'sl' . $i};
}Code Snippets
foreach ($this->sl as $k => $sl) {
$this->sl = ($sl === null) ? "0" : $sl;
}$sl = array();
for ($i = 1; isset($this->{'sl' . $i}; ++$i) {
$sl[] = $this->{'sl' . $i};
}for ($i = 1; isset($this->{'sl' . $i}; ++$i) {
$this->{'sl' . $i} = ($this->{'sl' . $i} === null) ? "0" : $this->{'sl' . $i};
}Context
StackExchange Code Review Q#41514, answer score: 5
Revisions (0)
No revisions yet.