2014-01-30 35 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"; 
} 
相关问题