How to know what if condition is being fulfilled in PHP

10

I have a statement IF with several conditions OR there is some way to know what 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){

}
    
asked by skycomputer2 18.10.2016 в 00:20
source

7 answers

8

You can use the in_array function.

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

$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}";
}

Watch Demo

    
answered by 18.10.2016 в 03:18
3

Actually being a logical condition OR if any of the options is true the condition will enter, what you could do is print the value of $variables_tax_id_next within the true condition and see what value that variable has, although if you send a single value in that variable, the if could be replaced by a switch , so that you have more control in each case.

switch ($variables_tax_id_next) {
      case 201:
        echo $variables_tax_id_next;
        break;
      case 206:
        .
        . 
        .
      default:
        break;
}
    
answered by 18.10.2016 в 00:29
0

To know which of the conditions is being met, I suggest you use a switch case, here is the reference.

link

switch ($variables_tax_id_next) {
    case "201":
        echo "Mi valor es". $variable_tax_id_next;
        break;
    .
    .
    .
}
    
answered by 18.10.2016 в 00:30
0

In addition to SWITCH , that recommendation, you can also play with ìf and else if

if ($variables_tax_id_next == 201){
else if( $variables_tax_id_next == 206){
else if( $variables_tax_id_next == 211){
else if( $variables_tax_id_next == 216){
else if( $variables_tax_id_next == 226){
else if( $variables_tax_id_next == 231){
else if( $variables_tax_id_prev == 201){
else if( $variables_tax_id_prev == 206){
else if( $variables_tax_id_prev == 211){
else if( $variables_tax_id_prev == 216){
else if( $variables_tax_id_prev == 226){
else if( $variables_tax_id_prev == 231){
    }
    
answered by 18.10.2016 в 00:33
0

As it is stated, any of the sentences within the if() that is TRUE will give as true the IF, so, with this structure you can not get what you want. To know which is the value that arrived the best is to use a switch . Now, as it seems that $variables_tax_id_next is a number, it might be useful to measure it by some limit, for example

if ($variables_tax_id_next > 230 && $variables_tax_id_next > 259){
  ...
}else if ($variables_tax_id_next > 260 && ...)...etc
    
answered by 18.10.2016 в 19:13
0

One solution is with the switch, although I propose the following one, since you want to know what the condition was:

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){
    echo "El valor de la variable es ".$variables_tax_id_prev."<br />";
}

Greetings !!

    
answered by 18.10.2016 в 17:59
-1

Hello, you can try the following:

if (intval($variables_tax_id_next) === 201 || 
    intval($variables_tax_id_next) === 206 || 
    intval($variables_tax_id_next) === 211 || 
    intval($variables_tax_id_next) === 216 || 
    intval($variables_tax_id_next) === 226 || 
    intval($variables_tax_id_next) === 231 || 
    intval($variables_tax_id_next) === 201 || 
    intval($variables_tax_id_next) === 206 || 
    intval($variables_tax_id_next) === 211 || 
    intval($variables_tax_id_next) === 216 || 
    intval($variables_tax_id_next) === 226 || 
    intval($variables_tax_id_next) === 231){
echo "resultado correcto";
}

how are you comparing whole results sometimes php does not interpret the variables as whole so the intval.

I hope it helps you.

Greetings.

    
answered by 18.10.2016 в 19:59