0

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>';

}

?>
3
  • Also I'm already tried change 'switch ($_POST['AnoMes'])' to 'switch ($sAnoMes)' abd result is the same. Commented Jul 9, 2020 at 12:13
  • You can't use an OR in a case statement. Just add 2 cases without a break between the two. See php.net/manual/en/control-structures.switch.php Commented Jul 9, 2020 at 12:15
  • all right, thanks for you help. Commented Jul 9, 2020 at 12:19

1 Answer 1

1

The problem is that ur cases 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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.