2011-09-08 132 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'] . ". "); 
}