2014-11-15 81 views
-1

我试图让它显示正确的东西。当我运行它时,酒店费用保持在960美元,如果我选择开罗,它将显示$ 0的航空公司门票,而酒店的价格为960美元。其他人工作正常,但不会改变从960美元的酒店成本。这个IF声明有什么问题

if ($destination == "Barcelona") 

    $airFare = 875; 
    $hotel = 85 * $numNights; 

    if ($destination == "Cairo") 
    $airfare = 950; 
    $hotel = 98 * $numNights; 

    if ($destination == "Rome") 
    $airFare = 875; 
    $hotel= 110 * $numNights; 

    if ($destination == "Santiago") 
    $airFare = 820; 
    $hotel = 85 * $numNights; 

    if ($destination == "Tokyo") 
    $airFare = 1575; 
    $perNight = 240; 

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

    print("<p>Destination: $destination<br />"); 
    print("Number of people: $numTravelers<br />"); 
    print("Number of nights: $numNights<br />"); 
    print("Airline Tickets: $".number_format($tickets, 2)."<br />"); 
    print("Hotel Charges: $".number_format($hotel, 2)."</p>"); 
    print("<p><strong>TOTAL COST: $".number_format($totalCost, 2)."</strong></p>"); 
+1

使用{}挡住代码,如果你不使用它们只剩下一行会if语句 – Emz

回答

1

的几个问题:

  • 大忌:您正在使用大括号少if声明没有压痕,因此目前还不清楚你的意图是什么。为了安全起见:始终使用括号与if,除非它们都在一条线上!这是今年早些时候导致苹果关键OpenSSL错误的原因。
  • $numNights未定义,并且未定义的数字在PHP中默认为0。任何零的产品都是......零。因此,当你的计算涉及到$numNights时,你的计算结果会出错。
  • 您已将$字符嵌入到双引号字符串中,这意味着PHP将尝试解析这些字符串的变量名称。通过使用\$或使用单引号字符串来逃避符号。
+0

的一部分,为什么出来到960 $,而不是为0?编辑:修复它。谢谢 – Victor

0
<?php 
$destination = "Cairo"; 
$numNights = (int)10; 
$airFare = (int)1000; 
$numTravelers = (int)2; 
$perNight = (int)49; 

if ($destination == "Barcelona") 
{ 
    $airFare = 875; 
    $hotel = 85 * $numNights; 
} 

if ($destination == "Cairo") 
{ 
    $airfare = 950; 
    $hotel = 98 * $numNights; 
} 

if ($destination == "Rome") 
{ 
    $airFare = 875; 
    $hotel= 110 * $numNights; 
} 

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

print("<p>Destination: $destination<br />"); 
print("Number of people: $numTravelers<br />"); 
print("Number of nights: $numNights<br />"); 
print("Airline Tickets: $".number_format($tickets, 2)."<br />"); 
print("Hotel Charges: $".number_format($hotel, 2)."</p>"); 
print("<p><strong>TOTAL COST: $".number_format($totalCost, 2)."</strong></p>"); 

?>