1

Improve php fragment

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...

Submitted October 12th 2021 by Admin

Answers
0

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

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

Admin | 6 days ago



Relevant Questions