Home > Mobile >  Improve php fragment
Improve php fragment

Time:10-13

I have a PHP fragment that have to be something like this:

$a = false;
if($something) $a = true;
elseif($xyz < 45) $a = true;
elseif($response % 5 == 0) $a = true;
elseif($etc >14) $a = true;

Then I was wondering if there is a way to write better this, like in RoR, that could be something like:

$a = true if($something or ($xyz < 45) or ($response % 5 == 0) or ($etc >14));

The goal is not to repeat the "$a = true" so many times...

CodePudding user response:

Since you're using elseif, you can write it in one line like this:

$a = $something || $xyz < 45 || $response % 5 == 0 || $etc >14;
  • Related