How to know what if condition is being fulfilled in PHP

Question:

I have an IF statement with several OR conditions, is there a way to know which condition is being met:

if ($variables_tax_id_next == 201 || 
    $variables_tax_id_next == 206 || 
    $variables_tax_id_next == 211 || 
    $variables_tax_id_next == 216 || 
    $variables_tax_id_next == 226 || 
    $variables_tax_id_next == 231 || 
    $variables_tax_id_prev == 201 || 
    $variables_tax_id_prev == 206 || 
    $variables_tax_id_prev == 211 || 
    $variables_tax_id_prev == 216 || 
    $variables_tax_id_prev == 226 || 
    $variables_tax_id_prev == 231){

}

Answer:

You can use the in_array function.

Since you are using the same numbers in the if condition for the variables $..._next and $..._prev , I have created an array ( $num ) and with that you do the check:

$num = [201, 206, 211, 216, 226, 231];

if (in_array($variables_tax_id_next, $num)) {

    echo "Valor next: {$variables_tax_id_next}\n";
}

if (in_array($variables_tax_id_prev, $num)) {

    echo "Valor prev: {$variables_tax_id_prev}";
}

View Demo

Scroll to Top