首页 > 解决方案 > PHP IF 语句要使用 Else If 还是另一个 IF 语句?

问题描述

我正在尝试编写一个解决以下问题的 PHP 语句。我的尝试没有采取。基本上我有关于加热类型的编码项目。我需要一个 PHP 语句,上面写着 If x = 356 then display Gas if x = 357 then display Oil if x = 358 display coal等等。这是我开始的,我是php的新手,拜托帮助!

    <?php
function heatfuel_function( $heatfuel ) {
if ( heatfuel = "543" ) {
    echo "Electric";
}

if (heatfuel = "544") {
    echo "LP Gas";
}

if (heatfuel = "545") {
    echo "Natural Gas";
}

if (heatfuel = "546") {
    echo "Natural Available Gas";
}

if (heatfuel = "550") {
    echo "Multi Fuel";
}

if (heatfuel = "552") {
    echo "Oil Fuel";
}

if (heatfuel = "556") {
    echo "Wood Fuel";
}

} else {
    echo "";
}
?>

标签: phpif-statement

解决方案


你应该调查一份case switch声明。并default用作您的“其他”

IE

switch ($heatfuel) {
case 543:
     echo "LP Gas";
    break;
case 545:
     echo "Natural Gas";
    break;
case 546:
   echo "Natural Available Gas";
    break;


 //  As many cases as you want ..  

 default:
   echo "None of the above";
}

来自 PHP.NET 的参考


推荐阅读