PHP代码如何使用if-else
我的问题是,东京的目的地是否为每个至少住5晚的旅行者提供200美元的折扣PHP代码如何使用if-else,php,Php,我的问题是,东京的目的地是否为每个至少住5晚的旅行者提供200美元的折扣 if($destination == "Barcelona") { $airFare = 875; $perNight = 85; } else if($destination == "Cairo") { $airFare = 950; $perNight = 98; } else if($destination == "Rome"
if($destination == "Barcelona")
{
$airFare = 875;
$perNight = 85;
}
else if($destination == "Cairo")
{
$airFare = 950;
$perNight = 98;
}
else if($destination == "Rome")
{
$airFare = 875;
$perNight = 110;
}
else if($destination == "Santiago")
{
$airFare = 820;
$perNight = 85;
}
elseif($destination == "Tokyo")
{
$airFare = 1575;
$perNight = 240;
}
$tickets = $numTravelers * $airFare;
$hotel = $numTravelers * $numNights * $perNight;
$totalCost = $tickets + $hotel;
*我不能在这里更改任何代码,只需要添加它就可以获得200美元的折扣,如果你要去东京住5晚!
if($destination == "Barcelona")
{
$airFare = 875;
$perNight = 85;
}
else if($destination == "Cairo")
{
$airFare = 950;
$perNight = 98;
}
else if($destination == "Rome")
{
$airFare = 875;
$perNight = 110;
}
else if($destination == "Santiago")
{
$airFare = 820;
$perNight = 85;
}
elseif($destination == "Tokyo")
{
$airFare = 1575;
$perNight = 240;
}
$discount = ($destination == "Tokyo" && $numNight == 5)? 200 : 0; // condition is here
$tickets = $numTravelers * $airFare;
$hotel = $numTravelers * $numNights * $perNight;
$totalCost = $tickets + $hotel - $discount;
?>
如果我输入1名乘客到东京5晚,价值必须是2575美元
另外,我必须使用嵌套的If或复合布尔表达式!
谢谢你的帮助
这是我的代码我如何加起来的问题,以获得200折扣在此代码?目的地在东京,至少说每人5晚可享受200折优惠 试试这段代码
$subtotal=0;
elseif($destination == "Tokyo")
{
$airFare = 1575;
$perNight = 240;
if($numNights >= 5){
$subtotal= ($perNight*numNights)-200;
}else{
$subtotal= ($perNight*numNights)-200;
}
}
$tickets = $numTravelers * $airFare;
$hotel = $numTravelers * $numNights * $perNight;
if($destination == "Barcelona")
{
$airFare = 875;
$perNight = 85;
}
else if($destination == "Cairo")
{
$airFare = 950;
$perNight = 98;
}
else if($destination == "Rome")
{
$airFare = 875;
$perNight = 110;
}
else if($destination == "Santiago")
{
$airFare = 820;
$perNight = 85;
}
elseif($destination == "Tokyo")
{
$airFare = 1575;
$perNight = 240;
}
$discount = ($destination == "Tokyo" && $numNight == 5)? 200 : 0; // condition is here
$tickets = $numTravelers * $airFare;
$hotel = $numTravelers * $numNights * $perNight;
$totalCost = $tickets + $hotel - $discount;
?>
改为:
您应该在Tokyo的代码中添加另一个IF语句。现在,假设您已声明$discount=200和$nights变量。使用以下命令:
elseif($destination == "Tokyo")
{
$airFare = 1575;
$perNight = 240;
if($nights >= 5){
$total = ($airFare + $perNight) - $discount;
}
}
if($destination == "Barcelona")
{
$airFare = 875;
$perNight = 85;
}
else if($destination == "Cairo")
{
$airFare = 950;
$perNight = 98;
}
else if($destination == "Rome")
{
$airFare = 875;
$perNight = 110;
}
else if($destination == "Santiago")
{
$airFare = 820;
$perNight = 85;
}
elseif($destination == "Tokyo")
{
$airFare = 1575;
$perNight = 240;
}
$discount = ($destination == "Tokyo" && $numNight == 5)? 200 : 0; // condition is here
$tickets = $numTravelers * $airFare;
$hotel = $numTravelers * $numNights * $perNight;
$totalCost = $tickets + $hotel - $discount;
?>
我尝试尽可能少地修改您的代码,如下所示: 编辑为仅在东京停留5晚将获得一次性200美元折扣
if($destination == "Barcelona")
{
$airFare = 875;
$perNight = 85;
}
else if($destination == "Cairo")
{
$airFare = 950;
$perNight = 98;
}
else if($destination == "Rome")
{
$airFare = 875;
$perNight = 110;
}
else if($destination == "Santiago")
{
$airFare = 820;
$perNight = 85;
}
elseif($destination == "Tokyo")
{
$airFare = 1575;
$perNight = 240;
}
$discount = ($destination == "Tokyo" && $numNight == 5)? 200 : 0; // condition is here
$tickets = $numTravelers * $airFare;
$hotel = $numTravelers * $numNights * $perNight;
$totalCost = $tickets + $hotel - $discount;
?>
这就是我给你的建议。你可能会发现它非常不同:
创建一个函数,而不是一个冗长的elseifs。也可以考虑开关情况,但我发现简单地使用数组更便于处理。希望有帮助
<?php
function getCost($destination, $numTravelers, $numNights) {
// organize them in an array format
$prices["Barcelona"] = ['airfare' => 875, 'perNight' => 85, 'discount' => 0];
$prices["Cairo"] = ['airfare' => 950, 'perNight' => 98, 'discount' => 0];
$prices["Rome"] = ['airfare' => 875, 'perNight' => 110, 'discount' => 0];
$prices["Tokyo"] = ['airfare' => 875, 'perNight' => 110, 'discount' => 200];
$min = 5; // minimum stay for discount
// just get the value by providing the key (destination name)
$tickets = $prices[$destination]['airfare'] * $numTravelers;
$hotel = $numTravelers * $numNights * $prices[$destination]['perNight'];
$total = ($numNights = $min) ? $tickets + $hotel - $prices[$destination]['discount'] : $tickets + $hotel;
// just for viewing the output
echo 'Travel to ' . $destination . ' for ' . $numTravelers . ' people for ' . $numNights . ' nights cost: ' . $total . '<br>';
return $total;
}
$cost1 = getCost('Tokyo', 3, 4);
$cost2 = getCost('Tokyo', 3, 5);
?>
输出
3人到东京旅游4晚费用:3945
3人到东京旅行5晚费用:4075您的停留时间变量在哪里?需要更多关于代码的详细信息谢谢您的评论,但您的代码不起作用。谢谢,你能再解释一下吗?是的,我的代码中没有$subtotal。如何从中获得小计?我需要添加一个代码,使每位乘客在东京至少住5晚可以获得200折扣。您可以定义一个变量,该变量将在应用折扣之前或之后保留总计。我在下面添加了更多详细信息。感谢您的反馈!如果您需要帮助,请提供更多详细信息谢谢您的帮助,但这不起作用!我为这个问题加了更多的细节。谢谢,我不能使用你的代码,这不是我想要的。很抱歉我必须使用嵌套If或复合布尔表达式。谢谢你,迈克!你的第二个代码工作,如果我输入5晚,我得到200折扣,但有一个问题。如果我输入大于5,如6或7,我不会得到200折扣。为什么呢?如果数量大于5,则必须获得200美元的折扣。您想要一次性折扣200美元,还是想要每晚超过200美元?那么第5晚+第6晚+第7晚..总共600美元?还是200美元?我的意思是,如果我在东京住6晚,就没有200美元的折扣。@BoramLamppa:对不起,你不能这么做。这些问题,连同它们的答案,都是为未来的游客准备的,像你这样彻底地改变这些部分,就是一种破坏行为。如果你有一个新问题,那么一定要按照程序申请一个新职位,包括做你的研究等。
if($destination == "Barcelona")
{
$airFare = 875;
$perNight = 85;
}
else if($destination == "Cairo")
{
$airFare = 950;
$perNight = 98;
}
else if($destination == "Rome")
{
$airFare = 875;
$perNight = 110;
}
else if($destination == "Santiago")
{
$airFare = 820;
$perNight = 85;
}
elseif($destination == "Tokyo")
{
$airFare = 1575;
$perNight = 240;
}
$discount = ($destination == "Tokyo" && $numNight == 5)? 200 : 0; // condition is here
$tickets = $numTravelers * $airFare;
$hotel = $numTravelers * $numNights * $perNight;
$totalCost = $tickets + $hotel - $discount;
?>