2014-01-30 101 views
-2

我有一個名爲$type的變量,即type1type2。我有另一個變量$price,我想要根據什麼$type變量進行更改。出於某種原因,在發送的電子郵件中,沒有任何內容。我已將$price設置爲if和if之外的一些隨機文本,因此我知道這不是郵件功能。任何人都知道爲什麼這個if語句不起作用?爲什麼不通過這個PHP如果聲明工作?

PHP

$type = "type 2"; 

if($type == "type1") $price = "249 kr"; 
if($type == "type2") $price = "349 kr"; 


$headers = 'From: [email protected]'; 
$subject = 'the subject!'; 
$message = $price; 

mail($email, $subject, $message, $headers); 

感謝

順便說一句,人生氣之前,我一直在尋找,隨後的幾件事情,但沒有奏效。

編輯 做正確的做法是:

$type = "type 2"; 

    if($type == "type1") $price = "249 kr"; 
    else     $price = "349 kr"; 


    $headers = 'From: [email protected]'; 
    $subject = 'the subject!'; 
    $message = $price; 

    mail($email, $subject, $message, $headers); 

由於瑪雅

+3

哪裏是'$ type'在代碼中設置?你確定字符串中沒有空格嗎?你有沒有嘗試拋棄變量來看看你有什麼? – j08691

+0

你做了'var_dump($ type)'來看看你在處理什麼? –

+0

「我一直在尋找並遵循幾件事情,但沒有任何成效。」更加詳細一些。你跟隨的這些「東西」是什麼? –

回答

1

如果$type只能是 「TYPE1」 或 「2型」,你應該寫這樣:

if($type == "type1") $price = "249 kr"; 
else     $price = "349 kr"; 

如果價格現在顯示爲「349 kr」,喲你可能在$type中有錯誤的值。

你也應該考慮

if($type == "type1") $price = "249 kr"; else 
if($type == "type1") $price = "349 kr"; 
else     $price = "error"; 
+0

謝謝,這是做的伎倆;) – user2961869

+2

請不要寫代碼在這種風格(水平),並始終把'{}',這將確保你永遠不會錯過任何超出它應該是。 – JorgeeFG

+0

在你的第二個例子中,不應該'$ type =='type1「'是'$ type ==」type2「'? – j08691

0

鑑於你的帖子,我能想象的唯一的事情就是$type沒有"type1""type2",所以$price從未分配,因爲如果內容不完全屬實。

你可以做

if($type === 'type1') { 
    $price = 'something'; 
} 
else { 
    $price = 'something default'; 
} 

所以至少它總是有一些分配。

此外,您還可以檢查什麼來內$typevar_dump($type)

0

你應該檢查$型isset與否:

$price=''; 
if(isset($type)){ 
if($type == "type1") $price = "249 kr"; 
if($type == "type2") $price = "349 kr"; 
} 
相關問題