You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP POST方法中if else分支优化及动物匹配结果输出实现问题

实现方案

1. 匹配成功后输出动物名称

先完善Animal类的构造方法,将名称属性纳入初始化参数,实例化每个动物时传入对应名称,匹配成功后直接调用该属性输出即可,无需在HTML中硬编码动物名称。

2. 多动物匹配逻辑优化

将所有动物实例存入统一的数组中管理,收到用户提交的参数后遍历数组做属性匹配即可。后续新增动物仅需要往数组中添加新的实例,无需修改匹配逻辑,即使有上百个动物也不需要写大量elseif分支。

优化后完整代码

<?php
class Animal{
    public $name;
    public $brain;
    public $stomach;
    public $heart;
    public $leg;

    function __construct($name, $aBrain, $aStomach, $aHeart, $aLeg)
    {
        $this->name = $name;
        $this->brain = $aBrain;
        $this->stomach = $aStomach;
        $this->heart = $aHeart;
        $this->setLeg($aLeg);
    }
    
    function isBipedal(){
        if ($this->leg == 2) {
            return "是两足动物";
        }else {
            return "不是两足动物";
        }
    }

    function getLeg(){
        return $this->leg;
    }

    function setLeg($leg){
        if (in_array($leg, [2,4,8])) {
            $this->leg= $leg; 
        }else {
            $this->leg= "非足类"; 
        }
    }
}

// 所有动物统一存入数组,新增动物只需要在这里加一行即可
$animals = [
    new Animal('章鱼',9,1,3,8),
    new Animal('牛',1,4,1,4),
    new Animal('人类',1,1,1,2)
];

// 测试输出
echo "<br>章鱼的腿数:".$animals[0]->getLeg();
echo "<br>人类:".$animals[2]->isBipedal(). "<br>"; 

if(isset($_POST['submit']))  { 
    if(isset($_POST['brain']) 
    && isset($_POST['stomach'])  
    && isset($_POST['heart']) 
    && isset($_POST['leg']) ){

        $myBrain = $_POST['brain'];
        $myStomach = $_POST['stomach'];
        $myHeart = $_POST['heart'];
        $myLeg = $_POST['leg'];
        
        $matchedAnimal = null;
        // 遍历数组匹配,不用写多个elseif
        foreach ($animals as $animal) {
            if (
                $myBrain == $animal->brain &&
                $myStomach == $animal->stomach &&
                $myHeart == $animal->heart &&
                $myLeg == $animal->leg
            ) {
                $matchedAnimal = $animal;
                break;
            }
        }

        if ($matchedAnimal) {
            $nameKey = strtolower($matchedAnimal->name);
?>
            <div class="animal-info">
                <div><p class="blinking">你是<?php echo $matchedAnimal->name; ?></p></div>
                <img src="media/<?php echo $nameKey; ?>.jpg" alt="<?php echo $matchedAnimal->name; ?>" class="animal-img">
                <img src="media/<?php echo $nameKey; ?>.gif" alt="<?php echo $matchedAnimal->name; ?>" class="animal-img">
            </div>
<?php
        } else {
            echo "没有匹配到任何动物"; 
        }
    }
}
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>动物属性匹配</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <form action="" method="post">
        <span>你有多少个大脑?</span>
        <input type="text" name="brain" id="brain">
    <br>
        <span>你有多少个胃?</span>
        <input type="text" name="stomach" id="stomach">
    <br>
        
        <span>你有多少个心脏?</span>
        <input type="text" name="heart" id="heart">
    <br>
        
        <span>你有多少条腿?</span>
        <input type="text" name="leg" id="leg"> 
    <br>
        <input type="submit" name="submit"  value="看看我是什么动物">
    </form>
</body>
</html>

内容的提问来源于stack exchange,提问作者Imrul Kayes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 19:54:01