我在使用这些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";
}
Run Code Online (Sandbox Code Playgroud)
现在我该怎么办呢$silver?等像silver,gold,plat和free甚至如果我想添加更多.
您可以使用该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.";
Run Code Online (Sandbox Code Playgroud)