Home > Mobile >  Shorten part of the code with the condition
Shorten part of the code with the condition

Time:10-08

There is a block of conditions.Help to shorten and improve this part of the code

$stock = $row['KOLVO_T'];

if ($row['CENA'] < 1000 && $row['KOLVO_T'] == 1) {
   $stock = 0;
}

if ($row['CENA'] >= 1000 && $row['KOLVO_T'] == 1) {
   $stock = $row['KOLVO_T'];
}
if ($row['KOLVO_T'] >= 2) {
   $stock = $row['KOLVO_T'];
}

return $stock;

CodePudding user response:

I don't really see the point, but here's the simplest I could do:

$stock = $row['KOLVO_T'];

if ($row['CENA'] < 1000 && $stock == 1) {
   $stock = 0;
}

return $stock;

The last two conditional blocks are useless since they repeat an operation you already did before the first conditional block, and their condition can't be verified if the first is.

  •  Tags:  
  • php
  • Related