Question

I have a variable called $typethat is either type1 or type2. I have another variable called $price that i want to change depending on what the $type variable is. For some reason in the email that gets sent, there is nothing. I have set $price to some random text outside of the if and then i works, so i know it isn't the mail function. Anybody know why this if statement doesn't work?

PHP

$type = "type 2";

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


$headers = 'From: xxxxxx@gmail.com';
$subject = 'the subject!';
$message = $price; 

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

Thanks

Btw, before people get mad, i have been searching and followed a few things but nothing has worked.

edit the correct way to do it is:

$type = "type 2";

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


    $headers = 'From: xxxxxx@gmail.com';
    $subject = 'the subject!';
    $message = $price; 

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

Thanks Maja

Was it helpful?

Solution

If $type can only be "type1" or "type2", you should write it this way:

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

If the price now appears as "349 kr", you might have a wrong value in $type.

You should also consider

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

OTHER TIPS

Given your post, the only thing I can imagine is that $type doesn't have "type1" or "type2", so $price is never assigned, because the IF statements aren't true.

You can do

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

So at least it will always have something assigned.

Also, you can check what comes inside $type doing var_dump($type)

you should check $type isset or not:

$price='';
if( isset($type) ){
 if( $type == "type1" ) $price = "249 kr";
 if( $type == "type2" ) $price = "349 kr";
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top