简体   繁体   中英

PHP case switch with or condition

I'm trying to use SWITCH CASE condition with 'or' for get one or other value received from form. But always getting into first condition 'here. 1'? Someone can help me to identify the error into the code: I already try using [('value1' || 'value2')] and ['value1' || 'value'] the the both doesnt work because always returnin "The value of the variable DDDs is: $sDDDs - here! 1"

<?php
$sDDDA = $_POST['DDDA'];
$sNumA = $_POST['NumA'];
$sDtInit = $_POST['DtInit'];
$sDtEnd = $_POST['DtEnd'];
$sIdProduct = $_POST['IdProduct'];
$sAnoMes = $_POST['AnoMes'];
$sDDDs = $_POST['DDDs'];
$sMSISDN = $_POST['DDDA'] . $_POST['NumA'];
$s55MSISDN = "55" . $_POST['DDDA'] . $_POST['NumA'];

echo "The value of the variable DDDA is: $sDDDA <br>";
echo "The value of the variable NumA is: $sNumA <br>";
echo "The value of the variable DtInit is: $sDtInit <br>";
echo "The value of the variable DtEnd is: $sDtEnd <br>";
echo "The value of the variable AnoMes is: $sAnoMes <br>";
echo "The value of the variable DDDs is: $sDDDs <br>";
echo "The value of the variable MSISDN is: $sMSISDN <br>";
echo "The value of the variable 55MSISDN is: $s55MSISDN <br>";
echo "</b><br><br>";

switch($_POST['IdProduct']){
case 'Conecta':

echo "The value of the variable IdProduct is: $sIdProduct - here! <b>1</b><br><br>";

    switch ($_POST['AnoMes']){
    case ('ate_201803'||'201804_201902'):
    echo "The value of the variable AnoMes is: $sAnoMes - here! <b>1</b><br><br>";

        switch ($_POST['DDDs']){
        case '1x4x5x6x':
        echo "The value of the variable DDDs is: $sDDDs - here! <b>1 - 1x </b><br><br>";

        break;
        case '2x3x7x8x9x' :
        echo "The value of the variable DDDs is: $sDDDs - here! <b>1 - 2x </b><br><br>";

        break;
        default:
        echo 'ERRO! The value is wrong for variable DDD! here! <b>1</b><br><br>';
          
        }

    break;
    case 'apartir_201903':
    echo "The value of the variable AnoMes is: $sAnoMes - here! <b>5</b><br><br>";

        switch ($_POST['DDDs']){
        case '1x4x5x6x' :
        echo "The value of the variable DDDs is: $sDDDs  - here! <b>5 - 1x </b><br><br>";

        break;
        case '2x3x7x8x9x' :
        echo "The value of the variable DDDs is: $sDDDs  - here! <b>5 - 2x </b><br><br>";

        break;
        default:
        echo 'ERRO! The value is wrong for variable DDD! here! <b>5</b><br><br>';
          
        }

    break;
    default:
    echo 'ERRO! The value is wrong for variable AnoMes!</b><br><br>';
    }

break;
case 'Recado':

echo "The value of the variable IdProduct is: $sIdProduct - here! <b>3<br>";

    switch ($_POST['AnoMes']){
    case ('ate_201803'||'201804_201902'):
    echo "The value of the variable AnoMes is: $sAnoMes - here! <b>3<br>";

        switch ($_POST['DDDs']){
        case '1x4x5x6x' :
        echo "The value of the variable DDDs is: $sDDDs - here! <b>3 - 1x </b><br><br>";

        break;
        case '2x3x7x8x9x' :
        echo "The value of the variable DDDs is: $sDDDs - here! <b>3 - 2x </b><br><br>";

        break;
        default:
        echo 'ERRO! The value is wrong for variable DDD! here! <b>3</b><br><br>';
          
        }

    break;
    case 'apartir_201903':
    echo "The value of the variable AnoMes is: $sAnoMes - here! <b>4<br>";

        switch ($_POST['DDDs']){
        case '1x4x5x6x' :
        echo "The value of the variable DDDs is: $sDDDs  - here! <b>4 - 1x </b><br><br>";
    
        break;
        case '2x3x7x8x9x' :
        echo "The value of the variable DDDs is: $sDDDs  - here! <b>4 - 2x </b><br><br>";

        break;
        default:
        echo 'ERRO! The value is wrong for variable DDD! here! <b>4</b><br><br>';
          
        }

    break;
    default:
    echo 'ERRO! The value is wrong for variable AnoMes!</b><br><br>';

    }

break;
default:
echo 'ERRO! The value is wrong for variable AnoMes!</b><br><br>';

}

?>

The problem is that ur case s are evalutating to bools if you use logical operators.

so when you have case ('ate_201803'||'201804_201902') PHP sees case true .

To use the equivalent of OR just list the cases below each other:

switch ($value) {
  case 'ate_201803':
  case '201804_201902':
    // ...
  break;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM