PHP IF语句要使用其他If或另一个IF语句?

时间:2019-09-24 22:39:03

标签: php if-statement

我正在尝试编写解决以下问题的PHP语句。我没有尝试。基本上,我已经编码了有关加热类型的项目。我需要一个PHP语句,如果x = 356,然后显示Gas,如果x = 357,然后显示Oil,如果x = 358,则显示煤炭,依此类推。等等。这就是我刚开始的工作,我是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 "";
}
?>

1 个答案:

答案 0 :(得分:0)

您应该查看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";
}

REFERENCE FROM PHP.NET