2011-09-08 134 views
0
<?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/'); 
    } 
?> 

它说第4行有T_IF错误。什么问题?这个T_IF错误是什么意思?

+0

它意味着 “发现'Token_IF'('if')其中'if'是无效的”。请参阅therin的答案,了解它为什么不是一个有效的表达式(只有表达式可以用作右值)。 – 2011-09-08 21:57:54

回答

3

不能使用if那里,这是一个语法错误。技术上if是一个声明,而不是一个表达式。这意味着你不能在像$types = if (...)这样的任务中使用它。

1

IF语句没有返回一个值,因此将它赋值给一个变量什么也不做(甚至可能导致你的错误!)也从if语句末尾取下分号。

试试这个:

if (!empty($some_variable)) { 
    $my_var = $some_variable; 
} 
2

if()是一种语言结构,而不是函数。它不返回任何内容,也不能分配给变量。

$types = if(!empty($_REQUEST['type'])) {echo($_REQUEST['type'] . ". ");}; 
^^^^^^^^--- not allowed 

尝试:

if (!empty($_REQUEST['type']) { 
    $types = $_REQUEST['type']; 
} 

同样,回声造成直接输出到客户端。它不会“返回”任何可以分配的东西。

+1

更好的是“if()是一个语句,而不是一个表达式”(因为在PHP中,一些语言构造如include,print或eval会返回一些东西。) – NikiC

0

我能看到的第一件事是在行$message = …有一个双连接运算符,这显然是一个语法错误。应该(并且应该使用逸出的输出):

$message = $types . $reps . "\n\nKommentarer:\n" . $_REQUEST['kommentarer'] . "\n\nFra:\n" . $_REQUEST['navn'] . "\nTelefon: " . $_REQUEST['telefon'] . "\nEmail: " . $_REQUEST['email'] . "\nBosted: " . $_REQUEST['bosted']; 

ps。天哪,这么多的错误与此代码(仍然没有到位转义/ sanitazation)...

<?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/'); 
    } 
?> 
0

线条

$types = if(!empty($_REQUEST['type'])) {echo($_REQUEST['type'] . ". ");}; 
$reps = if(!empty($_REQUEST['rep'])) {echo($_REQUEST['rep']);}; 

无效。 if语句不是PHP中的表达式;他们不评估可以分配给变量的值。你也不会从if“返回”任何东西; echo写入屏幕,它不会将if语句中的某个值“回显”给调用范围。

你想以下几点:

if(!empty($_REQUEST['type'])) { 
    $types = ($_REQUEST['type'] . ". "); 
}