如何在PHP中优化64 if else if语句

时间:2019-02-24 11:11:21

标签: php

我可以将代码优化为小代码吗,因为我有2个选择和8个选项

这是php代码:

if (isset($_POST['groupM']) && isset($_POST['groupF'])) {
        $groupM = $_POST['groupM'];
        $groupF = $_POST['groupF'];
        if ($groupM == 1 && $groupF == 1) {
            echo "It's a girl";
        }elseif($groupM == 1 && $groupF == 2){
            echo "Baby's gender can not be predicted — Rh factor and blood type produce different results.";
        }elseif($groupM == 1 && $groupF == 3){
            echo "Baby's gender can not be predicted — Rh factor and blood type produce different results.";
        }elseif($groupM == 1 && $groupF == 4){
            echo "It's a boy";
        }elseif($groupM == 1 && $groupF == 5){
            echo "It's a girl";
        }elseif($groupM == 1 && $groupF == 6){
            echo "Baby's gender can not be predicted — Rh factor and blood type produce different results.";
        }elseif($groupM == 1 && $groupF == 7){
            echo "Baby's gender can not be predicted — Rh factor and blood type produce different results.";
        }elseif($groupM == 1 && $groupF == 7){
            echo "It's a boy";
        }
        // ... and so on. 64 result have
    }

此代码通过选择父亲血型和母亲血型并显示结果,根据血型(男孩或女孩)计算婴儿的性别预测

这是html代码:

<form id="get_my_baby_gender" method="POST">
            <label for="groupM">Father's blood group:</label>
                <select class="input_box" id="groupM" name="groupM" size="1">
                    <option value="1">O(I) Rh−</option>
                    <option value="2">O(I) Rh+</option>
                    <option value="3">A(II) Rh−</option>
                    <option value="4">A(II) Rh+</option>
                    <option value="5">B(III) Rh−</option>
                    <option value="6">B(III) Rh+</option>
                    <option value="7">AB(IV) Rh−</option>
                    <option value="8">AB(IV) Rh+</option>
                </select>

            <label for="groupF">Mother's blood group:</label>
            <select class="input_box" id="groupF" name="groupF" size="1">
                <option value="1">O(I) Rh−</option>
                <option value="2">O(I) Rh+</option>
                <option value="3">A(II) Rh−</option>
                <option value="4">A(II) Rh+</option>
                <option value="5">B(III) Rh−</option>
                <option value="6">B(III) Rh+</option>
                <option value="7">AB(IV) Rh−</option>
                <option value="8">AB(IV) Rh+</option>
            </select>

            <input name="method" type="hidden" value="blood_group">
            <input id="get_gender_results" type="submit" value="What's my baby's gender?">
        </form>

4 个答案:

答案 0 :(得分:3)

您可以在$groupM$groupF上建立一个二维数组,并将它们的值用作索引。在这种情况下,我分配了-101的值来表示三种可能的结果(女孩,未知者和男孩):

$gender = array(1 => array(1 => 1, 0, 0, -1, 1, 0, 0, -1),
                2 => array(1 => 0, 1, 1, 0, 0, -1, -1, 0),
                ...
                8 => array(1 => 0, -1, 1, 0, 0, 1, -1, 0)
                );
if (isset($_POST['groupM']) && isset($_POST['groupF'])) {
    $groupM = $_POST['groupM'];
    $groupF = $_POST['groupF'];
    $result = $gender[$groupM][$groupF];
    switch ($result) {
        case 1:
            echo "It's a girl!";
            break;
        case 0:
            echo "Baby's gender can not be predicted — Rh factor and blood type produce different results.";
            break;
        case -1:
            echo "It's a boy";
            break;
    }
}

答案 1 :(得分:0)

根据您的示例,您可以使用in_array函数来检查性别是否不可预测,您可以尝试执行以下操作:

if($groupM == 1 && in_array($groupF, [2, 3, 6, 7])){
    echo "Baby's gender can not be predicted — Rh factor and blood type produce different results.";
}

答案 2 :(得分:0)

我会将其变成一个函数,以使其更易于处理:

echo returnGender( $_POST['groupM'] , $_POST['groupF'] );

function returnGender( $father , $mother ){
    // Boy Possibilities 
    $boy = [
        [1, 4], [1, 7]
    ];
    // Girl Possibilities 
    $girl = [
        [1, 1], [1, 5]
    ];
    if( in_array( [ $father , $mother ], $boy ) || in_array( [ $mother , $father], $boy ) ){
        return "It's A Boy";
    }
    if( in_array( [ $father , $mother ], $girl )|| in_array( [ $mother , $father], $girl ) ){
        return "It's A Girl";
    }
    return "Baby's gender can not be predicted — Rh factor and blood type produce different results."; 
}

