PHP переключатель корпуса с условием или - PullRequest
0 голосов
/ 09 июля 2020

Я пытаюсь использовать условие SWITCH CASE с 'или' для получения того или иного значения, полученного из формы. Но всегда попадаю в первое состояние! 1 '. Кто-нибудь может помочь мне определить ошибку в коде? Я уже пробовал использовать [('value1' || 'value2')] и ['value1' || 'value'] оба они не работают, потому что всегда returnin "Значение переменной DDDs: $ 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>';

}

?>

1 Ответ

1 голос
/ 09 июля 2020

Проблема в том, что ur case s вычисляются как bools, если вы используете логические операторы.

поэтому, когда у вас есть case ('ate_201803'||'201804_201902') PHP, видит case true.

To используйте эквивалент OR, просто перечислите случаи ниже друг друга:

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