How to deal with too many $_POST variable conditions from ajax request at backend? [closed]

I Would start with creating a array that hold all “conditions” logic, loop it, check to see if some condition is met and return the value.

$conditions = [
    'variable_1'  => 'DoSomeStuffWithVariable_1',
    'variable_1'  => 'DoSomeStuffWithVariable_1',
    'variable_1'  => 'DoSomeStuffWithVariable_1',
    'variable_xy' => 'DoSomeStuffWithVariable_xy'
];

foreach ($conditions as $condition_key => $condition_value) {
    if (isset($_POST[$condition_key])) {
        $returnArray['return'] = $model->$condition_value();
        break;
    }
}

Now you might ask why in the $conditions I used only the method name instead of calling the method so that it will already contain the value.

The reason is to save resources, I don’t know how many checks you have, maybe you habe hundreds, so instead of calling all hundred methods every time, we only call the method once only if the isset check passes.

The check also stops once we have found a valid condition so as to not use any more resources, because we only really need the one.