我在使用if/then/else
语句时遇到问题。
这是我的背景:
$email = trim($_POST['email']);
$name = trim($_POST['name']);
$lname = trim($_POST['lname']);
$order = trim($_POST['order']);
$tel = trim($_POST['tel']);
$comments = trim($_POST['comments']);
$op ="0000-0000-0000";
$om ="mail@mail.com";
$bronze = "180 EGP/yr";
$silver ="280 EGP/yr";
$gold ="350 EGP/yr";
$plat ="420 EGP/yr";
$free ="EGP/yr";
if ($order == 'bronze') {
echo "Please notice that your order will cost $bronze";
}
现在我如何使用$silver
再次执行此操作?等等silver
,gold
,plat
和free
,或者即使我想添加更多内容。
答案 0 :(得分:7)
您可以使用switch ... case
结构。
但是如果你必须做很简单的事情,你甚至可以使用关联数组重构很多。
示例:
$minerals = array(
'gold' => array(
'color' => 'yellow',
'cost' => 350
),
'silver' => array(
'color' => 'gray',
'cost' => 280
)
);
// check order type
if (!isset($minerals[$order]))
die("Unsupported mineral type ".$order.".");
echo "Please notice that your order will cost "
. $minerals[$order]['cost'] . " EGP/yr.";
答案 1 :(得分:2)
我认为您应该查看switch statement,这样您就可以根据$order
的值执行不同的操作。
答案 2 :(得分:1)
<?php
if($order == "bronze")
{
echo "Please notice that your order will cost $bronze";
}
else if($order == "silver")
{
echo "Please notice that your order will cost $silver";
}
...
?>
最简单的google search会带给你this。
答案 3 :(得分:1)
一种非常丑陋的方式就是这样:
echo 'Please notice that your order will cost '.$$order;
但是,我不建议您这样做,因为它可能会导致很多错误和安全风险。
我会用数组做到这一点:
$plan = array(
'bronze' => '180 EGP/yr',
'silver' => '280 EGP/yr',
'gold' => '350 EGP/yr',
'plat' => '420 EGP/yr',
'free' => 'EGP/yr'
);
if(!array_has_key($order, $plan))){
die('unknown plan');
}else{
echo 'Please notice that your order will cost '.$plan[$order];
}
通过这种方式,即使动态地也可以从db中添加新计划非常容易。
答案 4 :(得分:1)
switch statement实际上更适合这种情况:
<?php
$email = trim($_POST['email']);
$name = trim($_POST['name']);
$lname = trim($_POST['lname']);
$order = trim($_POST['order']);
$tel = trim($_POST['tel']);
$comments = trim($_POST['comments']);
$op ="0000-0000-0000";
$om ="mail@mail.com";
$bronze = "180 EGP/yr";
$silver ="280 EGP/yr";
$gold ="350 EGP/yr";
$plat ="420 EGP/yr";
$free ="EGP/yr";
switch ($order) {
case 'bronze':
echo "Please notice that your order will cost $bronze";
break;
case 'silver':
echo "Please notice that your order will cost $silver";
break;
case 'gold':
echo "Please notice that your order will cost $gold";
break;
case 'plat':
echo "Please notice that your order will cost $plat";
break;
case 'free':
echo "Please notice that your order will cost $free";
break;
default:
echo "Please choose a service package.";
break;
}
答案 5 :(得分:0)
您可以使用variable variable:
执行此操作if(!isset($$order)) {
die("Invalid order type");
}
$cost = $$order;
echo "Please notice that your order will cost $cost";
<强> See it in action 强>
这适用于任意数量的订单类型,无需再次指定,也会检测到无效的类型。
警告:但是,正如另一个代码正确指出的那样,此方法存在安全问题;用户可以通过为$order
制作特殊值来利用这一点,这会导致您的脚本显示任何变量的内容,这可能是一个非常重要的信息泄漏。
优选:如果您使用数组作为订单类型,则可以采用更标准的方式实现相同目标,此解决方案没有任何安全问题:
$types = array(
'bronze' => '180 EGP/yr',
'silver' => '280 EGP/yr',
// etc
);
if(!isset($types[$order])) {
die("Invalid order type");
}
echo "The cost will be ".$types[$order];
答案 6 :(得分:0)
这是你的意思吗?
if ( $gold == 'something' ) {
echo "Please notice that your order will cost $bronze";
}
if ( $silver == 'something' ) {
echo "Please notice that your order will cost $bronze";
}
答案 7 :(得分:0)
希望这有帮助
<?php
$key=0;
switch ($order) {
case "bronze":
$key=$bronze;
break;
case "silver":
$key=$silver;
break;
case "gold":
$key=$gold;
break;
default:
$key=1;
break;
}
echo"Please notice your order will cost $key amount";
?>