答案 3 :(得分:0)

让我们以按位逻辑的方式用数学方法解决它。

假设您使用的是https://whatsmybabygender.com/parents-blood-types方法,我们有以下表格:

enter image description here enter image description here

我们可以看到,一个不同的恒河猴因子预测一个男孩,一个等于女孩。血型方案相似。如果两种类型都为偶数或奇数,则预示一个女孩,如果偶/奇数不同,则预示一个男孩。当至少有一个血型为III或IV时,会出现一个例外,那就是总是可以通过血型预测男孩。

通常在信息学中,我们对I-IV血型使用基于0的零值枚举。这样,我们使用二进制数00、01、10、11,即2位。如果设置了低位,则ID号为奇数(即,输入I-IV的偶数)。如果第二位被设置,我们将具有III或IV类型之一。

我们将用来表示“ Rhesus factor +”的第三位(值4)。

我们可以生成<option>标签,以保留最后选择的选项,如下所示:

function blood_options($sel)
{
  foreach(['O(I)', 'A(II)', 'B(III)', 'AB(IV]'] as $k => $v)
  {
?>
    <option value="<?php echo $k  ;?>"<?php if($sel === $k  ) echo ' selected';?>><?php echo $v, ' Rh-'?></option>;
    <option value="<?php echo $k|4;?>"<?php if($sel === $k+4) echo ' selected';?>><?php echo $v, ' Rh+'?></option>;
<?php
  }
}

按位XOR操作(PHP中的^运算符)指示两个操作数的每一位的差异。我们可以使用它来检查第3位(数值4,我们的Rhesus系数位)和第0位(数值1,偶数/奇数)。

$m   = intval($_POST['groupM']); // get the numeric value
$f   = intval($_POST['groupF']); // get the numeric value
$xor = $m ^ $f;                  // indicate bitwise differences

现在我们可以找出哪些性别是由血型和恒河猴因素预测的:

$type1 = 1 & $xor          ;   // last bit of (m xor f) => even/odd group different
$type2 = 1 & (($m | $f)>>1);   // at least one of the second bits set => there is a group 2 (III) or 3 (IV)

$type  = $type1 | $type2    ;   // => 1 if at least one of type1 or type2 indicates maskulinum
$rhf   = 1&($xor>>2)        ;   // different rhesus? => 1 indicates maskulinum

$result = $type + $rhf    ;   // sum results in 0: 2x femininum ; 1: 1x m + 1x f ; 2: 2x maskulinum
var_dump($type1, $type2, $type, 

在上面的代码中,我们还使用 SHIFT LEFT 运算符>>将有效位移到了位置0(值1),并用屏蔽了所有其他位。 AND 操作1 &

在将和计算为$result之后,我们得到了

  • 0 = 2个预测:女孩
  • 1 =不同的预测
  • 2 = 2个预测:男孩

将它们放在一起并用短表达式编写,最终看起来像这样:

if(isset($_POST['groupM'], $_POST['groupF']))
{
  $m   = intval($_POST['groupM']);
  $f   = intval($_POST['groupF']);
  $xor = $m ^ $f;

  echo
  [
    "It's a girl",
    "Baby's gender can not be predicted — Rh factor and blood type produce different results.",
    "It's a boy"
  ][(1 & ((($m | $f)>>1) | $xor)) + (1&($xor>>2))];
}

HTML生成:

function blood_options($sel)
{
  foreach(['O(I)', 'A(II)', 'B(III)', 'AB(IV]'] as $k => $v)
  {
?>
    <option value="<?php echo $k  ;?>"<?php if($sel === $k  ) echo ' selected';?>><?php echo $v, ' Rh-'?></option>;
    <option value="<?php echo $k+4;?>"<?php if($sel === $k|4) echo ' selected';?>><?php echo $v, ' Rh+'?></option>;
<?php
  }
}
?>

<form id="get_my_baby_gender" method="POST">
  <label for="groupM">Father's blood group:</label>
  <select class="input_box" id="groupM" name="groupM" size="1">
<?php blood_options($m); ?>
  </select>

  <label for="groupF">Mother's blood group:</label>
  <select class="input_box" id="groupF" name="groupF" size="1">
<?php blood_options($f); ?>
  </select>

  <input name="method" type="hidden" value="blood_group">
  <input id="get_gender_results" type="submit" value="What's my baby's gender?">
</form>