PHP代码如果使用if else

时间:2018-03-10 16:42:57

标签: php

我的问题是,如果入住至少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;        
    }


    $tickets = $numTravelers * $airFare;
    $hotel = $numTravelers * $numNights * $perNight;
    $totalCost = $tickets + $hotel;

*我无法更改任何代码,只需要添加它即可获得200美元的折扣,他们将前往东京并入住5晚! 如果我输入1名乘客5晚到东京,价值必须是2575美元。

另外,我必须使用嵌套If或复合布尔表达式! 谢谢你的帮助!

这是我的代码如何在这段代码中添加问题以获得200折扣?目的地在东京,至少说每个旅客5晚可获得200折优惠。

3 个答案:

答案 0 :(得分:0)

试试此代码

 $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;

更改为:

$hotel = $numTravelers * $subtotal;

答案 1 :(得分:0)

您应该在东京的代码中添加另一个IF语句。现在,假设您已声明$discount = 200$nights变量。使用此:

elseif($destination == "Tokyo")
    {
    $airFare = 1575;
    $perNight = 240;
if($nights >= 5){
    $total = ($airFare + $perNight) - $discount;   
    }
    }

答案 2 :(得分:0)

我尝试尽可能少地修改您的代码,这里是: 仅限在东京住宿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;
    ?>

这是我建议你的。您可能会发现它非常不同:

创建一个函数,而不是一个冗长的elseif(s)。也可以考虑切换机箱,但我发现简单地使用数组更方便处理。希望有所帮助

<?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