2013-08-31 49 views
-4

我得到一個奇怪的錯誤,我的PHP代碼,我不明白爲什麼!
錯誤:Parse error: syntax error, unexpected '=' on line 9意外的'='在PHP

<?php 
session_start(); 

$name = $_POST['Contact-Name']; 
$address = $_POST['Contact-address']; 
$email = $_POST['Contact-Email']; 
$phone = $_POST['Contact-Phone']; 
$program = $_POST['Program-Name']; 
$date-requested = $_POST['date-requested']; 

$timestart = $_POST['program-start-time']; 
$timeend = $_POST['program-end-time']; 

$timestart-format = $_POST['starttime-format']; 
$timeend-format = $_POST['endtime-format']; 

$full-start-time = $timestart." ".$timestart-format; 
$full-end-time = $timeend." ".$timeend-format; 


//the book king hours 
$mon-thurs-hours = array("10:00 AM", "11:00 AM", "12:00 PM", "1:00 PM", "2:00 PM", "3:00 PM", "4:00 PM", "5:00 PM", "6:00 PM"); 
$friday-hours = array("10:00 AM", "11:00 AM", "12:00 PM", "1:00 PM", "2:00 PM", "3:00 PM", "4:00 PM", "5:00 PM", "6:00 PM", "7:00 PM", "8:00 PM"); 
$saturday-hours = array("10:00 AM", "11:00 AM", "12:00 PM", "1:00 PM", "2:00 PM", "3:00 PM", "4:00 PM", "5:00 PM", "6:00 PM"); 

//find the day of the week and save to $dayofweek 
$date = new DateTime(); 
$timestamp = date_timestamp_get($date-requested); 
$dayofweek = date("w", $timestamp); 

//if sunday 
if($dayofweek == 0){ 
echo "You choose Sunday!"; 
die('Sorry, the book king is closed on Sundays!'); 
} 

//if monday, tues, wed, thurs 
if(($dayofweek == 1)||($dayofweek == 2)||($dayofweek == 3)||($dayofweek == 4)){ 
echo "You choose day ".$dayofweek."!"; 
//see if bk is open at the specified times 
if (in_array($full-start-time, $mon-thurs-hours)) { 
    echo "Start time is okay!"; 
    } 
    if (in_array($full-end-time, $mon-thurs-hours)) { 
     echo "End time is okay!"; 
    } 
} 

//if friday 
if($dayofweek == 5){ 
    echo "You choose day ".$dayofweek."!"; 
//see if bk is open at the specified times 
if (in_array($full-start-time, $friday-hours)) { 
    echo "Start time is okay!"; 
} 
    if (in_array($full-end-time, $friday-hours)) { 
     echo "End time is okay!"; 
    } 
} 

//if saturday 
if($dayofweek == 6){ 
echo "You choose day ".$dayofweek."!"; 
    //see if bk is open at the specified times 
    if (in_array($full-start-time, $saturday-hours)) { 
     echo "Start time is okay!"; 
    } 
    if (in_array($full-end-time, $saturday-hours)) { 
     echo "End time is okay!"; 
    } 
} 

?> 

我不知道,如果你需要所有的代碼,或只是第9行,但我張貼的所有以防萬一!

我非常感謝您的幫助!

+0

如何從與以上不同的是行的變量?你在PHP中使用不同的符號是什麼? – geoffspear

+0

可能重複的[Mail Script - Parse error:syntax error,unexpected'='](http://stackoverflow.com/questions/18294546/mail-script-parse-error-syntax-error-unexpected) – mario

+0

可能重複的[PHP分析/語法錯誤;和如何解決它們?](http://stackoverflow.com/q/18050071) – mario

回答

3

問題出在$date-requested標識符。您不能在標識符中使用-。標識符可能只包含字母,數字和下劃線(_),並且必須以字母或者下劃線開頭。

所以它被解釋爲表達式(變量$date減去常量requested),並且整行作爲該表達式的分配,這對PHP處理器沒有意義。這就是爲什麼你會得到這樣奇怪的錯誤信息。

使用有效的變量名狀$date_requested

+0

謝謝!從來沒有注意到'''被禁止。在$ _POST變量中怎麼樣?它會在那裏引起問題嗎? – pattyd

+0

''是一個數學運算符。顯然你不能在變量名中使用它......但對錶單元素來說很好。 –

+0

'$ _POST'數組就像所有其他PHP數組一樣,允許任何字符串作爲關鍵字。所以'$ _POST ['date-requested']'是有效的,但我不建議故意使用這樣的字符串鍵,而不是有效的標識符,只是因爲以後你可能想使用'extract'到數組上或者將它轉換爲目的。 –

1

$date-requested不是有效的變量名稱(不能包含連字符),請使用$date_requested或類似的替代。