2014-11-15 78 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>"); 

?>