dro80463
dro80463
2011-11-07 23:04

如何让PHP在IF语句中回显错误?

  • echo
  • php
  • printing
已采纳

How do i print an error if one of these are not working or rather not display some of these?

if ( $what === $toyota ) {
$print = "Camry";
} elseIF($what === $benz) {
$print = "S350";
} elseIF($what === $bmw) {
$print = "M5";
} elseIF($what === $honda) {
$print = "Accord";
} elseIF($what === $acura) {
$print = "mdx";
} elseIF($what === $jaguar) {
$print = "rx";
} elseIF($what === $landrover) {
$print = "rover";
} elseIF($what === $ford) {
$print = "sucks";
} elseIF($what === $gm) {
$print = "garbage";
} elseIF($what === $saturn) {
$print = "sat";
} elseIF($what === $jeep) {
$print = "not sure";
} elseIF($what === $chevy) {
$print = "whatever";
} elseIF($what === $porsche) {
$print = "Panamera";
} elseIF($what === $volkswagan) {
$print = "Passat";

}

print("$print");
}

}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • dpz90118 dpz90118 10年前

    Each one of your logic branches is comparing the variable $what to another variable named after a make of car. ie ($what === $honda) Where and what are you setting all these different variables to before the logic begins? Seems like it would be easier to write ($what === "honda") or use a switch statement like everyone recommends.

    Also on printing out the variable, you don't need quotes.

    Just print($print) and why not print($what) to see exactly what it is.

    点赞 评论 复制链接分享
  • dongwen3093 dongwen3093 10年前

    how about adding a simple else at the end ?

    } elseIF($what === $volkswagan) {
        $print = "Passat";
    } else {
        $print = "ERROR !!!";
    }
    
    点赞 评论 复制链接分享
  • doutao4480 doutao4480 10年前
    $map = array($wolskwagan => 'Passat', $chevy => 'whatever');
    if (isset($map[$what])) print $map[$what]; else print 'error'; `
    
    点赞 评论 复制链接分享