【问题标题】:PHP case switch with or condition带有或条件的 PHP 大小写切换
【发布时间】:2020-07-09 12:10:42
【问题描述】:

我正在尝试使用带有“或”的 SWITCH CASE 条件来获取从表单接收到的一个或其他值。但总是进入第一个条件'这里! 1'。有人可以帮我识别代码中的错误吗? 我已经尝试使用 [('value1' || 'value2')] 和 ['value1' || 'value'] 两者都不起作用,因为总是返回“变量 DDDs 的值是:$sDDDs - 这里!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>';

}

?>

【问题讨论】:

  • 另外我已经尝试将 'switch ($_POST['AnoMes'])' 更改为 'switch ($sAnoMes)' abd 结果是一样的。
  • 您不能在 case 语句中使用 OR。只需添加 2 个案例,两者之间没有中断。见php.net/manual/en/control-structures.switch.php
  • 好的,谢谢你的帮助。

标签: php switch-statement case


【解决方案1】:

问题在于,如果您使用逻辑运算符,您的 cases 正在评估为布尔值。

所以当你有case ('ate_201803'||'201804_201902') PHP 看到case true

要使用 OR 的等价物,只需列出彼此下方的案例:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-13
    • 2016-03-05
    相关资源
    最近更新 更多