Reputation: 79
<?php
$to = "[email protected]";
$subject = "Reparasjon av " . $_REQUEST['type'] . " fra mysite.no";
$types = if(!empty($_REQUEST['type'])) {echo($_REQUEST['type'] . ". ");};
$reps = if(!empty($_REQUEST['rep'])) {echo($_REQUEST['rep']);};
$message = $types . . $reps . "\n\nKommentarer:\n" . $_REQUEST['kommentarer'] . "\n\nFra:\n" . $_REQUEST['navn'] . "\nTelefon: " . $_REQUEST['telefon'] . "\nEmail: " . $_REQUEST['email'] . "\nBosted: " . $_REQUEST['bosted'];
$headers = "From: " . $_REQUEST['email'] . "\r\n" . 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/plain; charset=UTF-8' . "\r\n";
if (mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=', $message, $headers)) {
header( 'Location: http://www.mysite.no/' );
} else {
header( 'Location: http://www.mysite.no/' );
}
?>
It says there's a T_IF error in line 4. What's the problem?
Upvotes: 0
Views: 279
Reputation: 239402
The lines
$types = if(!empty($_REQUEST['type'])) {echo($_REQUEST['type'] . ". ");};
$reps = if(!empty($_REQUEST['rep'])) {echo($_REQUEST['rep']);};
are invalid. if
statements are not expressions in PHP; they don't evaluate to a value which can be assigned to a variable. You're also not "returning" anything from the if
; echo
writes to the screen, it doesn't "echo" a value out of an if statement to the calling scope.
You want the following:
if(!empty($_REQUEST['type'])) {
$types = ($_REQUEST['type'] . ". ");
}
Upvotes: 0
Reputation: 265574
The first thing I can see is in line $message = …
there is a double concatenation operator, which is clearly a syntax error. Should be (and should use escaped output):
$message = $types . $reps . "\n\nKommentarer:\n" . $_REQUEST['kommentarer'] . "\n\nFra:\n" . $_REQUEST['navn'] . "\nTelefon: " . $_REQUEST['telefon'] . "\nEmail: " . $_REQUEST['email'] . "\nBosted: " . $_REQUEST['bosted'];
ps. god, so much wrong with this code (still no escaping/sanitazation in place) …
<?php
$to = "[email protected]";
$subject = "Reparasjon av " . $_REQUEST['type'] . " fra mysite.no";
$types = !empty($_REQUEST['type']) ? $_REQUEST['type'] . ". " : '';
$reps = !empty($_REQUEST['rep']) ? $_REQUEST['rep'] : '' ;
$message = $types . $reps . "\n\nKommentarer:\n" . $_REQUEST['kommentarer'] . "\n\nFra:\n" . $_REQUEST['navn'] . "\nTelefon: " . $_REQUEST['telefon'] . "\nEmail: " . $_REQUEST['email'] . "\nBosted: " . $_REQUEST['bosted'];
$headers = "From: " . $_REQUEST['email'] . "\r\n" . 'MIME-Version: 1.0' . "\r\n" . 'Content-type: text/plain; charset=UTF-8' . "\r\n";
if (mail($to, '=?UTF-8?B?'.base64_encode($subject).'?=', $message, $headers)) {
header( 'Location: http://www.mysite.no/' );
} else {
header( 'Location: http://www.mysite.no/' );
}
?>
Upvotes: 0
Reputation: 360762
if()
is a language construct, not a function. It does not return anything, and cannot be assigned to a variable.
$types = if(!empty($_REQUEST['type'])) {echo($_REQUEST['type'] . ". ");};
^^^^^^^^--- not allowed
Try:
if (!empty($_REQUEST['type']) {
$types = $_REQUEST['type'];
}
As well, echo cause direct output to the client. it does not "return" anything that can be assigned.
Upvotes: 2
Reputation: 522412
You can't use if
there, it's a syntax error. Technically if
is a statement, not an expression. That means you can't use it in an assignment like $types = if (...)
.
Upvotes: 3
Reputation: 1468
An IF statement does not return a value, so assigning it to a variable does nothing (and might even be causing your error!) Take off the semicolons from the end of the if statements, too.
Try this:
if (!empty($some_variable)) {
$my_var = $some_variable;
}
Upvotes: 1